版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、菜单编程_动态添加1.动态的添加、删除、插入菜单新建一个MFC单文档应用程序,取名Menu2.添加菜单,在CMainFrame:OnCreate(LPCREATESTRUCT lpCreateStruct)中添加:CMenu menu;menu.CreatePopupMenu();/创建一个空的弹出菜单 GetMenu()->AppendMenu(MF_POPUP,(UINT)menu.m_hMenu,"NO_1");/添加一个弹出菜单 menu.Detach();注释:CreatePopupMenuThe CreatePopupMenu function creat
2、es a drop-down menu, submenu, or shortcut menu. The menu is initially empty. You can insert or append menu items by using the InsertMenuItem function. You can also use the InsertMenu function to insert menu items and the AppendMenu function to append menu items.HMENU CreatePopupMenu(VOID);Parameters
3、This function has no parameters. Return ValuesIf the function succeeds, the return value is a handle to the newly created menu.If the function fails, the return value is NULL. To get extended error information, call GetLastError.RemarksThe application can add the new menu to an existing menu, or it
4、can display a shortcut menu by calling the TrackPopupMenuEx or TrackPopupMenu functions. Resources associated with a menu that is assigned to a window are freed automatically. If the menu is not assigned to a window, an application must free system resources associated with the menu before closing.
5、An application frees menu resources by calling the DestroyMenu function. -AppendMenuThe AppendMenu function appends a new item to the end of the specified menu bar, drop-down menu, submenu, or shortcut menu. You can use this function to specify the content, appearance, and behavior of the menu item.
6、 Note The AppendMenu function has been superseded by the InsertMenuItem function. You can still use AppendMenu, however, if you do not need any of the extended features of InsertMenuItem.BOOL AppendMenu( HMENU hMenu, / handle to menu UINT uFlags, / menu-item options UINT_PTR uIDNewItem, /
7、 identifier, menu, or submenu LPCTSTR lpNewItem / menu-item content);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. 2.插入菜单项,在CMainFrame:OnCreate(LPCREATESTRUCT lpCreateStruct)中
8、继续添加:GetMenu()->InsertMenu(2,MF_BYPOSITION|MF_POPUP,(UINT)menu.m_hMenu,"NO_1");/插入菜单menu.AppendMenu(MF_STRING,111,"Hello");/添加菜单项menu.AppendMenu(MF_STRING,112,"Good");menu.AppendMenu(MF_STRING,113,"Nice");menu.Detach();/分离与control bar的连接GetMenu()->GetSub
9、Menu(0)->AppendMenu(MF_STRING,114,"Welcome");/在第1个弹出菜单添加项GetMenu()->GetSubMenu(0)->InsertMenu(ID_FILE_OPEN,MF_BYCOMMAND|MF_STRING,115,"GoodLuck");/插入菜单项注释:InsertMenuThe InsertMenu function inserts a new menu item into a menu, moving other items down the menu. Note &
10、#160;The InsertMenu function has been superseded by the InsertMenuItem function. You can still use InsertMenu, however, if you do not need any of the extended features of InsertMenuItem.BOOL InsertMenu( HMENU hMenu, / handle to menu UINT uPosition, / item that new item precedes UINT uFlags, / option
11、s UINT_PTR uIDNewItem, / identifier, menu, or submenu LPCTSTR lpNewItem / menu item content);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. 删除菜单,在CMainFrame:OnCreate(LPCREATESTR
12、UCT lpCreateStruct)中继续添加:GetMenu()->DeleteMenu(1,MF_BYPOSITION);/删除编辑菜单GetMenu()->GetSubMenu(0)->DeleteMenu(2,MF_BYPOSITION);/删除打开菜单注释:DeleteMenuThe DeleteMenu function deletes an item from the specified menu. If the menu item opens a menu or submenu, this function destroys the handle to th
13、e menu or submenu and frees the memory used by the menu or submenu. BOOL DeleteMenu( HMENU hMenu, / handle to menu UINT uPosition, / menu item identifier or position UINT uFlags / option);Return ValuesIf the function succeeds, the return value is nonzero.If the function fails, the return value is ze
14、ro. To get extended error information, call GetLastError. RemarksThe application must call the DrawMenuBar function whenever a menu changes, whether or not the menu is in a displayed window. 增加菜单项的命令响应,在Resource.h中添加:#define IDM_HELLO111将CMainFrame:OnCreate(LPCREATESTRUCT lpCreateStruct)修改:menu.Appe
15、ndMenu(MF_STRING,IDM_HELLO,"Hello");/添加菜单项在MainFrm.h文件中添加消息原型:protected:/AFX_MSG(CMainFrame)afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);/ NOTE - the ClassWizard will add and remove member functions here./ DO NOT EDIT what you see in these blocks of generated code!/AFX_MSGafx_msg vo
16、id OnHello();DECLARE_MESSAGE_MAP()在MainFrm.cpp文件中添加消息映射:BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)/AFX_MSG_MAP(CMainFrame)/ NOTE - the ClassWizard will add and remove mapping macros here./ DO NOT EDIT what you see in these blocks of generated code !ON_WM_CREATE()/AFX_MSG_MAPON_COMMAND(IDM_HELLO,OnHell
17、o)END_MESSAGE_MAP()并添加一个函数:void CMainFrame:OnHello()MessageBox("Hello!");编写一个电话本程序:在CMenu2View类中添加一个WM_CHAR消息,添加成员变量:public:CStringArray m_strArray;private:CMenu m_menu;int m_nIndex;CString m_strLine;在CMenu2View:CMenu2View()函数中赋初值:m_nIndex=-1;m_strLine=""在CMenu2View中添加WM_CHAR消息响应
18、函数,并写上以下代码:void CMenu2View:OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) / TODO: Add your message handler code here and/or call defaultCClientDC dc(this);if(0x0d=nChar)if(0=+m_nIndex)m_menu.CreateMenu();GetParent()->GetMenu()->AppendMenu(MF_POPUP,(UINT)m_menu.m_hMenu,"PhoneBook");/获取
19、菜单项GetParent()->DrawMenuBar();/重画改变的菜单栏m_menu.AppendMenu(MF_STRING,IDM_PHONE1+m_nIndex,m_strLine.Left(m_strLine.Find(' ');m_strArray.Add(m_strLine);/将增加的一个字符保存到字符数组中m_strLine.Empty();/清空先前的字符Invalidate();/擦除先前窗口的内容,缺省为TRUEelsem_strLine+=nChar;dc.TextOut(0,0,m_strLine);/输出字符串CView:OnChar(n
20、Char, nRepCnt, nFlags);注释:CStringArrayThe CStringArray class supports arrays of CString objects. The member functions of CStringArray are similar to the member functions of class CObArray. Because of this similarity, you can use the CObArray reference documentation for member function specifics. Whe
21、rever you see a CObject pointer as a return value, substitute a CString (not a CString pointer). Wherever you see a CObject pointer as a function parameter, substitute a LPCTSTR.CObject* CObArray:GetAt( int <nIndex> ) const;for example, translates toCString CStringArray:GetAt( int <nIndex&g
22、t; ) const;andvoid SetAt( int <nIndex>, CObject* <newElement> )translates tovoid SetAt( int <nIndex>, LPCTSTR <newElement> )CStringArray incorporates the IMPLEMENT_SERIAL macro to support serialization and dumping of its elements. If an array of CString objects is stored to a
23、n archive, either with an overloaded insertion operator or with the Serialize member function, each element is serialized in turn. Note Before using an array, use SetSize to establish its size and allocate memory for it. If you do not use SetSize, adding elements to your array cause
24、s it to be frequently reallocated and copied. Frequent reallocation and copying are inefficient and can fragment memory.If you need a dump of individual string elements in the array, you must set the depth of the dump context to 1 or greater. When a CString array is deleted, or when its elements are
25、 removed, string memory is freed as appropriate.For more information on using CStringArray, see the article Collections in Visual C+ Programmers Guide.#include <afxcoll.h>-CObArray:Add int Add( CObject* newElement );throw( CMemoryException );Return ValueThe index of the added element.Parameter
26、snewElementThe CObject pointer to be added to this array.RemarksAdds a new element to the end of an array, growing the array by 1. If SetSize has been used with an nGrowBy value greater than 1, then extra memory may be allocated. However, the upper bound will increase by only 1.The following table s
27、hows other member functions that are similar to CObArray:Add.-CWindow:InvalidateBOOL Invalidate( BOOL bErase = TRUE );See InvalidateRect in the Win32 SDK.RemarksInvalidates the entire client area. Passes NULL for the RECT parameter to the InvalidateRect Win32 function.Example/The following example a
28、ttaches an HWND to the CWindow object and /calls CWindow:Invalidate() to invalidate the entire client areaCWindow myWindow;myWindow.Attach(hWndFoo);myWindow.Invalidate();并相继添加五个按钮:protected:/AFX_MSG(CMenu2View)afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);/AFX_MSGafx_msg void OnPhone1()
29、;/消息原型声明afx_msg void OnPhone2();afx_msg void OnPhone3();afx_msg void OnPhone4();afx_msg void OnPhone5();DECLARE_MESSAGE_MAP()-BEGIN_MESSAGE_MAP(CMenu2View, CView)/AFX_MSG_MAP(CMenu2View)ON_WM_CHAR()/AFX_MSG_MAP/ Standard printing commandsON_COMMAND(IDM_PHONE1, OnPhone1)/消息映射ON_COMMAND(IDM_PHONE2, On
30、Phone2)ON_COMMAND(IDM_PHONE3, OnPhone3)ON_COMMAND(IDM_PHONE4, OnPhone4)ON_COMMAND(IDM_PHONE5, OnPhone5)ON_COMMAND(ID_FILE_PRINT, CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView:OnFilePrintPreview)END_MESSAGE_MAP()-void CMenu2View:OnPhone1(
31、) /消息响应函数/ TODO: Add your command handler code hereCClientDC dc(this);dc.TextOut(0,0,m_strArray.GetAt(0);void CMenu2View:OnPhone2()/消息响应函数/ TODO: Add your command handler code hereCClientDC dc(this);dc.TextOut(0,0,m_strArray.GetAt(1);void CMenu2View:OnPhone3()/消息响应函数/ TODO: Add your command handler
32、code hereCClientDC dc(this);dc.TextOut(0,0,m_strArray.GetAt(2);void CMenu2View:OnPhone4()/消息响应函数/ TODO: Add your command handler code hereCClientDC dc(this);dc.TextOut(0,0,m_strArray.GetAt(3);void CMenu2View:OnPhone5()/消息响应函数/ TODO: Add your command handler code hereCClientDC dc(this);dc.TextOut(0,0
33、,m_strArray.GetAt(4);用框架类来捕获消息,在CMainFrame类中,添加一个OnCommand虚函数,并添加:BOOL CMainFrame:OnCommand(WPARAM wParam, LPARAM lParam) / TODO: Add your specialized code here and/or call the base classint MenuCmdId=LOWORD(wParam);/获得wParam的低两个字节CMenu2View *pView=(CMenu2View*)GetActiveView();/得到CMenu2View的一个指针if(M
34、enuCmdId>=IDM_PHONE1 && MenuCmdId<IDM_PHONE1+pView->m_strArray.GetSize()/*获得这个变量的大小*/)CClientDC dc(pView);/用指向View类的指针dc.TextOut(0,0,pView->m_strArray.GetAt(MenuCmdId-IDM_PHONE1);/输出/MessageBox("Test");return TRUE;return CFrameWnd:OnCommand(wParam, lParam);注释:CWnd:OnCom
35、mandThis method is called by the framework when the user selects an item from a menu, when a child control sends a notification message, or when an accelerator keystroke is translated. virtual BOOL OnCommand( WPARAM wParam, LPARAM lParam ); ParameterswParam The low-order word of wParam identifies th
36、e command ID of the menu item, control, or accelerator. The high-order word of wParam specifies the notification message if the message is from a control. If the message is from an accelerator, the high-order word is 1. If the message is from a menu, the high-order word is 0. lParam Identifies the control that sends the message if the message is from a control. Otherwise, lParam is 0. Return ValueAn application returns nonzero if it processes this message; otherwise, it is zero.RemarksOnCommand processes the message map for
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 职业学校泥水工程协议
- 学校建设防尘网施工合同
- 地震学校食堂员工劳动合同
- 农业企业股权登记策略
- 林地征用补偿协议范本
- 合同纠纷调解培训
- 摩托车交易合同模板
- 农药化肥知识产权认证管理办法
- 展厅多媒体使用规范
- 财务资质管理办法
- 浅析新能源电动汽车火灾调查方法
- 养老院健康体检表
- 高中英语选修一(人教版)2-2Learning About Language 教学课件
- 韵母教学讲解课件
- 《马立平中文》教学大纲
- 一年级美术大眼睛-完整版课件
- 浅谈我校啦啦操队存在的问题以及解决措施
- 广东省河源市各县区乡镇行政村村庄村名明细及行政区划代码
- 人教版选修《中国小说欣赏》课件:聊斋志异
- 一例慢阻肺病人护理个案
- 工程量计量计算表模板监理
评论
0/150
提交评论