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

void CMenuDoc::OnEditInsertDynamicMenu()<br />

{<br />

CMenu *pTopMenu=AfxGetMainWnd()->GetMenu();<br />

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

pTopMenu->InsertMenu<br />

(<br />

1, MF_BYPOSITION | MF_POPUP, (UINT)ptrMenu->GetSafeHmenu(), "&Dynamic Menu"<br />

);<br />

AfxGetMainWnd()->DrawMenuBar();<br />

m_bSubMenuOn=TRUE;<br />

}<br />

void CMenuDoc::OnEditDeleteDynamicMenu()<br />

{<br />

CMenu *pTopMenu=AfxGetMainWnd()->GetMenu();<br />

pTopMenu->RemoveMenu(1, MF_BYPOSITION);<br />

AfxGetMainWnd()->DrawMenuBar();<br />

m_bSubMenuOn=FALSE;<br />

}<br />

When inserting sub-menu, flag MF_BYPOSITION is used. This is because the first level menu items do<br />

not have command IDs.<br />

After the menu is inserted or removed, we must call function CWnd::DrawMenuBar() to let the menu be<br />

updated. Otherwise although the content of the menu is actually changed, it will not be reflected to the user<br />

interface until the update is triggered by some other reasons.<br />

2.4 Bitmap Check<br />

The default menu check provided by <strong>MFC</strong> is a tick mark, and nothing is displayed when the check is<br />

removed. With a little effort, we can prepare our own bitmaps and use them to implement the checked and<br />

unchecked state (Figure 2-3).<br />

To implement the checked and unchecked states of menu items using bitmaps, we need to call the<br />

following member function of CMenu:<br />

BOOL CMenu::SetMenuItemBitmaps<br />

(<br />

UINT nPosition, UINT nFlags, const CBitmap* pBmpUnchecked,<br />

const CBitmap* pBmpChecked<br />

);<br />

The first two parameters of this function indicate which menu item we are working with. Their<br />

meanings are the same with that of functions such as CMenu::EnableMenuItem(…). When calling this<br />

function, we can use either a command ID or an absolute position to identify a menu item. The third and<br />

fourth parameters are pointers to bitmaps (CBitmap type objects), one for checked state, one for unchecked<br />

state.<br />

Bitmap checks<br />

Standard checks<br />

Figure 2-3. Standard menu checks and bitmap menu checks<br />

Like menu, bitmap can also be prepared as resource then be loaded at program’s runtime. We can edit<br />

a bitmap in Developer Studio, and save it as application’s resource. Adding a bitmap resource is the same<br />

45

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

Saved successfully!

Ooh no, something went wrong!