📄 visual c++编程技巧之八.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0038)http://www.vchome.net/tech/vctip8.html -->
<HTML><HEAD><TITLE>Visual C++编程技巧之八</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 5.50.4134.100" name=GENERATOR>
<META content=sss name=doccomm>
<META content=C:\MSOffice\Template\Sl_BOOK.dot name=Template>
<META content="Cheng Jinxing" name=Author></HEAD>
<BODY text=#000000 vLink=#551a8b aLink=#ff0000 link=#0000ee bgColor=#ccffff>
<P align=center><BR><FONT face=楷体_GB2312 size=+3>Visual C++编程技巧之八</FONT>
</P>
<P><A href="http://www.vchome.net/tech/vctip8.html#tip57"><FONT
face="Times New Roman" size=+0>57</FONT><FONT size=+0>、为什么即使调用</FONT><FONT
face="Times New Roman" size=+0>EnableMenuItem</FONT><FONT
size=+0>菜单项后,菜单项还处于禁止状态</FONT></A> </P>
<P><A href="http://www.vchome.net/tech/vctip8.html#tip58"><FONT
face="Times New Roman" size=+0>58</FONT><FONT size=+0>、如何给系统菜单添加一个菜单项</FONT></A>
</P>
<P><A href="http://www.vchome.net/tech/vctip8.html#tip59"><FONT
face="Times New Roman" size=+0>59</FONT><FONT
size=+0>、如何确定顶层菜单所占据的菜单行数</FONT></A> </P>
<P><A href="http://www.vchome.net/tech/vctip8.html#tip60"><FONT
face="Times New Roman" size=+0>60</FONT><FONT
size=+0>、在用户环境中如何确定系统显示元素的颜色</FONT></A> </P>
<P><A href="http://www.vchome.net/tech/vctip8.html#tip61"><FONT
face="Times New Roman" size=+0>61</FONT><FONT size=+0>、如何查询和设置系统参数</FONT></A>
</P>
<P><A href="http://www.vchome.net/tech/vctip8.html#tip62"><FONT
face="Times New Roman" size=+0>62</FONT><FONT size=+0>、如何使用一个预定义的</FONT><FONT
face="Times New Roman" size=+0>Windows</FONT><FONT size=+0>光标</FONT></A> </P>
<P><A href="http://www.vchome.net/tech/vctip8.html#tip63"><FONT
face="Times New Roman" size=+0>63</FONT><FONT size=+0>、如何确定当前屏幕分辨率</FONT></A>
</P>
<P><A href="http://www.vchome.net/tech/vctip8.html#tip64"><FONT
face="Times New Roman" size=+0>64</FONT><FONT size=+0>、如何检索原先的</FONT><FONT
face="Times New Roman" size=+0>Task Manager</FONT><FONT
size=+0>应用程序使用的任务列表</FONT></A> </P>
<P><A href="http://www.vchome.net/tech/vctip8.html#tip65"><FONT
face="Times New Roman" size=+0>65</FONT><FONT size=+0>、如何确定</FONT><FONT
face="Times New Roman" size=+0>Windows</FONT><FONT size=+0>和</FONT><FONT
face="Times New Roman" size=+0>Windows</FONT><FONT size=+0>系统目录</FONT></A> </P>
<P><A href="http://www.vchome.net/tech/vctip8.html#tip66"><FONT
face="Times New Roman" size=+0>66</FONT><FONT size=+0>、在哪儿创建临文件</FONT></A> </P>
<P><A href="http://www.vchome.net/tech/vctip8.html#tip67"><FONT
face="Times New Roman" size=+0>67</FONT><FONT size=+0>、如何访问桌面窗口</FONT></A> </P>
<P><FONT size=+2><B> </B></FONT> </P>
<P><A name=tip57></A><B><FONT size=+2><FONT
face="Times New Roman">57</FONT><FONT face=黑体>、为什么即使调用</FONT><FONT
face="Times New Roman">EnableMenuItem</FONT><FONT
face=黑体>菜单项后,菜单项还处于禁止状态</FONT></FONT></B> </P>
<P><FONT size=+0>需要将<FONT face="Times New Roman">CFrameWnd::
m_bAutomenuEnable</FONT>设置为<FONT
face="Times New Roman">FALSE</FONT>,如果该数据成员为<FONT
face="Times New Roman">TRUE</FONT>(缺省值),工作框将自动地禁止没有<FONT
face="Times New Roman">ON_UPDATE_COMMAND_UI</FONT>或者<FONT
face="Times New Roman">ON_COMMAND</FONT>的菜单项。</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Disable MFC from automatically
disabling menu items.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>m_bAuoMenuEnable=FALSE;</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Now enable the menu item.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CMenu* pMenu=GetMenu ();</FONT> </P>
<P><FONT face="Times New Roman" size=+0>ASSERT_VALID (pMenu);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>pMenu->EnableMenuItem
(ID_MENU_ITEM,MF_BYCOMMAND | MF_ENABLED);</FONT> </P>
<P><A name=tip58></A><B><FONT size=+2><FONT
face="Times New Roman">58</FONT><FONT face=黑体>、如何给系统菜单添加一个菜单项</FONT></FONT></B>
</P>
<P><FONT size=+0>给系统菜单添加一个菜单项需要进行下述三个步骤:</FONT> </P>
<P><FONT size=+0>首先,使用<FONT face="Times New Roman">Resource
Symbols</FONT>对话(在<FONT face="Times New Roman">View</FONT>菜单中选择<FONT
face="Times New Roman">Resource Symbols</FONT>...</FONT> </P>
<P><FONT size=+0>可以显示该对话)定义菜单项<FONT face="Times New Roman">ID</FONT>,该<FONT
face="Times New Roman">ID</FONT>应大于<FONT
face="Times New Roman">0x0F</FONT>而小于<FONT
face="Times New Roman">0xF000</FONT>;</FONT> </P>
<P><FONT size=+0>其次,调用<FONT
face="Times New Roman">CWnd::GetSystemMenu</FONT>获取系统菜单的指针并调用<FONT
face="Times New Roman">CWnd::
Appendmenu</FONT>将菜单项添加到菜单中。下例给系统菜单添加两个新的菜单项:</FONT> </P>
<P><FONT face="Times New Roman" size=+0>int CMainFrame:: OnCreate
(LPCREATESTRUCT lpCreateStruct)</FONT> </P>
<P><FONT face="Times New Roman" size=+0>{</FONT> </P>
<P><FONT size=+0>…</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Make sure system menu item is in the
right range.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>ASSERT (IDM_MYSYSITEM
&0xFFF0)==IDM_MYSYSITEM);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>ASSERT (IDM-MYSYSITEM<0xF000);</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>//Get pointer to system menu.</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>CMenu* pSysmenu=GetSystemmenu
(FALSE);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>ASSERT_VALID (pSysMenu);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Add a separator and our menu item to
system menu.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CString StrMenuItem (_T ("New menu
item"));</FONT> </P>
<P><FONT face="Times New Roman" size=+0>pSysMenu->Appendmenu
(MF_SEPARATOR);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>pSysMenu->AppendMenu (MF_STRING,
IDM_MYSYSITEM, strMenuitem);</FONT> </P>
<P><FONT size=+0>…</FONT> </P>
<P><FONT face="Times New Roman" size=+0>}</FONT> </P>
<P><FONT size=+0>现在,选择系统菜单项时用户应进行检测。使用<FONT
face="Times New Roman">ClassWizard</FONT>处理</FONT> </P>
<P><FONT size=+0><FONT
face="Times New Roman">WM_SYSCOMMAND</FONT>消息并检测用户菜单的<FONT
face="Times New Roman">nID</FONT>参数:</FONT> </P>
<P><FONT face="Times New Roman" size=+0>void CMainFrame:: OnSysCommand (UINT
nID,LPARAM lParam)</FONT> </P>
<P><FONT face="Times New Roman" size=+0>{</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Determine if our system menu item was
selected.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>if ( (nID &
0xFFF0)==IDM_MYSYSITEM)</FONT> </P>
<P><FONT face="Times New Roman" size=+0>{</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//TODO-process system menu item</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>}</FONT> </P>
<P><FONT face="Times New Roman" size=+0>else</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CMDIFrameWnd:: OnSysCommand (nID,
lParam);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>}</FONT> </P>
<P><FONT size=+0>最后,一个设计良好的<FONT
face="Times New Roman">UI</FONT>应用程序应当在系统菜单项加亮时在状态条显示一个帮助信息,这可以通过增加一个包含系统菜单基<FONT
face="Times New Roman">ID</FONT>的串表的入口来实现。</FONT> </P>
<P><A name=tip59></A><B><FONT size=+2><FONT
face="Times New Roman">59</FONT><FONT
face=黑体>、如何确定顶层菜单所占据的菜单行数</FONT></FONT></B> </P>
<P><FONT
size=+0>这可以通过简单的减法和除法来实现。首先,用户需要计算主框窗口的高度和客户区;其次,从主框窗口的高度中减去客户区、框边界以及标题的高度;最后,除以菜单栏的高度。下例成员函数是一个计算主框菜单所占据的行数的代码实现。</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>int CMainFrame:: GetMenuRows ()</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>{</FONT> </P>
<P><FONT face="Times New Roman" size=+0>CRect rcFrame,rcClient;</FONT> </P>
<P><FONT face="Times New Roman" size=+0>GetWindowRect (rcFrame);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>GetClientRect (rcClient);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>return (rcFrame.Height ()
-rcClient.Height ()-</FONT> </P>
<P><FONT face="Times New Roman" size=+0>:: GetSystemMetrics (SM_CYCAPTION)
-</FONT> </P>
<P><FONT face="Times New Roman" size=+0>(:: getSystemMetrics (SM_CYFRAME) *2))
/</FONT> </P>
<P><FONT face="Times New Roman" size=+0>:: GetSystemMetrics (SM_CYMENU);</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>}</FONT> </P>
<P><A name=tip60></A><B><FONT size=+2><FONT
face="Times New Roman">60</FONT><FONT
face=黑体>、在用户环境中如何确定系统显示元素的颜色</FONT></FONT></B> </P>
<P><FONT size=+0>调用<FONT face="Times New Roman">SDK</FONT>函数<FONT
face="Times New Roman">GetSysColor</FONT>可以获取一个特定显示元素的颜色。下例说明了如何在<FONT
face="Times New Roman">MFC</FONT>函数<FONT face="Times New Roman">CMainFrameWnd::
OnNcPaint</FONT>中调用该函数设置窗口标题颜色。</FONT> </P>
<P><FONT face="Times New Roman" size=+0>void CMiniFrameWnd:: OnNcPaint ()</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>{</FONT> </P>
<P><FONT size=+0>…</FONT> </P>
<P><FONT face="Times New Roman" size=+0>dc.SetTextColor (:: GetSysColor
(m_bActive ?</FONT> </P>
<P><FONT face="Times New Roman" size=+0>COLOR_CAPTIONTEXT :
COLOR_INACTIVECAPTIONTEXT));</FONT> </P>
<P><FONT size=+0>…</FONT> </P>
<P><FONT face="Times New Roman" size=+0>}</FONT> </P>
<P><A name=tip61></A><B><FONT size=+2><FONT
face="Times New Roman">61</FONT><FONT face=黑体>、如何查询和设置系统参数</FONT></FONT></B>
</P>
<P><FONT size=+0>在<FONT face="Times New Roman">Windows 3.1 SDK</FONT>中介绍过<FONT
face="Times New Roman">SDK</FONT>函数<FONT
face="Times New Roman">SystemParametersInfo</FONT>,调用该函数可以查询和设置系统参数,诸如按键的重复速率设置、鼠标双击延迟时间、图标字体以及桌面覆盖位图等等。</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>//Create a font that is used for icon
titles.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>LOGFONT stFont;</FONT> </P>
<P><FONT face="Times New Roman" size=+0>:: SystemParametersInfo
(SPIF_GETICONTITLELOGFONT,</FONT> </P>
<P><FONT face="Times New Roman" size=+0>sizeof (LOGFONT), &stFont,
SPIF_SENDWININICHANGE);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>m_font.CreateFontIndirect
(&stFont);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Change the wallpaper to
leaves.bmp.</FONT> </P>
<P><FONT face="Times New Roman" size=+0>:: SystemParametersInfo
(SPI_SETDESKWALLPAPER, 0,</FONT> </P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -