孙鑫VC学习笔记:第六讲

六、菜单

2006年8月3日

第六讲三个多小时,花了我两天时间才看完,然后用OneNote花花绿绿地做了好多笔记,可以说是图文并茂,但是这里显示不出来,所以做了一张抓图,图片地址:http://img565.photo.163.com/goodmoood/61588745/1745167041.gif

 

<<<

 

孙鑫VC++讲座笔记-(6)菜单编程

 

 

 

1,弹出菜单(Pop-up)是不能用来作命令响应的。因而,也不能编辑ID号。去掉Pop-up的复选框后,就可以给定ID号了,也可以做命令响应了。比如我们增加一个ID号为IDM_TEST的菜单项,那么我们可以用ClassWizard来对这个菜单项添加消息响应。但是这个消息响应该添加在View Doc CMainFrame App类中那个类上呢?还是每个类里面都可以同时添加消息响应函数?答案是可以同时在上述四个类中同时添加消息响应函数。

 

 2,MFC中菜单项消息如果利用ClassWizard来对菜单项消息分别在上述四个类中进行响应,则菜单消息传递顺序:View--Doc类--CMainFrame类--App。菜单消息一旦在其中一个类中响应则不再在其它类中查找响应函数。

具体:

当点击一个菜单项的时候,最先接受到菜单项消息的是CMainFrame框架类,CMainFrame框架类将会把菜单项消息交给它的子窗口View类,由View类首先进行处理;如果View类检测到没对该菜单项消息做响应,则View类把菜单项消息交由文档类Doc类进行处理;如果Doc类检测到Doc类中也没对该菜单项消息做响应,则Doc类又把该菜单项消息交还给View类,由View类再交还给CMainFrame类处理。如果CMainFrame类查看到CMainFrame类中也没对该消息做响应,则最终交给App类进行处理。

 

 3,消息的分类:标准消息,命令消息,通告消息。

[标准消息]:除WM_COMMAND之外,所有以WM_开头的消息。

[命令消息]:来自菜单、加速键或工具栏按钮的消息。这类消息都以WM_COMMAND呈现。

在MFC中,通过菜单项的标识(ID)来区分不同的命令消息;

在SDK中,通过消息的wParam参数识别。

[通告消息]:由控件产生的消息,例如,按钮的单击,列表框的选择等均产生此类消息,为的是向其父窗口(通常是对话框)通知事件的发生。这类消息也是以WM_COMMAND形式呈现。

说明:

1)从CWnd派生的类,都可以接收到[标准消息],当然也可以接收[命令消息]、[通告消息]。

2)从CCmdTarget派生的类,都可以接收到[命令消息]和[通告消息],不能接收[标准消息]。

 

4,一个菜单拦可以有若干个子菜单,一个子菜单又可以有若干个菜单项等。对菜单栏的子菜单由左至右建立从0开始的索引。对特定子菜单的菜单项由上至下建立了从0开始的索引,访问子菜单和菜单项均可以通过其索引或标识(如果有标识的话)进行。子菜单只能通过索引访问,因为没有标识。

相关重要函数:

 

CMenu* GetMenu( ) ;//CWnd::GetMenu得到窗口菜单栏对象指针。

注意:在View类中调用GetMenu()一般得不到菜单指针,因为View类中往往没有菜单。框架类中有。

 

CMenu* GetSubMenu( ) ;//CMenu::GetSubMenu获得指向弹出菜单对象指针

 

UINT CheckMenuItem(UINT nIDCheckItem/*取得菜单项的ID*/, UINT nCheck/**/);

//CMenu::CheckMenuItem Adds check marks to or removes check marks from menu items in the pop-up menu.

 

BOOL SetDefaultItem(UINT uItem, BOOL fByPos = FALSE ); //设置缺省菜单项

//CheckMenuItem一样,第一个参数的含义由第二个参数的值来决定。如果fByPos = FALSE  uItem //是菜单项的一个标识;否则是菜单项的位置。注意,一个子菜单中,缺省菜单只能有一项。

//CMenu::SetDefaultItem Sets the default menu item for the specified menu.

 

BOOL SetMenuItemBitmaps(UINT nPosition, UINT nFlags,

const CBitmap* pBmpUnchecked /*选中时指向一幅位图*/,

const CBitmap* pBmpChecked /*未选中时指向另外一副位图*/

);//CMenu::SetMenuItemBitmaps 设置位图标题菜单。

 

UINT EnableMenuItem(UINT nIDEnableItem, UINT nEnable );//使菜单项有效,无效,或变灰

//nEnable: Specifies 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 bitwise OR operator. 注意:在程序中直接使用该函数,,菜单项不一定能够会失效,还得先把m_bAutoMenuEnable设为FALSE才行.这个叫命令更新机制

 

BOOL SetMenu( CMenu* pMenu );//CWnd::SetMenu在当前窗口上设置新菜单或移除菜单

HMENU Detach( );//CMenu::Detach Detaches a Windows menu from a CMenu object and returns the handle.

说明:

1)在计算子菜单菜单项的索引的时候,分隔栏符也算索引的

2)int GetSystemMetrics(int nIndex)获取系统信息度量。可以用它来获取菜单标题的尺寸从而设置位图标题菜单中位图的大小。还记得前面学过的GetTextMetrics()吗?它用来获取文本信息度量。

SM_CXMENUCHECK,

SM_CYMENUCHECK    //Dimensions, in pixels, of the default menu check-mark bitmap.

 

3)在MFC中MFC为我们提供了一套命令更新机制,所有菜单项的更新都是由这套机制来完成的。所以要想利用CMenu::EnableMenuItem来自己控制菜单使用或不使用变灰等,必须要在CMainFrame的构造函数中将变量m_bAutoMenuEnable设置为FALSE。

 

4)CMenu

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 a local, then call CMenu’s 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 object’s Detach member function. The CWnd::SetMenu member function sets the window’s menu to the new menu, causes the window 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 menu itself is automatically destroyed when the window is destroyed.也就是说,对于局部函数里面创建的Cmenu 对象使用时,当函数销毁时该对象也会被销毁,但是当调用Detach成员函数后,它会将Cmenu对象与函数断开,这样就不会被销毁。

5)You can use the LoadMenuIndirect member function to create a menu from a template in memory, but a menu created from a resource by a call to LoadMenu is more easily maintained, and the menu resource itself can be created and modified by the menu editor.

 

6)EXAMPLE:

CMenu menu;//定义为局部对象

menu.LoadMenu(IDR_MAINFRAME);

SetMenu(&menu);

menu.Detach();// 这里menu对象作为一个局部对象。使用Detach()从menu对象中分离窗口菜单句柄,从而当menu对象析构的时候窗口菜单资源不随之销毁。

 

5,命令更新机制:

 

菜单项状态的维护是依赖于CN_UPDATE_COMMAND_UI消息,谁捕获CN_UPDATE_COMMAND_UI消息,MFC就在其中创建一个CCmdUI对象。

在后台操作系统发出WM_INITMENUPOPUP消息,然后由MFC的基类如CFrameWnd接管并创建一个CCmdUI对象和第一个菜单项相关联,调用对象成员函数DoUpdate()(注:这个函数在MSDN中没有找到说明)发出CN_UPDATE_COMMAND_UI消息,这条消息带有指向CCmdUI对象的指针。此后同一个CCmdUI对象又设置为与第二个菜单项相关联,这样顺序进行,直到完成所有菜单项。

更新命令UI处理程序仅应用于弹出式菜单项上的项目不能应用于永久显示的顶级菜单项目。

 

说明:

1)可以手工或用ClassWizard来给菜单项添加 UPDATE_COMMAND_UI 消息响应,利用响应函数中传进来的CCmdUI对象指针可完成设置菜单项可使用,不可使用、变灰、设置标记菜单等操作。

 

6,如果要想让工具栏上的某个图标与菜单项的某个菜单相关联只需要将图标的ID设置为该菜单项的ID。但是它们的索引号未必相同。工具栏图标的索引记数顺序是:从做至右从0开始,分隔符也算索引号。

 

7,实现右键快捷菜单

利用向项目中添加VC的POPMENU控件:Project->Add to Project->Components and Controls..

系统增加的内容:A,一个菜单资源;B,在派生View类中增加了OnContextMenu()函数

说明:

1)CWnd::OnContextMenu Called by the framework when the user has clicked the right mouse button (right clicked) in the window. You can process this message by displaying a context menu using the TrackPopupMenu.

2)BOOL TrackPopupMenu( UINT nFlags, int x, int y, CWnd* pWnd, LPCRECT lpRect = NULL );

 

CMenu::TrackPopupMenu //Displays a floating pop-up menu at the specified location and tracks the selection of items on the pop-up menu. A floating pop-up menu can appear anywhere on the screen.

 

8,利用调用TrackPopupMenu函数,手工添加弹出菜单:

1)用资源管理器添加一个菜单资源

2)在鼠标右键消息响应函数(比如:void CMenuView::OnRButtonDown()

中,加载菜单资源(menu.LoadMenu();),并获得要显示的子菜单指针(CMenu *pPopup = menu.GetSubMenu(0);),并用该指针调用TrackPopupMenu函数便完成任务(pPopup->  TrackPopupMenu();但要注意:鼠标响应函数OnRButtonDown传进来的坐标是客户区坐标,而TrackPopupMenu函数中使用的是屏幕坐标,在调用TrackPopupMenu前要调用ClientToScreen客户区坐标到屏幕坐标的转换)

实例代码:

说明:

CWnd::ClientToScreen(..);//将一个坐标点或一个矩形区域坐标转换成屏幕坐标。

CMenu::TrackPopupMenu(..);//在指定位置以指定的方式显示弹出菜单。

CWnd::ScreenToClient(..);

//Converts the screen coordinates of a given point or rectangle on the display to client coordinates.

 

9,弹出菜单像普通菜单一样,也可以为各个菜单项设置响应函数。当弹出菜单属于框架窗口的时候(可在TrackPopupMenu函数参数中设置),弹出菜单上的消息,在路由的时候,仍然遵循View-DOC-MainFrame-APP的响应顺序。

 

10,动态菜单编程:

所有的资源对象都有一个数据成员保存了资源的句柄。

CMenu::AppendMenu( UINT nFlags, UINT nIDNewItem = 0, LPCTSTR lpszNewItem = NULL )

 //Appends a new item to the end of a menu.

CMenu::CreatePopupMenu //Creates an empty pop-up menu and attaches it to a CMenu object.

CMenu::InsertMenu       //Inserts a new menu item at the position specified by nPosition and moves other items down the menu.

CMenu::GetSubMenu   //Retrieves a pointer to a pop-up menu.

CWnd::GetMenu        //Retrieves a pointer to the menu for this window.

CMenu::DeleteMenu //Deletes an item from the menu.

 

11,手动给动态菜单项添加响应函数:

在Resource.h中可以添加资源的ID

在头文件中写消息函数原型

在代码文件中添加消息映射和添加消息响应函数

说明:

可以先创建一个临时的菜单项,设置它的ID和动态菜单项的一致,然后对它用向导进行消息响应,然后删除临时菜单。

再在代码文件中把消息映射放到宏外(注意一定要放到宏外面,因为CLASSWIZARD发现菜单删除了,同时要把其宏对里的消息映射也删除掉的)

 

12,CWnd::DrawMenuBar //重绘菜单

//Redraws the menu bar. If a menu bar is changed after Windows has created the window, call this function to draw the changed menu bar

 

CWnd::GetParent //get a pointer to a child window’s parent window (if any).

CWnd::Invalidate(BOOL bErase = TRUE ) //窗口重绘,bErase 在重绘时是否擦除以前的背景

 

13,集合类:

CStringArray,CStringArray,CDWordArray,CPtrArray,CStringArray,CUIntArray,CWordArray

其中成员函数:

CArray::GetAt //取出一个数据,CStringArray.GetAt通过一个索引返回CString

CArray::Add      //动态增加成员,CStringArray.Add增加一个常量的字符串

CStringArray支持CString对象数组,可以动态增加

 

14,命令消息是到OnCommand函数的时候完成路由的。

由于CWnd::OnCommand 是个虚函数,可以在框架类中重写OnCommand函数,从而可以截获菜单消息使它不再往下(VIEW类)路由。

例:

BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam)

{

// TODO: Add your specialized code here and/or call the base class

int MenuCmdId=LOWORD(wParam);//取wParm的低字节序,作为命令ID

CMenu2View *pView=(CMenu2View*)GetActiveView();//获取当前VIEW类指针

if(MenuCmdId>=IDM_PHONE1 && MenuCmdId<IDM_PHONE1+pView->m_strArray.GetSize())

//消息范围判断

{

CClientDC dc(pView);

dc.TextOut(0,0,pView->m_strArray.GetAt(MenuCmdId-IDM_PHONE1));

return TRUE;

//函数返回,避免调用CFrameWnd::OnCommand函数,在CFrameWnd::OnCommand中截获的消息会交由VIEW类处理

}

return CFrameWnd::OnCommand(wParam, lParam);

//调用基类OnCommand函数,在CFrameWnd::OnCommand中截获的消息会交由VIEW类处理

}

 

 

MSDN说明:

virtual BOOL OnCommand( WPARAM wParam, LPARAM lParam );

//The framework calls this member function when the user selects an item from a menu, when a child control sends a notification message, or when an accelerator keystroke is translated.

OnCommand processes the message map for control notification and ON_COMMAND entries, and calls the appropriate member function.

Override this member function in your derived class to handle the WM_COMMAND message. An override will not process the message map unless the base class OnCommand is called.

 

15,LOWORD与HIWORD宏

WORD LOWORD(

DWORD dwValue // value from which low-order word is retrieved

);

WORD HIWORD(

DWORD dwValue // value from which high-order word is retrieved

);

 

//The LOWORD macro retrieves the low-order word from the given 32-bit value.

//The HIWORD macro retrieves the high-order word from the given 32-bit value.

 

 

16,CFrameWnd::GetActiveView

CView* GetActiveView( ) const;//获取当前视窗口指针(单文档框架中)

 

17,源文件是单独参与编译的。

注意:Wnd类定义了MessageBox函数,在DocApp类中没有定义,要弹出窗口要用全局的AfxMessageBox函数。

WindowProcCWnd的成员函数

命令消息

通告消息

标记菜单:是指菜单项可以标记对钩,

 

注意:子菜单不是指菜单项,这两个概念不要搞混了

使“新建”菜单项失效步骤如下:

1.ClassWizard来给菜单项ID_FILE_NEW添加UPDATE_COMMAND_UI消息响应

2.利用响应函数中传进来的CCmdUI对象指针可完成设置菜单项失效

 

动态创建菜单项实例

上述代码写在CMainFrame::OnCreate()

效果如下:

 

利用向项目中添加VC的POPMENU控件:

Project->Add to Project->Components and Controls

 

使用 Microsoft Office OneNote 2003 创建
万千笔记,一处锁定。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值