菜单编程静态添加.docx
- 文档编号:8395111
- 上传时间:2023-01-31
- 格式:DOCX
- 页数:16
- 大小:281.09KB
菜单编程静态添加.docx
《菜单编程静态添加.docx》由会员分享,可在线阅读,更多相关《菜单编程静态添加.docx(16页珍藏版)》请在冰豆网上搜索。
菜单编程静态添加
菜单编程_静态添加
1.
新建一个MFC单文档应用程序,取名Menu
在IDM_NAINFRAME中添加一个菜单test->右键->ClassWizard…分别在CMainFrame、CMenuDoc、CMenuApp、CMenuView中添加对Test菜单项的COMMAND消息。
MessageBox("ManinFrameclicked");
AfxMessageBox("Docclicked");
AfxMessageBox("Appclicked");
MessageBox("clicked");
命令消息传递顺序:
view类-doc类-frame类-app类
2.
消息的分类(如下图)
创建标记菜单:
在新建菜单上创建一个标记菜单,在CMainFrame:
:
OnCreate(LPCREATESTRUCTlpCreateStruct)函数中添加:
//GetMenu()->GetSubMenu(0)->CheckMenuItem(0,MF_BYPOSITION|MF_CHECKED);//添加标记
GetMenu()->GetSubMenu(0)->CheckMenuItem(ID_FILE_NEW,MF_BYCOMMAND|MF_CHECKED);//添加标记第二种方法
注释:
UINTCheckMenuItem(UINTnIDCheckItem,UINTnCheck);
ReturnValue
Thepreviousstateoftheitem:
MF_CHECKEDorMF_UNCHECKED,or0xFFFFFFFFifthemenuitemdidnotexist.
Parameters
nIDCheckItem
Specifiesthemenuitemtobechecked,asdeterminedbynCheck.
nCheck
Specifieshowtocheckthemenuitemandhowtodeterminetheitem’spositioninthemenu.ThenCheckparametercanbeacombinationofMF_CHECKEDorMF_UNCHECKEDwithMF_BYPOSITIONorMF_BYCOMMANDflags.TheseflagscanbecombinedbyusingthebitwiseORoperator.Theyhavethefollowingmeanings:
∙MF_BYCOMMAND SpecifiesthattheparametergivesthecommandIDoftheexistingmenuitem.Thisisthedefault.
∙MF_BYPOSITION Specifiesthattheparametergivesthepositionoftheexistingmenuitem.Thefirstitemisatposition0.
∙MF_CHECKED ActsasatogglewithMF_UNCHECKEDtoplacethedefaultcheckmarknexttotheitem.
∙MF_UNCHECKED ActsasatogglewithMF_CHECKEDtoremoveacheckmarknexttotheitem.
Remarks
Addscheckmarkstoorremovescheckmarksfrommenuitemsinthepop-upmenu.ThenIDCheckItemparameterspecifiestheitemtobemodified.
ThenIDCheckItemparametermayidentifyapop-upmenuitemaswellasamenuitem.Nospecialstepsarerequiredtocheckapop-upmenuitem.Top-levelmenuitemscannotbechecked.Apop-upmenuitemmustbecheckedbypositionsinceitdoesnothaveamenu-itemidentifierassociatedwithit.
-------------------------------------------------------------------------------
CMenu:
:
GetSubMenu
CMenu*GetSubMenu(intnPos)const;
ReturnValue
ApointertoaCMenuobjectwhosem_hMenumembercontainsahandletothepop-upmenuifapop-upmenuexistsatthegivenposition;otherwiseNULL.IfaCMenuobjectdoesnotexist,thenatemporaryoneiscreated.TheCMenupointerreturnedshouldnotbestored.
Parameters
nPos
Specifiesthepositionofthepop-upmenucontainedinthemenu.Positionvaluesstartat0forthefirstmenuitem.Thepop-upmenu’sidentifiercannotbeusedinthisfunction.
Remarks
RetrievestheCMenuobjectofapop-upmenu.
----------------------------------------------------------------------------------
CheckMenuItem
TheCheckMenuItemfunctionsetsthestateofthespecifiedmenuitem'scheck-markattributetoeitherselectedorclear.
Note TheCheckMenuItemfunctionhasbeensupersededbytheSetMenuItemInfofunction.YoucanstilluseCheckMenuItem,however,ifyoudonotneedanyoftheextendedfeaturesofSetMenuItemInfo.
DWORDCheckMenuItem(
HMENUhmenu,//handletomenu
UINTuIDCheckItem,//menuitemtocheckoruncheck
UINTuCheck//menuitemoptions
);
Parameters
hmenu
[in]Handletothemenuofinterest.
uIDCheckItem
[in]Specifiesthemenuitemwhosecheck-markattributeistobeset,asdeterminedbytheuCheckparameter.
uCheck
[in]SpecifiesflagsthatcontroltheinterpretationoftheuIDCheckItemparameterandthestateofthemenuitem'scheck-markattribute.ThisparametercanbeacombinationofeitherMF_BYCOMMAND,orMF_BYPOSITIONandMF_CHECKEDorMF_UNCHECKED.
Value
Meaning
MF_BYCOMMAND
IndicatesthattheuIDCheckItemparametergivestheidentifierofthemenuitem.TheMF_BYCOMMANDflagisthedefault,ifneithertheMF_BYCOMMANDnorMF_BYPOSITIONflagisspecified.
MF_BYPOSITION
IndicatesthattheuIDCheckItemparametergivesthezero-basedrelativepositionofthemenuitem.
MF_CHECKED
Setsthecheck-markattributetotheselectedstate.
MF_UNCHECKED
Setsthecheck-markattributetotheclearstate.
ReturnValues
Thereturnvaluespecifiesthepreviousstateofthemenuitem(eitherMF_CHECKEDorMF_UNCHECKED).Ifthemenuitemdoesnotexist,thereturnvalueis-1.
Remarks
Aniteminamenubarcannothaveacheckmark.
TheuIDCheckItemparameteridentifiesaitemthatopensasubmenuoracommanditem.Foraitemthatopensasubmenu,theuIDCheckItemparametermustspecifythepositionoftheitem.Foracommanditem,theuIDCheckItemparametercanspecifyeithertheitem'spositionoritsidentifier.
3.
设置缺省菜单,在CMainFrame:
:
OnCreate(LPCREATESTRUCTlpCreateStruct)函数中添加:
//GetMenu()->GetSubMenu(0)->SetDefaultItem(1,TRUE);//设置缺省菜单
//GetMenu()->GetSubMenu(0)->SetDefaultItem(ID_FILE_OPEN);
GetMenu()->GetSubMenu(0)->SetDefaultItem(5,TRUE);
注释:
CMenu:
:
SetDefaultItem
BOOLSetDefaultItem(UINTuItem,BOOLfByPos=FALSE);
ReturnValue
Ifthefunctionsucceeds,thereturnvalueisnonzero.Ifthefunctionfails,thereturnvalueiszero.Togetextendederrorinformation,usetheWin32functionGetLastError,asdescribedinthePlatformSDK.
Parameters
uItem
Identifierorpositionofthenewdefaultmenuitemor-1fornodefaultitem.ThemeaningofthisparameterdependsonthevalueoffByPos.
fByPos
ValuespecifyingthemeaningofuItem.IfthisparameterisFALSE,uItemisamenuitemidentifier.Otherwise,itisamenuitemposition.
Remarks
ThismemberfunctionimplementsthebehavioroftheWin32functionSetMenuDefaultItem,asdescribedinthePlatformSDK.
4.
创建一个图形标记菜单,在CMainFrame中添加成员变量:
private:
CBitmapm_bitmap;
在CMainFrame:
:
OnCreate(LPCREATESTRUCTlpCreateStruct)函数中添加:
CStringstr;
str.Format("x=%d,y=%d",GetSystemMetrics(SM_CXMENUCHECK),
GetSystemMetrics(SM_CYMENUCHECK));//获取菜单图形标记的尺寸
MessageBox(str);
m_bitmap.LoadBitmap(IDB_BITMAP1);
GetMenu()->GetSubMenu(0)->SetMenuItemBitmaps(0,MF_BYPOSITION,&m_bitmap,&m_bitmap);
注释:
GetSystemMetrics
TheGetSystemMetricsfunctionretrievesvarioussystemmetrics(widthsandheightsofdisplayelements)andsystemconfigurationsettings.AlldimensionsretrievedbyGetSystemMetricsareinpixels.
intGetSystemMetrics(
intnIndex//systemmetricorconfigurationsetting
);
Parameters
nIndex
[in]Specifiesthesystemmetricorconfigurationsettingtoretrieve.AllSM_CX*valuesarewidths.AllSM_CY*valuesareheights.
SM_CXMENUCHECK,
SM_CYMENUCHECK
Dimensions,inpixels,ofthedefaultmenucheck-markbitmap.
-----------------------------------------------------------------------------------
SetMenuItemBitmaps
TheSetMenuItemBitmapsfunctionassociatesthespecifiedbitmapwithamenuitem.Whetherthemenuitemisselectedorclear,thesystemdisplaystheappropriatebitmapnexttothemenuitem.
BOOLSetMenuItemBitmaps(
HMENUhMenu,//handletomenu
UINTuPosition,//menuitem
UINTuFlags,//options
HBITMAPhBitmapUnchecked,//handletouncheckedbitmap
HBITMAPhBitmapChecked//handletocheckedbitmap
);
Parameters
hMenu
[in]Handletothemenucontainingtheitemtoreceivenewcheck-markbitmaps.
uPosition
[in]Specifiesthemenuitemtobechanged,asdeterminedbytheuFlagsparameter.
uFlags
[in]SpecifieshowtheuPositionparameterisinterpreted.TheuFlagsparametermustbeoneofthefollowingvalues.
Value
Meaning
MF_BYCOMMAND
IndicatesthatuPositiongivestheidentifierofthemenuitem.IfneitherMF_BYCOMMANDnorMF_BYPOSITIONisspecified,MF_BYCOMMANDisthedefaultflag.
MF_BYPOSITION
IndicatesthatuPositiongivesthezero-basedrelativepositionofthemenuitem.
hBitmapUnchecked
[in]Handletothebitmapdisplayedwhenthemenuitemisnotselected.
hBitmapChecked
[in]Handletothebitmapdisplayedwhenthemenuitemisselected.
ReturnValues
Ifthefunctionsucceeds,thereturnvalueisnonzero.
Ifthefunctionfails,thereturnvalueiszero.Togetextendederrorinformation,callGetLastError.
Remarks
IfeitherthehBitmapUncheckedorhBitmapCheckedparameterisNULL,thesystemdisplaysnothingnexttothemenuitemforthecorrespondingcheckstate.IfbothparametersareNULL,thesystemdisplaysthedefaultcheck-markbitmapwhentheitemisselected,andremovesthebitmapwhentheitemisnotselected.
Whenthemenuisdestroyed,thesebitmapsarenotdestroyed;itisuptotheapplicationtodestroythem.
Theselectedandclearbitmapsshouldbemonochrome.ThesystemusestheBooleanANDoperatortocombinebitmapswiththemenusothatthewhitepartbecomestransparentandtheblackpartbecomesthemenu-itemcolor.Ifyouusecolorbitmaps,theresultsmaybeundesirable.
UsetheGetSystemMetricsfunctionwiththeCXMENUCHECKandCYMENUCHECKvaluestoretrievethebitmapdimensions.
5.
使菜单不可用,变灰
在CMainFrame:
:
CMainFrame()函数中添加:
m_bAutoMenuEnable=FALSE;
在CMainFrame:
:
OnCreate(LPCREATESTRUCTlpCreateStruct)函数中添加:
GetMenu()->GetSubMenu(0)->EnableMenuItem(1,MF_BYPOSITION|MF_DISABLED|MF_GRAYED);
注释:
CMenu:
:
EnableMenuItem
UINTEnableMenuItem(UINTnIDEnableItem,UINTnEnable);
ReturnValue
Previousstate(MF_DISABLED,MF_ENABLED,orMF_GRAYED)or–1ifnotvalid.
Parameters
nIDEnableItem
Specifiesthemenuitemtobeenabled,asdeterminedbynEnable.Thisparametercanspecifypop-upmenuitemsaswellasstandardmenuitems.
nEnable
Specifiestheactiontotake.ItcanbeacombinationofMF_DISABLED,MF_ENABLED,orMF_GRAYED,withMF_BYCOMMANDorMF_BYPOSITION.ThesevaluescanbecombinedbyusingthebitwiseORoperator.Thesevalueshavethefollowingmeanings:
∙MF_BYCOMMAND SpecifiesthattheparametergivesthecommandIDoftheexistingmenuitem.Thisisthedefault.
∙MF_BYPOSITION Specifiesthattheparametergivesthepositionoftheexistingmen
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 菜单 编程 静态 添加