Owner draw menus with MFC
Richard Tearle -- Richard.Tearle@flightdata.co.uk
Friday, March 21, 1997
Environment: Visual C++ 4.2-flat + NT 4.0 sp2
What I though would be a simple question .....
How can I have owner draw menus using MFC ?
I want to have all menus throughout the application owner draw. The
owner bit is to display an icon to the left of the menu item, such as
Office 97 (I think it's a good idea). I don't what to do to much special
to the rest of my application. I expect to have a app-global (member of
CwinApp) image list and id maps into this image list. When I have to
draw the menu item, I do a quick search in this list, if there is an
image, show the image.
The trouble is, I don't get DrawItem or MeasureItem (I've look in the
KB, and tried Q143209, but to no avail).
Any suggestions would be welcome.
Richard.
Niels Ruetthard -- ruetthard@office.ifum.uni-hannover.de
Tuesday, March 25, 1997
Hi Richard,
I had the same problem a few month ago. Here ist my solution that works quit fine.
1. Derive a class from CMenu
2. Override MeasureItem --> sets the Size of the menu item
3. Override DrawItem to draw the various states of the menu item (selected, focus etc.)
void CBitmapMenu::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{ ...
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
if (lpDIS->itemAction & ODA_DRAWENTIRE)
...
if ((lpDIS->itemState & ODS_SELECTED) && (lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
...
if (!(lpDIS->itemState & ODS_SELECTED) && (lpDIS->itemAction & ODA_SELECT))
...
Print your stuff into pDC
4. Build your Menu (and submenus) of class CBitmapMenu in the document. -> m_myNewMenu
5. Attach the menu to the desired master menu in the menu bar. I used mine as popup menus in a form view as follows.
CWnd* pWndPopupOwner = this;
GetDocument()->m_myNewMenu.TrackPopupMenu(TPM_LEFTALIGN, left, top, pWndPopupOwner, NULL);
Hope it helps.
Niels Ruetthard
University of Hannover
Become an MFC-L member
| Вернуться в корень Архива
|