菜单编程静态添加.docx

上传人:b****5 文档编号:8395111 上传时间:2023-01-31 格式:DOCX 页数:16 大小:281.09KB
下载 相关 举报
菜单编程静态添加.docx_第1页
第1页 / 共16页
菜单编程静态添加.docx_第2页
第2页 / 共16页
菜单编程静态添加.docx_第3页
第3页 / 共16页
菜单编程静态添加.docx_第4页
第4页 / 共16页
菜单编程静态添加.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

菜单编程静态添加.docx

《菜单编程静态添加.docx》由会员分享,可在线阅读,更多相关《菜单编程静态添加.docx(16页珍藏版)》请在冰豆网上搜索。

菜单编程静态添加.docx

菜单编程静态添加

菜单编程_静态添加

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1