




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、菜單編程_靜態(tài)添加1.新建一個MFC單文檔應(yīng)用程序,取名Menu在IDM_NAINFRAME中添加一個菜單test ->右鍵-> ClassWizard 分別在CMainFrame、CMenuDoc、CMenuApp、CMenuView中添加對Test菜單項的COMMAND消息。MessageBox("ManinFrame clicked");AfxMessageBox("Doc clicked");AfxMessageBox("App clicked");MessageBox("clicked");命
2、令消息傳遞順序:view類-doc類-frame類-app類2.消息的分類(如下圖)創(chuàng)建標(biāo)記菜單:在新建菜單上創(chuàng)建一個標(biāo)記菜單,在CMainFrame:OnCreate(LPCREATESTRUCT lpCreateStruct)函數(shù)中添加:/GetMenu()->GetSubMenu(0)->CheckMenuItem(0,MF_BYPOSITION|MF_CHECKED);/添加標(biāo)記GetMenu()->GetSubMenu(0)->CheckMenuItem(ID_FILE_NEW,MF_BYCOMMAND|MF_CHECKED);/添加標(biāo)記第二種方法注釋:UIN
3、T CheckMenuItem( UINT nIDCheckItem, UINT nCheck );Return ValueThe previous state of the item: MF_CHECKED or MF_UNCHECKED, or 0xFFFFFFFF if the menu item did not exist.ParametersnIDCheckItemSpecifies the menu item to be checked, as determined by nCheck.nCheckSpecifies how to check the menu item and h
4、ow to determine the items position in the menu. The nCheck parameter can be a combination of MF_CHECKED or MF_UNCHECKED with MF_BYPOSITION or MF_BYCOMMAND flags. These flags can be combined by using the bitwise OR operator. They have the following meanings: · MF_BYCOMMAND Speci
5、fies that the parameter gives the command ID of the existing menu item. This is the default.· MF_BYPOSITION Specifies that the parameter gives the position of the existing menu item. The first item is at position 0.· MF_CHECKED Acts as a toggle with MF_UNC
6、HECKED to place the default check mark next to the item.· MF_UNCHECKED Acts as a toggle with MF_CHECKED to remove a check mark next to the item. RemarksAdds check marks to or removes check marks from menu items in the pop-up menu. The nIDCheckItem parameter specifies the item t
7、o be modified. The nIDCheckItem parameter may identify a pop-up menu item as well as a menu item. No special steps are required to check a pop-up menu item. Top-level menu items cannot be checked. A pop-up menu item must be checked by position since it does not have a menu-item identifier associated
8、 with it.-CMenu:GetSubMenu CMenu* GetSubMenu( int nPos ) const;Return ValueA pointer to a CMenu object whose m_hMenu member contains a handle to the pop-up menu if a pop-up menu exists at the given position; otherwise NULL. If a CMenu object does not exist, then a temporary one is created. The CMenu
9、 pointer returned should not be stored.ParametersnPosSpecifies the position of the pop-up menu contained in the menu. Position values start at 0 for the first menu item. The pop-up menus identifier cannot be used in this function.RemarksRetrieves the CMenu object of a pop-up menu.-CheckMenuItemThe C
10、heckMenuItem function sets the state of the specified menu item's check-mark attribute to either selected or clear. Note The CheckMenuItem function has been superseded by the SetMenuItemInfo function. You can still use CheckMenuItem, however, if you do not need any of the extended fea
11、tures of SetMenuItemInfo.DWORD CheckMenuItem( HMENU hmenu, / handle to menu UINT uIDCheckItem, / menu item to check or uncheck UINT uCheck / menu item options);Parametershmenu in Handle to the menu of interest. uIDCheckItem in Specifies the menu item whose check-mark attribute is to be set, as deter
12、mined by the uCheck parameter. uCheck in Specifies flags that control the interpretation of the uIDCheckItem parameter and the state of the menu item's check-mark attribute. This parameter can be a combination of either MF_BYCOMMAND, or MF_BYPOSITION and MF_CHECKED or MF_UNCHECKED. ValueMeaningM
13、F_BYCOMMANDIndicates that the uIDCheckItem parameter gives the identifier of the menu item. The MF_BYCOMMAND flag is the default, if neither the MF_BYCOMMAND nor MF_BYPOSITION flag is specified.MF_BYPOSITIONIndicates that the uIDCheckItem parameter gives the zero-based relative position of the menu
14、item.MF_CHECKEDSets the check-mark attribute to the selected state.MF_UNCHECKEDSets the check-mark attribute to the clear state. Return ValuesThe return value specifies the previous state of the menu item (either MF_CHECKED or MF_UNCHECKED). If the menu item does not exist, the return value is -1. R
15、emarksAn item in a menu bar cannot have a check mark. The uIDCheckItem parameter identifies a item that opens a submenu or a command item. For a item that opens a submenu, the uIDCheckItem parameter must specify the position of the item. For a command item, the uIDCheckItem parameter can specify eit
16、her the item's position or its identifier. 3.設(shè)置缺省菜單,在CMainFrame:OnCreate(LPCREATESTRUCT lpCreateStruct)函數(shù)中添加:/GetMenu()->GetSubMenu(0)->SetDefaultItem(1,TRUE);/設(shè)置缺省菜單/GetMenu()->GetSubMenu(0)->SetDefaultItem(ID_FILE_OPEN);GetMenu()->GetSubMenu(0)->SetDefaultItem(5,TRUE);注釋:CMen
17、u:SetDefaultItemBOOL SetDefaultItem( UINT uItem, BOOL fByPos = FALSE );Return ValueIf the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, use the Win32 function GetLastError, as described in the Platform SDK.Paramete
18、rsuItemIdentifier or position of the new default menu item or - 1 for no default item. The meaning of this parameter depends on the value of fByPos.fByPosValue specifying the meaning of uItem. If this parameter is FALSE, uItem is a menu item identifier. Otherwise, it is a menu item position.RemarksT
19、his member function implements the behavior of the Win32 function SetMenuDefaultItem, as described in the Platform SDK.4.創(chuàng)建一個圖形標(biāo)記菜單,在CMainFrame中添加成員變量:private:CBitmap m_bitmap;在CMainFrame:OnCreate(LPCREATESTRUCT lpCreateStruct)函數(shù)中添加:CString str;str.Format("x=%d,y=%d",GetSystemMetrics(SM_CX
20、MENUCHECK),GetSystemMetrics(SM_CYMENUCHECK);/獲取菜單圖形標(biāo)記的尺寸MessageBox(str);m_bitmap.LoadBitmap(IDB_BITMAP1);GetMenu()->GetSubMenu(0)->SetMenuItemBitmaps(0,MF_BYPOSITION,&m_bitmap,&m_bitmap);注釋:GetSystemMetricsThe GetSystemMetrics function retrieves various system metrics (widths and heigh
21、ts of display elements) and system configuration settings. All dimensions retrieved by GetSystemMetrics are in pixels. int GetSystemMetrics( int nIndex / system metric or configuration setting);ParametersnIndex in Specifies the system metric or configuration setting to retrieve. All SM_CX* values ar
22、e widths. All SM_CY* values are heights. SM_CXMENUCHECK,SM_CYMENUCHECKDimensions, in pixels, of the default menu check-mark bitmap.-SetMenuItemBitmapsThe SetMenuItemBitmaps function associates the specified bitmap with a menu item. Whether the menu item is selected or clear, the system displays the
23、appropriate bitmap next to the menu item. BOOL SetMenuItemBitmaps( HMENU hMenu, / handle to menu UINT uPosition, / menu item UINT uFlags, / options HBITMAP hBitmapUnchecked, / handle to unchecked bitmap HBITMAP hBitmapChecked / handle to checked bitmap);ParametershMenu in Handle to the menu containi
24、ng the item to receive new check-mark bitmaps. uPosition in Specifies the menu item to be changed, as determined by the uFlags parameter. uFlags in Specifies how the uPosition parameter is interpreted. The uFlags parameter must be one of the following values. ValueMeaningMF_BYCOMMANDIndicates that u
25、Position gives the identifier of the menu item. If neither MF_BYCOMMAND nor MF_BYPOSITION is specified, MF_BYCOMMAND is the default flag.MF_BYPOSITIONIndicates that uPosition gives the zero-based relative position of the menu item.hBitmapUnchecked in Handle to the bitmap displayed when the menu item
26、 is not selected. hBitmapChecked in Handle to the bitmap displayed when the menu item is selected. Return ValuesIf the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, call GetLastError. RemarksIf either the hBitmapUnc
27、hecked or hBitmapChecked parameter is NULL, the system displays nothing next to the menu item for the corresponding check state. If both parameters are NULL, the system displays the default check-mark bitmap when the item is selected, and removes the bitmap when the item is not selected. When the me
28、nu is destroyed, these bitmaps are not destroyed; it is up to the application to destroy them. The selected and clear bitmaps should be monochrome. The system uses the Boolean AND operator to combine bitmaps with the menu so that the white part becomes transparent and the black part becomes the menu
29、-item color. If you use color bitmaps, the results may be undesirable.Use the GetSystemMetrics function with the CXMENUCHECK and CYMENUCHECK values to retrieve the bitmap dimensions.5.使菜單不可用,變灰在CMainFrame:CMainFrame()函數(shù)中添加:m_bAutoMenuEnable=FALSE;在CMainFrame:OnCreate(LPCREATESTRUCT lpCreateStruct)函數(shù)
30、中添加:GetMenu()->GetSubMenu(0)->EnableMenuItem(1,MF_BYPOSITION|MF_DISABLED|MF_GRAYED);注釋:CMenu:EnableMenuItem UINT EnableMenuItem( UINT nIDEnableItem, UINT nEnable );Return ValuePrevious state (MF_DISABLED, MF_ENABLED, or MF_GRAYED) or 1 if not valid.ParametersnIDEnableItemSpecifies the menu ite
31、m to be enabled, as determined by nEnable. This parameter can specify pop-up menu items as well as standard menu items.nEnableSpecifies the action to take. It can be a combination of MF_DISABLED, MF_ENABLED, or MF_GRAYED, with MF_BYCOMMAND or MF_BYPOSITION. These values can be combined by using the
32、bitwise OR operator. These values have the following meanings: · MF_BYCOMMAND Specifies that the parameter gives the command ID of the existing menu item. This is the default.· MF_BYPOSITION Specifies that the parameter gives the position of the existing m
33、enu item. The first item is at position 0.· MF_DISABLED Disables the menu item so that it cannot be selected but does not dim it.· MF_ENABLED Enables the menu item so that it can be selected and restores it from its dimmed state.· MF_GRAYED
34、; Disables the menu item so that it cannot be selected and dims it. RemarksEnables, disables, or dims a menu item. The CreateMenu, InsertMenu, ModifyMenu, and LoadMenuIndirect member functions can also set the state (enabled, disabled, or dimmed) of a menu item.Using the MF_BYPOSITION value req
35、uires an application to use the correct CMenu. If the CMenu of the menu bar is used, a top-level menu item (an item in the menu bar) is affected. To set the state of an item in a pop-up or nested pop-up menu by position, an application must specify the CMenu of the pop-up menu. When an application s
36、pecifies the MF_BYCOMMAND flag, Windows checks all pop-up menu items that are subordinate to the CMenu; therefore, unless duplicate menu items are present, using the CMenu of the menu bar is sufficient.Example/ The code fragment below shows how to disable (and gray out) the / FileNew menu item./ NOT
37、E: m_bAutoMenuEnable is set to FALSE in the constructor of / CMainFrame so no ON_UPDATE_COMMAND_UI or ON_COMMAND handlers are / needed, and CMenu:EnableMenuItem() will work as expected.CMenu* mmenu = GetMenu();CMenu* submenu = mmenu->GetSubMenu(0);submenu->EnableMenuItem(ID_FILE_NEW, MF_BYCOMM
38、AND | MF_DISABLED | MF_GRAYED);-6.如何取消整個菜單,在CMainFrame:OnCreate(LPCREATESTRUCT lpCreateStruct)函數(shù)中添加:SetMenu(NULL);/取消菜單CMenu menu;/在這是局部變量,一般會有問題,應(yīng)設(shè)置成CMainFrame的成員變量menu.LoadMenu(IDR_MAINFRAME);/加載菜單SetMenu(&menu);menu.Detach();注釋:SetMenuThe SetMenu function assigns a new menu to the specified w
39、indow. BOOL SetMenu( HWND hWnd, / handle to window HMENU hMenu / handle to menu);ParametershWnd in Handle to the window to which the menu is to be assigned. hMenu in Handle to the new menu. If this parameter is NULL, the window's current menu is removed. Return ValuesIf the function succeeds, th
40、e return value is nonzero.If the function fails, the return value is zero. To get extended error information, call GetLastError. RemarksThe window is redrawn to reflect the menu change. A menu can be assigned to any window that is not a child window.The SetMenu function replaces the previous menu, i
41、f any, but it does not destroy it. An application should call the DestroyMenu function to accomplish this task. -The CMenu class is an encapsulation of the Windows HMENU. It provides member functions for creating, tracking, updating, and destroying a menu.Create a CMenu object on the stack frame as
42、a local, then call CMenus member functions to manipulate the new menu as needed. Next, call CWnd:SetMenu to set the menu to a window, followed immediately by a call to the CMenu objects Detach member function. The CWnd:SetMenu member function sets the windows menu to the new menu, causes the window
43、to be redrawn to reflect the menu change, and also passes ownership of the menu to the window. The call to Detach detaches the HMENU from the CMenu object, so that when the local CMenu variable passes out of scope, the CMenu object destructor does not attempt to destroy a menu it no longer owns. The
44、 menu itself is automatically destroyed when the window is destroyed.7.命令更新,在View-> ClassWizard 中添加對ID_EDIT_CUT的UPDATE_COMMAND_UI消息,然后在CMainFrame:OnUpdateEditCut(CCmdUI* pCmdUI)函數(shù)中添加:pCmdUI->Enable();/默認為TRUE這樣剪切菜單就變成了可有狀態(tài)。注釋:CCmdUICCmdUI does not have a base class.The CCmdUI class is used onl
45、y within an ON_UPDATE_COMMAND_UI handler in a CCmdTarget-derived class.When a user of your application pulls down a menu, each menu item needs to know whether it should be displayed as enabled or disabled. The target of a menu command provides this information by implementing an ON_UPDATE_COMMAND_UI
46、 handler. Use ClassWizard to browse the command user-interface objects in your application and create a message-map entry and function prototype for each handler.When the menu is pulled down, the framework searches for and calls each ON_UPDATE_COMMAND_UI handler, each handler calls CCmdUI member fun
47、ctions such as Enable and Check, and the framework then appropriately displays each menu item.A menu item can be replaced with a control-bar button or other command user-interface object without changing the code within the ON_UPDATE_COMMAND_UI handler. The following table summarizes the effect CCmd
48、UIs member functions have on various command user-interface items.-CCmdUI Class MembersData Membersm_nIDThe ID of the user-interface object.m_nIndexThe index of the user-interface object. m_pMenuPoints to the menu represented by the CCmdUI object.m_pSubMenuPoints to the contained sub-menu represente
49、d by the CCmdUI object. m_pOtherPoints to the window object that sent the notification.OperationsEnableEnables or disables the user-interface item for this command.SetCheckSets the check state of the user-interface item for this command.SetRadioLike the SetCheck member function, but operates on radi
50、o groups.SetTextSets the text for the user-interface item for this command.ContinueRouting Tells the command-routing mechanism to continue routing the current message down the chain of handlers.如果我們需要將工具欄上的某個圖標(biāo)與某個菜單項相關(guān)聯(lián),我們只需要它們的ID號設(shè)置成同一個就可以了。再做一個使新建菜單項不能使用狀態(tài)的操作,同樣對ID_FILE_NEW添加UPDATE_COMMAND_UI消息,然后
51、在CMainFrame:OnUpdateEditCut(CCmdUI* pCmdUI)函數(shù)中添加:if(ID_FILE_NEW=PcmdUI->m_nID)/m_nID是CCmdUI的成員變量,保存了與這個UI當(dāng)前相關(guān)的菜單項pCmdUI->Enable(FALSE);當(dāng)然也可以通過索引實現(xiàn),if(0=pCmdUI->m_nIndex)pCmdUI->Enable(FALSE);注意:如果對前面的菜單的狀態(tài)也用索引來判斷,那工具欄上的剪切圖標(biāo)將不會變成可用狀態(tài);而用ID號來判斷,則工具欄上的剪切則會變成可用狀態(tài)。8.添加右鍵彈出菜單功能,首先選擇Project ->
52、;Add To Project -> Components and Controls 在彈出的對話框中選擇visual C+ Components -> Pop up Menu 選擇插入,”Insert the Pop-up Menu Component?” 選擇確定,增加彈出菜單到CMenuView類當(dāng)中,ID暫不修改,OK!運行點右鍵可以看到,增加了彈出菜單!在資源中打開CG_IDR_POPUP_MENU_VIEW,可以看到增加的三個菜單項,下面手動創(chuàng)建一個彈出菜單:新建一個菜單,添加兩個菜單項 IDM_SHOW 、 IDM_EXIT ,并在視類中添加WM_RBUTTONDOW
53、N消息響應(yīng)函數(shù),并在CMenuView:OnRButtonDown(UINT nFlags, CPoint point)函數(shù)中添加:CMenu menu;menu.LoadMenu(IDR_MENU1);CMenu *pPopup=menu.GetSubMenu(0);ClientToScreen(&point);/把給定的點所在的客戶區(qū)坐標(biāo)轉(zhuǎn)換成屏幕坐標(biāo)pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this);/顯示彈出菜單注釋:TrackPopupMenuThe TrackPopupM
54、enu function displays a shortcut menu at the specified location and tracks the selection of items on the menu. The shortcut menu can appear anywhere on the screen.To specify an area of the screen the menu should not overlap, use the TrackPopupMenuEx function. BOOL TrackPopupMenu( HMENU hMenu, / hand
55、le to shortcut menu UINT uFlags, / options int x, / horizontal position int y, / vertical position int nReserved, / reserved, must be zero HWND hWnd, / handle to owner window CONST RECT *prcRect / ignored);Return ValuesIf you specify TPM_RETURNCMD in the uFlags parameter, the return value is the men
56、u-item identifier of the item that the user selected. If the user cancels the menu without making a selection, or if an error occurs, then the return value is zero.If you do not specify TPM_RETURNCMD in the uFlags parameter, the return value is nonzero if the function succeeds and zero if it fails. To g
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 壓緊氣缸采購合同范本
- 縣勞務(wù)輸出合同范本
- 化肥賒欠合同范例
- 辦公電腦訂購合同范本
- 出國出境勞務(wù)合同范本
- 北京土方備案合同范本
- 廠房水電安裝合同范本
- 副食進貨合同范本
- 合同范本模板收費
- 南園新村租房合同范本
- 《原來數(shù)學(xué)這么有趣》小學(xué)數(shù)學(xué)啟蒙課程
- 中醫(yī)內(nèi)科臨床診療指南-塵肺病
- DZ∕T 0399-2022 礦山資源儲量管理規(guī)范(正式版)
- 2024年鄂爾多斯市國資產(chǎn)投資控股集團限公司招聘公開引進高層次人才和急需緊缺人才筆試參考題庫(共500題)答案詳解版
- 競賽試卷(試題)-2023-2024學(xué)年六年級下冊數(shù)學(xué)人教版
- 《研學(xué)旅行課程設(shè)計》課件-辨識與研學(xué)旅行場混淆的概念
- 創(chuàng)維電視55寸說明書
- 部編版道德與法治三年級下冊教案全冊
- 山西眾輝電力服務(wù)公司歷年真題
- 修建蓄水池施工合同協(xié)議書范本
- CJJ-181-2012(精華部分)城鎮(zhèn)排水管道檢測與評估技術(shù)規(guī)程
評論
0/150
提交評論