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 />

with adding other types of resources: we can execute Insert | Resource… command, then select Bitmap<br />

from the popped up dialog box. The newly added resource will be assigned a default ID, it could also be<br />

changed by the programmer.<br />

To load a bitmap resource into the memory, we need to use class CBitmap. This procedure is similar to<br />

loading a menu resource: first we need to use CBitmap to declare a variable, then call function<br />

CBitmap::LoadBitmap(…) to load the resource. For example, if we have a CBitmap type variable bmp, and<br />

our bitmap resource’s ID is IDB_BITMAP, we can load the bitmap as follows:<br />

bmp.LoadBitmap(IDB_BITMAP);<br />

When calling function CMenu::SetMenuItemBitmaps(…), we can pass the pointers of CBitmap type<br />

variables to its parameters.<br />

Sample 2.4\Menu demonstrates bitmap check implementation. It is based on sample 2.3\Menu, which<br />

adds check bitmaps to menu item ID__POPUPITEM1 and ID__POPUPITEM2. Two bitmap resources<br />

IDB_BITMAP_CHECK and IDB_BITMAP_UNCHECK are used to indicate menu item’s checked and unchecked<br />

states respectively. Both bitmaps have a size of 15×15, which is a suitable size for normal menu items. If<br />

we use bigger bitmaps, they might be chopped to fit into the area of menu item.<br />

In the sample, two new CBitmap type variables m_bmpCheck and m_bmpUnCheck are declared in class<br />

CMenuDoc, which are used to load the bitmap resources:<br />

……<br />

class CMenuDoc : public CDocument<br />

{<br />

protected:<br />

CMenu m_menuSub;<br />

CBitmap m_bmpCheck;<br />

CBitmap m_bmpUnCheck;<br />

BOOL m_bSubMenuOn;<br />

}<br />

In the constructor of CMenuDoc, bitmap resources IDB_BITMAP_CHECK and IDB_BITMAP_UNCHECK are<br />

loaded using two variables. Also, after we load the pop up menu resource, function CMenu::<br />

SetMenuItemBitmap(…) is called for both ID__POPUPITEM1 and ID__POPITEM2. We use bitmaps to indicate<br />

both checked and unchecked states. The following code fragment shows how it is implemented:<br />

CMenuDoc::CMenuDoc()<br />

{<br />

CMenu *ptrMenu;<br />

}<br />

m_menuSub.LoadMenu(IDR_MENU_POPUP);<br />

m_bmpCheck.LoadBitmap(IDB_BITMAP_CHECK);<br />

m_bmpUnCheck.LoadBitmap(IDB_BITMAP_UNCHECK);<br />

ptrMenu=m_menuSub.GetSubMenu(0);<br />

ptrMenu->SetMenuItemBitmaps(0, MF_BYPOSITION, &m_bmpUnCheck, &m_bmpCheck);<br />

ptrMenu->SetMenuItemBitmaps(1, MF_BYPOSITION, &m_bmpUnCheck, &m_bmpCheck);<br />

m_bSubMenuOn=FALSE;<br />

When calling function CMenu::SetMenuItemBitmap(…), we use absolute position instead of command<br />

ID to identify a menu item. So the second parameter passed to the function is MF_BYPOSITION.<br />

Besides these, two UPDATE_COMMAND_UI message handlers are also added to CMenuDoc to set item<br />

ID__POPUPITEM1 to checked state and set ID_POPUPITEM2 to unchecked state permenently. The following<br />

two functions show the implementation of the messages handlers:<br />

void CMenuDoc::OnUpdatePopUpItem1(CCmdUI *pCmdUI)<br />

{<br />

if(m_bSubMenuOn)pCmdUI->SetCheck(TRUE);<br />

}<br />

void CMenuDoc::OnUpdatePopUpItem2(CCmdUI *pCmdUI)<br />

{<br />

if(m_bSubMenuOn)pCmdUI->SetCheck(FALSE);<br />

46

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

Saved successfully!

Ooh no, something went wrong!