11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 2. Menu<br />

BOOL CMenu::ModifyMenu<br />

(<br />

UINT nPosition, UINT nFlags, UINT nIDNewItem=0, LPCTSTR lpszNewItem = NULL<br />

);<br />

BOOL CMenu::ModifyMenu<br />

(<br />

UINT nPosition, UINT nFlags, UINT nIDNewItem, const CBitmap* pBmp<br />

);<br />

The first version of this function allows us to change a menu item to a text item, a separator, or a submenu.<br />

The second version allows us to change a menu item to a bitmap item. For the second version,<br />

parameter nIDNewItem specifies the new command ID, and parameter pBmp is a pointer to a CBitmap object,<br />

which must contain a valid bitmap resource.<br />

Menu Modification<br />

In the sample application, a bitmap resource ID_BITMAP_QUESTION is prepared for implementing<br />

bitmap menu item. This bitmap contains a question mark. There is no restriction on the bitmap size,<br />

because the size of menu item will be adjusted automatically to let the image fit in.<br />

To load the image, a new CBitmap type variable m_bmpQuestion is declared in class CMainFrame, and<br />

bitmap resource ID_BITMAP_QUESTION is loaded in the constructor of class CMainFrame:<br />

……<br />

……<br />

class CMainFrame : public CFrameWnd<br />

{<br />

protected:<br />

CStatusBar m_wndStatusBar;<br />

CToolBar m_wndToolBar;<br />

CBitmap m_bmpQuestion;<br />

};<br />

CMainFrame::CMainFrame()<br />

{<br />

m_bmpQuestion.LoadBitmap(IDB_BITMAP_QUESTION);<br />

}<br />

The pointer to the system menu is obtained in function CMainFrame::OnCreate(…). First, system<br />

menu’s fifth item (a separator) is modified to a bitmap menu item, then another new command “Resume<br />

standard system menu” is inserted before this bitmap menu item. The IDs of the two commands are<br />

ID_QUESTION and ID_RESUME respectively.<br />

Although we can use any numerical values as the command IDs of the newly added menu items, they<br />

should not be used by other resources of the application. The best way to prevent this from happening is to<br />

generate two new string resources in the application, and use ID_QUESTION and ID_RESUME as their<br />

symbolic IDs. Because Developer Studio will always allocate unused values for new resources, we can<br />

avoid sharing IDs with other resources by using this method.<br />

The following shows how we access the system menu and make changes to its items:<br />

……<br />

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)<br />

{<br />

CMenu *ptrMenu;<br />

ptrMenu=GetSystemMenu(FALSE);<br />

ptrMenu->ModifyMenu(5, MF_BYPOSITION, ID_QUESTION, &m_bmpQuestion);<br />

ptrMenu->InsertMenu(5, MF_BYPOSITION, ID_RESUME, "Resume standard system menu");<br />

}<br />

return 0;<br />

48

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!