⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 菜单控制.htm

📁 VC的一些技巧性文档
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html><style type="text/css"><!--.p9 {  font-family: "宋体"; font-size: 9pt}a        {text-transform: none; text-decoration: none;}a:hover {text-decoration: underline; color: #FF0000;}--></style><body background="di2001.jpg"><h3 align="center"><font COLOR="#AOAO99">菜单控制</font></h3><table width="100%"><tr><td><font color="0000FF"><a name="1">为什么即使调用EnableMenuItem菜单项后,菜单项还处于禁止状态 </a></font></td></tr><tr><td><p></Br>需要将CFrameWnd:: m_bAutomenuEnable设置为FALSE,如果该数据成员为TRUE(缺省值),工作框将自动地禁止没有ON_UPDATE_COMMAND_UI或者ON_COMMAND的菜单项。<Br></Br>//Disable MFC from automatically disabling menu items.<Br></Br>m_bAuoMenuEnable=FALSE;<Br></Br>//Now enable the menu item.<Br></Br>CMenu* pMenu=GetMenu ();<Br></Br>ASSERT_VALID (pMenu);<Br></Br>pMenu->EnableMenuItem (ID_MENU_ITEM,MF_BYCOMMAND | MF_ENABLED);<Br></Br></p></td></tr><tr><td><font color="0000FF"><a name="2">如何给系统菜单添加一个菜单项 </a></font></td></tr><tr><td><p></Br>给系统菜单添加一个菜单项需要进行下述三个步骤:<Br></Br>首先,使用Resource Symbols对话(在View菜单中选择Resource Symbols...<Br></Br>可以显示该对话)定义菜单项ID,该ID应大于0x0F而小于0xF000;<Br></Br>其次,调用CWnd::GetSystemMenu获取系统菜单的指针并调用CWnd:: Appendmenu将菜单项添加到菜单中。下例给系统菜单添加两个新的菜单项:<Br></Br>int CMainFrame:: OnCreate (LPCREATESTRUCT lpCreateStruct)<Br></Br>{<Br></Br>…<Br></Br>//Make sure system menu item is in the right range.<Br></Br>ASSERT (IDM_MYSYSITEM &0xFFF0)==IDM_MYSYSITEM);<Br></Br>ASSERT (IDM-MYSYSITEM<0xF000);<Br></Br>//Get pointer to system menu.<Br></Br>CMenu* pSysmenu=GetSystemmenu (FALSE);<Br></Br>ASSERT_VALID (pSysMenu);<Br></Br>//Add a separator and our menu item to system menu.<Br></Br>CString StrMenuItem (_T ("New menu item"));<Br></Br>pSysMenu->Appendmenu (MF_SEPARATOR);<Br></Br>pSysMenu->AppendMenu (MF_STRING, IDM_MYSYSITEM, strMenuitem);<Br></Br>…<Br></Br>}<Br></Br>现在,选择系统菜单项时用户应进行检测。使用ClassWizard处理<Br></Br>WM_SYSCOMMAND消息并检测用户菜单的nID参数:<Br></Br>void CMainFrame:: OnSysCommand (UINT nID,LPARAM lParam)<Br></Br>{<Br></Br>//Determine if our system menu item was selected.<Br></Br>if ( (nID & 0xFFF0)==IDM_MYSYSITEM)<Br></Br>{<Br></Br>//TODO-process system menu item<Br></Br>}<Br></Br>else<Br></Br>CMDIFrameWnd:: OnSysCommand (nID, lParam);<Br></Br>}<Br></Br>最后,一个设计良好的UI应用程序应当在系统菜单项加亮时在状态条显示一个帮助信息,这可以通过增加一个包含系统菜单基ID的串表的入口来实现。<Br></Br></p></td></tr><tr><td><font color="0000FF"><a name="3">如何确定顶层菜单所占据的菜单行数 </a></font></td></tr><tr><td><p></Br>这可以通过简单的减法和除法来实现。首先,用户需要计算主框窗口的高度和客户区;其次,从主框窗口的高度中减去客户区、框边界以及标题的高度;最后,除以菜单栏的高度。下例成员函数是一个计算主框菜单所占据的行数的代码实现。<Br></Br>int CMainFrame:: GetMenuRows ()<Br></Br>{<Br></Br>CRect rcFrame,rcClient;<Br></Br>GetWindowRect (rcFrame);<Br></Br>GetClientRect (rcClient);<Br></Br>return (rcFrame.Height () -rcClient.Height ()-<Br></Br>:: GetSystemMetrics (SM_CYCAPTION) -<Br></Br>(:: getSystemMetrics (SM_CYFRAME) *2)) /<Br></Br>:: GetSystemMetrics (SM_CYMENU);<Br></Br>}<Br></Br></Br></p></td></tr><tr><td><font color="0000FF"><a name="4">自绘菜单<Br>闻怡洋译</a></font></td></tr><tr><td><p></Br>在这里提供一个C++类(CCustomMenu),该类是CMenu的子类,并且拥有自绘能力。它可以向你提供以下的功能:<Br></Br>设置字体颜色。<Br>设置高亮度颜色。<Br>设置高亮度时的风格。<Br>设置选中时和在普通状态下的菜单显示的图标。<Br>设置显示图标大小。<Br>在CCustomMenu中定义了结构MENUDATA,你必须根据你的需要填充该结构,并且在增加菜单时提供该结构的指针(调用AppendMenu,InsertMenu)。下面是一个例子:<Br></Br>1、定义CCustomMenu的实例,和MENUDATA结构变量。<Br></Br>&nbsp;&nbsp;&nbsp;&nbsp;CCustomMenu m_cCustomMenu;<Br>&nbsp;&nbsp;&nbsp;&nbsp;MENUDATA menuData [8];	// as many menu items are present , You should be able to use<Br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//new and do the same<Br>2、调用CreateMenu()设置有关参数。<Br>&nbsp;&nbsp;&nbsp;&nbsp;m_customMenu.CreateMenu ();<Br>&nbsp;&nbsp;&nbsp;&nbsp;m_customMenu.SetIconSize (25,25);	//This is to set the size of the Icon.<Br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// This should be used only once for any menu<Br>&nbsp;&nbsp;&nbsp;&nbsp;// in order to resize it, destroy and create the menu again with  different size.<Br>&nbsp;&nbsp;&nbsp;&nbsp;m_customMenu.SetHighlightStyle (Normal); //Or TextOnly, if you want the<Br>&nbsp;&nbsp;&nbsp;&nbsp;// background color to remain the same<Br>&nbsp;&nbsp;&nbsp;&nbsp;// and the Text color to change to the Highlight color.<Br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// The following setXXXColor sets the menu colors. If you dont want to change any, Dont call these member functions.<Br>&nbsp;&nbsp;&nbsp;&nbsp;       m_customMenu.SetTextColor (RGB (255,0,0));<Br>&nbsp;&nbsp;&nbsp;&nbsp;       m_customMenu.SetBackColor (RGB (255,255,255));<Br>&nbsp;&nbsp;&nbsp;&nbsp;       m_customMenu.SetHighlightColor (RGB (0,0,255));<Br>3、设置MENUDATA变量,并增加菜单项。<Br>&nbsp;&nbsp;&nbsp;&nbsp;    lstrcpy (menuData[0].menuText , "text1");<Br>&nbsp;&nbsp;&nbsp;&nbsp;    menuData[0].menuIconNormal= IDI_ICON1;<Br>&nbsp;&nbsp;&nbsp;&nbsp;    m_customMenu.AppendMenu (MF_OWNERDRAW,3,(LPCTSTR)menuData);<Br></Br>3、在你的窗口中重载OnMeasureItem(...)函数。<Br>void CMyView::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)<Br>{<Br>&nbsp;&nbsp;&nbsp;&nbsp;if ( lpMeasureItemStruct->CtlType == ODT_MENU &&<Br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IsMenu((HMENU)lpMeasureItemStruct->itemID) &&<Br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(lpMeasureItemStruct->itemID == (UINT)m_hMenuSub) )<Br>&nbsp;&nbsp;&nbsp;&nbsp;{<Br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_customMenu.MeasureItem (lpMeasureItemStruct);<Br>&nbsp;&nbsp;&nbsp;&nbsp;}<Br>&nbsp;&nbsp;&nbsp;&nbsp;else<Br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// let MFC's self-drawing handle it<Br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CView::OnMeasureItem(nIDCtl, lpMeasureItemStruct);<Br>}<Br></Br>下面的函数将帮助你设置菜单属性。<Br></Br>&nbsp;&nbsp;&nbsp;&nbsp;void SetTextColor (COLORREF );<Br>&nbsp;&nbsp;&nbsp;&nbsp;void SetBackColor (COLORREF);<Br>&nbsp;&nbsp;&nbsp;&nbsp;void SetHighlightColor (COLORREF);<Br>&nbsp;&nbsp;&nbsp;&nbsp;void SetIconSize (int, int);<Br>&nbsp;&nbsp;&nbsp;&nbsp;void SetHighlightStyle (HIGHLIGHTSTYLE ); // HIGHLIGHTSTYLE : enum {Normal, TextOnly}<Br>&nbsp;&nbsp;&nbsp;&nbsp;void SetHighlightTextColor (COLORREF);<Br></Br></Br></Br>下面是文件代码:<Br>//*************************************************************************<Br>// CustomMenu.h : header file<Br>//<Br></Br>#if<Br>!defined(AFX_CUSTOMMENU_H__FE5B01C3_1E02_11D1_B87A_0060979CDF6D__INCLUDED_)<Br>#define AFX_CUSTOMMENU_H__FE5B01C3_1E02_11D1_B87A_0060979CDF6D__INCLUDED_<Br></Br>#if _MSC_VER >= 1000<Br>#pragma once<Br>#endif // _MSC_VER >= 1000<Br>class MENUDATA<Br>{<Br>public:<Br>&nbsp;&nbsp;&nbsp;&nbsp;MENUDATA () { menuIconNormal = -1; menuIconSelected = -1;};<Br>&nbsp;&nbsp;&nbsp;&nbsp;char  menuText[32];<Br>&nbsp;&nbsp;&nbsp;&nbsp;UINT menuIconNormal;<Br>&nbsp;&nbsp;&nbsp;&nbsp;UINT menuIconSelected;<Br>};<Br></Br></Br>typedef enum {Normal,TextOnly} HIGHLIGHTSTYLE;<Br></Br>////////////////////////////////////////////////<Br>//<Br>// CCustomMenu window<Br></Br>class CCustomMenu : public CMenu<Br>{<Br>// Construction<Br>public:<Br>&nbsp;&nbsp;&nbsp;&nbsp;CCustomMenu();<Br></Br>// Attributes<Br>public:<Br></Br>// Operations<Br>public:<Br></Br>// Overrides<Br>&nbsp;&nbsp;&nbsp;&nbsp;// ClassWizard generated virtual function overrides<Br>&nbsp;&nbsp;&nbsp;&nbsp;//{{AFX_VIRTUAL(CCustomMenu)<Br>&nbsp;&nbsp;&nbsp;&nbsp;//}}AFX_VIRTUAL<Br></Br>// Implementation<Br>public:<Br>&nbsp;&nbsp;&nbsp;&nbsp;virtual ~CCustomMenu();<Br>&nbsp;&nbsp;&nbsp;&nbsp;virtual void DrawItem( LPDRAWITEMSTRUCT);<Br>&nbsp;&nbsp;&nbsp;&nbsp;virtual void MeasureItem( LPMEASUREITEMSTRUCT );<Br>&nbsp;&nbsp;&nbsp;&nbsp;void SetTextColor (COLORREF );<Br>&nbsp;&nbsp;&nbsp;&nbsp;void SetBackColor (COLORREF);<Br>&nbsp;&nbsp;&nbsp;&nbsp;void SetHighlightColor (COLORREF);<Br>&nbsp;&nbsp;&nbsp;&nbsp;void SetIconSize (int, int);<Br>&nbsp;&nbsp;&nbsp;&nbsp;void SetHighlightStyle (HIGHLIGHTSTYLE );<Br>&nbsp;&nbsp;&nbsp;&nbsp;void SetHighlightTextColor (COLORREF);<Br></Br>&nbsp;&nbsp;&nbsp;&nbsp;// Generated message map functions<Br>protected:<Br>&nbsp;&nbsp;&nbsp;&nbsp;COLORREF m_crText;<Br>&nbsp;&nbsp;&nbsp;&nbsp;COLORREF m_clrBack;<Br>&nbsp;&nbsp;&nbsp;&nbsp;COLORREF m_clrText;<Br>&nbsp;&nbsp;&nbsp;&nbsp;COLORREF m_clrHilight;<Br>&nbsp;&nbsp;&nbsp;&nbsp;COLORREF m_clrHilightText;<Br>&nbsp;&nbsp;&nbsp;&nbsp;LOGFONT m_lf;<Br>&nbsp;&nbsp;&nbsp;&nbsp;CFont m_fontMenu;<Br>&nbsp;&nbsp;&nbsp;&nbsp;UINT m_iMenuHeight;<Br>&nbsp;&nbsp;&nbsp;&nbsp;BOOL m_bLBtnDown;<Br>&nbsp;&nbsp;&nbsp;&nbsp;CBrush m_brBackground,m_brSelect;<Br>&nbsp;&nbsp;&nbsp;&nbsp;CPen m_penBack;<Br>&nbsp;&nbsp;&nbsp;&nbsp;int m_iconX,m_iconY;<Br>&nbsp;&nbsp;&nbsp;&nbsp;HIGHLIGHTSTYLE m_hilightStyle;<Br></Br>&nbsp;&nbsp;&nbsp;&nbsp;//{{AFX_MSG(CCustomMenu)<Br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// NOTE - the ClassWizard will add and remove member functions here.<Br>&nbsp;&nbsp;&nbsp;&nbsp;//}}AFX_MSG<Br>};<Br>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -