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

📄 csdn_文档中心_vc++的菜单控制和自绘菜单。.htm

📁 csdn10年中间经典帖子
💻 HTM
📖 第 1 页 / 共 3 页
字号:
        <TR>
          <TD align=middle height=5></TD>
          <TD align=middle width=500></TD></TR>
        <TR>
          <TD align=middle bgColor=#003399 height=10><FONT 
            color=#ffffff>出处</FONT></TD>
          <TD height=10>&nbsp;&nbsp;&nbsp;&nbsp;<A 
            href="http://www.chinaasp.com/sqlbbs/showEssence.asp?id=830">http://www.chinaasp.com/sqlbbs/showEssence.asp?id=830</A></TD></TR>
        <TR>
          <TD align=middle height=10></TD>
          <TD height=10></TD></TR></TBODY></TABLE><!--文章说明信息结束//-->
      <TABLE border=0 width=600>
        <TBODY>
        <TR>
          <TD align=left><BR><FONT face=宋体 
            size=2>菜单控制<BR>为什么即使调用EnableMenuItem菜单项后,菜单项还处于禁止状态&nbsp;&nbsp;<BR><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-&gt;EnableMenuItem (ID_MENU_ITEM,MF_BYCOMMAND 
            | 
            MF_ENABLED);<BR><BR><BR><BR>如何给系统菜单添加一个菜单项&nbsp;&nbsp;<BR><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 
            &amp;0xFFF0)==IDM_MYSYSITEM);<BR><BR>ASSERT 
            (IDM-MYSYSITEM&lt;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-&gt;Appendmenu 
            (MF_SEPARATOR);<BR><BR>pSysMenu-&gt;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 &amp; 
            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><BR><BR>如何确定顶层菜单所占据的菜单行数&nbsp;&nbsp;<BR><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><BR><BR>自绘菜单<BR>闻怡洋译 
            <BR><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;&nbsp;m_customMenu.SetTextColor 
            (RGB 
            (255,0,0));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_customMenu.SetBackColor 
            (RGB 
            (255,255,255));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_customMenu.SetHighlightColor 
            (RGB 
            (0,0,255));<BR>3、设置MENUDATA变量,并增加菜单项。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lstrcpy 
            (menuData[0].menuText , 
            "text1");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;menuData[0].menuIconNormal= 
            IDI_ICON1;<BR>&nbsp;&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-&gt;CtlType == ODT_MENU 
            &amp;&amp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IsMenu((HMENU)lpMeasureItemStruct-&gt;itemID) 
            &amp;&amp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(lpMeasureItemStruct-&gt;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 &gt;= 1000<BR>#pragma once<BR>#endif // _MSC_VER &gt;= 
            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><BR>///////////////////////////////////////<BR>//<BR><BR>//{{AFX_INSERT_LOCATION}}<BR>// 
            Microsoft Developer Studio will insert additional declarations 
            immediately before the previous line.<BR><BR>#endif 
            //!defined(AFX_CUSTOMMENU_H__FE5B01C3_1E02_11D1_B87A_0060979CDF6D__INCLUDED_)<BR><BR>//*************************************************************************<BR>// 
            CustomMenu.cpp : implementation file<BR>//<BR><BR>#include 
            "stdafx.h"<BR>#include "CustomMenu.h"<BR><BR>#ifdef 
            _DEBUG<BR>#define new DEBUG_NEW<BR>#undef THIS_FILE<BR>static char 
            THIS_FILE[] = 
            __FILE__;<BR>#endif<BR><BR>////////////////////////////////////////////////<BR>//<BR>// 
            CCustomMenu<BR><BR>CCustomMenu::CCustomMenu()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_clrText 
            = GetSysColor (COLOR_MENUTEXT);<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_clrBack 
            = GetSysColor 
            (COLOR_MENU);<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_brBackground.CreateSolidBrush 
            (m_clrBack);<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_penBack.CreatePen 
            (PS_SOLID,0,m_clrBack);<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_crText = 
            m_clrText;<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_bLBtnDown = 
            FALSE;<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_iconX = GetSystemMetrics ( 
            SM_CXMENUCHECK);<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_iconY = 
            GetSystemMetrics (SM_CYMENUCHECK 
            );<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;m_clrHilight = GetSysColor 
            (COLOR_HIGHLIGHT);<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_brSelect.CreateSolidBrush 
            (m_clrHilight);<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_clrHilightText = 
            GetSysColor 
            (COLOR_HIGHLIGHTTEXT);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;ZeroMemory 
            ((PVOID) &amp;m_lf,sizeof 
            (LOGFONT));<BR>&nbsp;&nbsp;&nbsp;&nbsp;NONCLIENTMETRICS 
            nm;<BR>&nbsp;&nbsp;&nbsp;&nbsp;nm.cbSize = sizeof 
            (NONCLIENTMETRICS);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;//Get the system 
            metrics for the Captionfromhere<BR>&nbsp;&nbsp;&nbsp;&nbsp;VERIFY 
            (SystemParametersInfo 
            (SPI_GETNONCLIENTMETRICS,0,&amp;nm,0));<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;m_lf 
            = nm.lfMenuFont;<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_iMenuHeight = 
            nm.iMenuHeight;<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_fontMenu.CreateFontIndirect 
            (&amp;m_lf);<BR>}<BR><BR>CCustomMenu::~CCustomMenu()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;if 
            ((HBRUSH) m_brBackground != 
            NULL)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_brBackground.DeleteObject 
            ();<BR>&nbsp;&nbsp;&nbsp;&nbsp;if ((HFONT)m_fontMenu 
            !=NULL)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_fontMenu.DeleteObject 
            ();<BR>&nbsp;&nbsp;&nbsp;&nbsp;if ((HBRUSH)m_brSelect != 
            NULL)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_brSelect.DeleteObject 
            ();<BR>}<BR><BR><BR><BR>////////////////////////////////////////////////<BR>//<BR>// 
            CCustomMenu message handlers<BR><BR><BR>void CCustomMenu::DrawItem 
            (LPDRAWITEMSTRUCT 
            lpDIS)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;ASSERT(lpDIS != 
            NULL);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;CDC* pDC = 

⌨️ 快捷键说明

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