CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

Home >> Visual C++ / C++ >> Controls >> Menu >> Bitmapped menus


Owner Drawn Menu with Icons (4) (automatically uses toolbar res)
Rating: none

Iuri Apollonio (view profile)
August 6, 1998

This is another way to draw a menu with bitmaps item. Basically, this thing was created to let the user see visually the correspondence of a menu item and a toolbar button, so that he can learn easily the meanings of sometime strange button drawing.

Now, the Visual C++ give us that new nice toolbar resources .. why bother with icons or bitmaps and link them to menus while we can automatically "connect" the toolbar resource to them ?
(continued)



So I wrote a small class, CMenuSpawn, which takes care with some help from FrameWnd of menu remapping and drawing.
ownerdrawmenu4.gif (2479 bytes)

What has been updated, changed, added since the last version:
06.22.1998:

  • fixed something in documentation
06.14.1998:
  • fixed again standard menu font different from control panel settings
  • add support for having the main menu (on title bar) highlighted with 3d boxes instead of standard highlight color (this has been implemented in both MDI and SDI code, but tested only under MDI .. hope it will work with SDI too)
05.14.1998:
  • fix a bug that crashed when trying a not-existing shortcut under NT (thanks, Craig!)
  • enhanced drawing of disabled bitmap (thanks to Michael Santoro)
  • fixed standard menu font different from control panel settings
04.28.1998:
  • fix some memory/resource leaks reported from BoundsChecker but not from Msdev (I'm not an owner of BoundsChecker - thanks to H.Tome, C. Schmidt, and all the others who report them)
  • added support for shortcut for menu items and popups
  • added support for a user font for the menu
  • added support for user defined color of menu items
  • added support for using bitmap as menu background
  • (hope to have) fixed the bug who sometimes make the text (especially of popups) disappear and/or enlarge VERY much the popup - however, yet to understand why it did so :)
  • added a contructor with a boolean value to be used when creating popup menu (look at demo, in the view right mouse click)
  • added some base classes for CFrameWnd, CMDIFrameWnd and CMDIChildFrameWnd to make it easier the work; you can now simply rederive your CMainFrame from CSpawnFrameWnd or CSpawnMDIFrameWnd and eventually your CChildFrame from CSpawnMDIChildWnd to get the WM_MEASUREITEM, WM_DRAWITEM, WM_INITMENU, WM_INITMENUPOPUP handled

Now I will explain how to use it in a MDI application:

Step 1: including files

We need to include the CSpawnMenu class in our application and the Bitmap resource named IDB_MENUCHK from the sample application which comes along (it will be used to draw "check marks" aside menu item which need it - you can freely change it).

Step 2: handling frame messages

We also need to manage some of the frame windows handler; we have 2 ways to do this. The simplest is to derive the CMainFrame from the CSpawnMDIFrameWnd class instead of CMDIFrameWnd. This means you have to (using Search/Replace) replace all istance of CMDIFrameWnd with CSpawnMDIFrameWnd (and of course include in your project file the files SpawnMDIFrameWnd.cpp and SpawnMDIFrameWnd.h). You will also need to do this for the CChildFrame class, deriving from CSpawnMDIChildWnd and including SpawnMDIChildWnd.cpp/.h. The other way is to look at how this CSpawnFramexxx classes are been made and copy it (that means putting a CMenuSpawn member in the frame class header and handling the WM_DRAWITEM, WM_MEASUREITEM, WM_MENUCHAR, WM_INITMENU and WM_INITMENUPOPUP).

If you are making an SDI application instead of an MDI, derive the CFrameWnd from CSpawnFrameWnd and include the SpawnFrameWnd.cpp/.h files.

Step 3: initializing the CSpawnClass

We need to modify the CMainFrame class in this way:
  • add a CMenuSpawn cSpawn instance in the include file; note that if you use a CSpawnFramexxx class as base class for mainframe, you already get an instance of CMenuSpawn named cSpawn.
  • initialize the CMenuSpawn class in the CMainFrame constructor (cSpawn.LoadToolBarResource(IDR_MAINFRAME)) this will load the toolbar resource into the class; you can alse use the AddToolBarResource function to add more than toolbar resource to the class. In the demo app, I add a toolbar for the system menu
CMainFrame::CMainFrame()
{
	cSpawn.LoadToolBarResource(IDR_MAINFRAME);
}

Step 4: Note to Stingray Software's Objective Toolkit Pro users

If you are using the OT Pro, maybe along with the SECMenuBar to obtain Office97 menu button look and feel, you will also need to override the WindowProc virtual of the CMainFrame as noted below:
LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	if (message != WM_MEASUREITEM) return SECFrameWnd::WindowProc(message, wParam, lParam);
	else return CFrameWnd::WindowProc( message, wParam, lParam );
}
If you are using another OTPro class other than SECFrameWnd as base class, change SECFrameWnd to reflect your base class. Note however that their very last release of OTPro support bitmapped menu (but not popups).

Note for misterious bug in the system menu (usually in SDI application)

It has been reported (thanks to Alexander V. Kudakov) that often in SDI application the system menu get corrupted. The fix is simple and mystic enought (magic here, guy!):
in the CMainframe::OnCreate() you'll have to call:
GetSystemMenu(false);
and thak's all, folks! If someone is able to explain why this fix works to us, Alexander and I will be glad to know.

Note for new 3d highlight of main menu items on menubar

I've added the 3d highlight of main menu items; look at source code for CSpawnMDIFrameWnd for understanding how. I've written code for those function:
void OnEnterMenuLoop(BOOL bIsTrackPopupMenu);
void OnExitMenuLoop(BOOL bIsTrackPopupMenu);
UINT OnNcHitTest(CPoint point);
void OnTimer(UINT nIDEvent);
int OnCreate(LPCREATESTRUCT lpCreateStruct);
void OnDestroy();
Actually, if you don't want this highlight system, you'll have to remove it by yourself .. I've not yet included a way to enable/disable it. If you are making a MDI application, look at the InitInstance function of the app class for the code between the // 3D HIGHLIGHT CODE and // 3D HIGHLIGHT CODE END comments; you'll need to do this in your app too or the initial menu will be ugly to see until you use them almost once.

And that's all to have basic coolmenu support.
Looking at the demo application, which implements an MDI Edit application, you will find a way to implement popup coolmenu.

Here follows a small description of the most useful function of the CSpawnMenu class:

bool MeasureItem(..)
To be called from the CWnd OnMeasureItem(..); call the base class if it return false

bool DrawItem(..)
To be called from the CWnd OnDrawItem(..); call the base class if it return false

bool LoadToolBarResource(unsigned int resId)
Load a toolbar resource in the class; an array of command id will be created and mapped to index of an imagelist

bool AddToolBarResource(unsigned int resId)
Add a toolbar resource to the class; works as the LoadToolBarResource

void RemapMenu(CMenu * pMenu)
Makes all the items of the menu OwnerDraw, (eventually) maps them with the appropiate images index based on the item command id; has to be called from the WM_INITMENU and WM_INITPOPUPMENU and before the TrackPopupMenu function

void EnableMenuItems(CMenu * pMenu, CWnd * pWnd)
Use the MFC command enabler mechanism to enable/disable menu items; need the pointer of the menu and the pointer of the CWnd of which the command enablers are to be used; it is designed to be used with popups menu from the right button click in Views (see the CEditView in sample apps for an example)

bool SetFont(LOGFONT * lf)
If someone, for evil pourpose, want to use a different font to draw the menu items, it is possible to call this function (before the menu are displayed, for example in the CMainFrame contructor after the LoadToolBarResource()). Look at the demo, there is commented code.

bool FindKeyboardShortcut(UINT nChar, UINT nFlags, CMenu * pMenu, LRESULT & lRes)
This is needed to handle the shortcuts when the menu is displayed (the &Letter shortcurs); you need to call it in response to WM_MENUCHAR messages of CMainFrame, CChildFrame if MDI app, and CView if using Spawn popups; the usage is a bit complex, look at demo and copy/paste the code.

void SetTextColor(const COLORREF crNormal, const COLORREF crSelected)
You can use this to specify different colors from default for the normal and selected menu items

void SetBackBitmap(const int iRes)
void SetBackBitmap(const int iRes, COLORREF crBackColor)

Now you can use bitmaps as background for menu using one of this 2 function. The first simply set a bitmap, the second change the color you specify to the menu background obtaining a simple transparency. Pay attention to the bitmap you use, which can make the menu text unreadable; you may need to use SetFont to set a bold font to read the text and the SetTextColor to use brighter color if you use strange bitmaps. In the demo, bitmap are enabled in the app menu, and disabled in the popup of the view. To see how to disable it, comment the SetBackBitmap in the CMainFrame constructor of the demo.

bool GetMenuItemText(CString &csText, CMenu * pMenu, const int cmd, bool bByPos)
Use this function to retrieve the text of a menu item; csText will contain the text, cmd is the menu identifier or position, bByPos is true if cmd is the position of menu identifier or false if it is an identifier. If the funtion returns true, csText contain valid text.

bool IsSpawnMenu(CMenu * pMenu, const int iItem, const bool bByPos)
This function is used internally; however, you can use it to see if a menu item as been handled by SpawnMenu or not (not usually means bitmap menu item, like icons of mdi child and maximize/minimize/close buttons of mdi child, which are handled by Evil Windows as menus, and not as buttons as everybody could imagine).

Very thanks to Craig Schmidt for support!

Feel free to use and modify this class with all your software - I will only be glad to know for which apps it will be used and of any improvement you will do to it!

Download demo project 107K

Last updated: 26 May 1998

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
Resource leak - Legacy CodeGuru (09/17/2003)
Problem showing any popup menu under any first level submenu - Legacy CodeGuru (10/01/2002)
AddToolbarResource & CMenuSpawn popup menus - Legacy CodeGuru (09/20/2002)
This Code is Really Cool - Legacy CodeGuru (04/12/2002)
How to enable menu items in MDI app - Legacy CodeGuru (04/05/2002)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES