📄 visual c++编程技巧之二.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0038)http://www.vchome.net/tech/vctip2.html -->
<HTML><HEAD><TITLE>unsaved:///newpage2.htm</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></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/vctip2.html#tip9"><FONT
face="Times New Roman" size=+0>9. </FONT><FONT size=+0>如何改变窗口的图标</FONT><FONT
face="Times New Roman" size=+0>?</FONT></A> </P>
<P><A href="http://www.vchome.net/tech/vctip2.html#tip10"><FONT
face="Times New Roman" size=+0>10. </FONT><FONT size=+0>如何改变窗口的缺省风格</FONT><FONT
face="Times New Roman" size=+0>?</FONT></A> </P>
<P><A href="http://www.vchome.net/tech/vctip2.html#tip11"><FONT
face="Times New Roman" size=+0>11. </FONT><FONT size=+0>如何将窗口居中显示</FONT><FONT
face="Times New Roman" size=+0>?</FONT></A> </P>
<P><A href="http://www.vchome.net/tech/vctip2.html#tip12"><FONT
face="Times New Roman" size=+0>12. </FONT><FONT size=+0>如何让窗口和 </FONT><FONT
face="Times New Roman" size=+0>MDI</FONT><FONT size=+0>窗口一启动就最大化和最小化</FONT><FONT
face="Times New Roman" size=+0>?</FONT></A> </P>
<P><A href="http://www.vchome.net/tech/vctip2.html#tip13"><FONT
face="Times New Roman" size=+0>13. </FONT><FONT size=+0>如何使程序保持极小状态</FONT><FONT
face="Times New Roman" size=+0>?</FONT></A> </P>
<P><A href="http://www.vchome.net/tech/vctip2.html#tip14"><FONT
face="Times New Roman" size=+0>14. </FONT><FONT size=+0>如何限制窗口的大小</FONT><FONT
face="Times New Roman" size=+0>?</FONT></A> </P>
<P><A href="http://www.vchome.net/tech/vctip2.html#tip15"><FONT
face="Times New Roman" size=+0>15. </FONT><FONT size=+0>如何使窗口不可见?</FONT></A>
</P>
<P><A href="http://www.vchome.net/tech/vctip2.html#tip16"><FONT
face="Times New Roman" size=+0>16. </FONT><FONT size=+0>如何使窗口始终在最前方</FONT><FONT
face="Times New Roman" size=+0>?</FONT></A> </P>
<P><FONT size=+2><B> </B></FONT> </P>
<P><A name=tip9></A><B><FONT size=+2><FONT face="Times New Roman">9.
</FONT><FONT face=黑体>如何改变窗口的图标</FONT><FONT
face="Times New Roman">?</FONT></FONT></B> </P>
<P><FONT size=+0>向窗口发送 <FONT face="Times New Roman">WM_SECTION</FONT>消息。</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>Example:</FONT> </P>
<P><FONT face="Times New Roman" size=+0>HICON hIcon=AfxGetApp()
->LoadIcon(IDI_ICON);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>ASSERT(hIcon);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>AfxGetMainWnd()
->SendMessage(WM_SECTION,TRUE,(LPARAM) hIcon);</FONT> </P>
<P><A name=tip10></A><B><FONT size=+2><FONT face="Times New Roman">10.
</FONT><FONT face=黑体>如何改变窗口的缺省风格</FONT><FONT
face="Times New Roman">?</FONT></FONT></B> </P>
<P><FONT size=+0>重栽 <FONT face="Times New Roman">CWnd:: PreCreateWindow
</FONT>并修改<FONT face="Times New Roman">CREATESTRUCT</FONT>结构来指定窗口风格和其他</FONT>
</P>
<P><FONT size=+0>创建信息<FONT face="Times New Roman">.</FONT></FONT> </P>
<P><FONT face="Times New Roman" size=+0>Example: Delete "Max" Button and Set
Original Window's Position and Size</FONT> </P>
<P><FONT face="Times New Roman" size=+0>BOOL CMainFrame:: PreCreateWindow
(CREATESTRUCT &cs)</FONT> </P>
<P><FONT face="Times New Roman" size=+0>{</FONT> </P>
<P><FONT face="Times New Roman" size=+0>cs.style &=~WS_MAXINIZEMOX;</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>cs.x=cs.y=0;</FONT> </P>
<P><FONT face="Times New Roman"
size=+0>cs.cx=GetSystemMetrics(SM_CXSCREEN/2);</FONT> </P>
<P><FONT face="Times New Roman"
size=+0>cs.cy=GetSystemMetrics(SM_CYSCREEN/2);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>return CMDIFramewnd
::PreCreateWindow(cs);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>}</FONT> </P>
<P><A name=tip11></A><B><FONT size=+2><FONT face="Times New Roman">11.
</FONT><FONT face=黑体>如何将窗口居中显示</FONT><FONT
face="Times New Roman">?</FONT></FONT></B> </P>
<P><FONT face="Times New Roman" size=+0>Easy, Call Function CWnd:: Center
Windows</FONT> </P>
<P><FONT face="Times New Roman" size=+0>Example(1): Center Window( ); //Relative
to it's parent</FONT> </P>
<P><FONT face="Times New Roman" size=+0>// Relative to Screen</FONT> </P>
<P><FONT face="Times New Roman" size=+0>Example(2): Center Window(CWnd::
GetDesktopWindow( ));</FONT> </P>
<P><FONT face="Times New Roman" size=+0>//Relative to Application's
MainWindow</FONT> </P>
<P><FONT face="Times New Roman" size=+0>AfxGetMainWnd( ) -> Center Window(
);</FONT> </P>
<P><A name=tip12></A><B><FONT size=+2><FONT face="Times New Roman">12.
</FONT><FONT face=黑体>如何让窗口和</FONT> <FONT face="Times New Roman">MDI</FONT><FONT
face=黑体>窗口一启动就最大化和最小化</FONT><FONT face="Times New Roman">?</FONT></FONT></B>
</P>
<P><FONT size=+0>先说窗口。</FONT> </P>
<P><FONT size=+0>在 <FONT face="Times New Roman">InitStance </FONT>函数中设定 <FONT
face="Times New Roman">m_nCmdShow</FONT>的 取值<FONT
face="Times New Roman">.</FONT></FONT> </P>
<P><FONT size=+0><FONT face="Times New Roman">m_nCmdShow=SW_SHOWMAXMIZED ;
//</FONT>最大化</FONT> </P>
<P><FONT size=+0><FONT face="Times New Roman">m_nCmdShow=SW_SHOWMINMIZED ;
//</FONT>最小化</FONT> </P>
<P><FONT size=+0><FONT face="Times New Roman">m_nCmdShow=SW_SHOWNORMAL ;
//</FONT>正常方式</FONT> </P>
<P><FONT size=+0><FONT face="Times New Roman">MDI</FONT>窗口<FONT
face="Times New Roman">:</FONT></FONT> </P>
<P><FONT size=+0>如果是创建新的应用程序<FONT face="Times New Roman">,</FONT>可以用 <FONT
face="Times New Roman">MFC AppWizard </FONT>的<FONT
face="Times New Roman">Advanced </FONT>按钮并在</FONT> </P>
<P><FONT size=+0><FONT face="Times New Roman">MDI</FONT>子窗口风格组中检测最大化或最小化<FONT
face="Times New Roman">; </FONT>还可以重载 <FONT face="Times New Roman">MDI Window
</FONT>的</FONT> </P>
<P><FONT size=+0><FONT face="Times New Roman">PreCreateWindow</FONT>函数,设置<FONT
face="Times New Roman">WS_MAXMIZE or WS_MINMIZE;</FONT></FONT> </P>
<P><FONT size=+0>如果从 <FONT face="Times New Roman">CMDIChildWnd </FONT>派生<FONT
face="Times New Roman">,</FONT>调用 <FONT
face="Times New Roman">OnInitialUpdate</FONT>函数中的 <FONT
face="Times New Roman">CWnd::Show</FONT></FONT> </P>
<P><FONT size=+0><FONT face="Times New Roman">Window</FONT>来指定 <FONT
face="Times New Roman">MDI Child Window</FONT>的 风格。</FONT> </P>
<P><A name=tip13></A><B><FONT size=+2><FONT face="Times New Roman">13.
</FONT><FONT face=黑体>如何使程序保持极小状态</FONT><FONT
face="Times New Roman">?</FONT></FONT></B> </P>
<P><FONT size=+0>很有意思的 问题</FONT> </P>
<P><FONT size=+0>这么办<FONT face="Times New Roman">: </FONT>在恢复程序窗体大小时, <FONT
face="Times New Roman">Windows</FONT>会发送<FONT
face="Times New Roman">WM_QUERY-OPEN</FONT>消息,</FONT> </P>
<P><FONT size=+0>用 <FONT face="Times New Roman">ClassWizard</FONT>设置成员函数 <FONT
face="Times New Roman">OnQueryOpen() ,add following code:</FONT></FONT> </P>
<P><FONT face="Times New Roman" size=+0>Bool CMainFrame:: OnQueryOpen( )</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>{</FONT> </P>
<P><FONT face="Times New Roman" size=+0>Return false;</FONT> </P>
<P><FONT face="Times New Roman" size=+0>}</FONT> </P>
<P><A name=tip14></A><B><FONT size=+2><FONT face="Times New Roman">14.
</FONT><FONT face=黑体>如何限制窗口的大小</FONT><FONT
face="Times New Roman">?</FONT></FONT></B> </P>
<P><FONT size=+0>也就是 <FONT face="Times New Roman">FixedDialog</FONT>形式。 <FONT
face="Times New Roman">Windows </FONT>发送 <FONT
face="Times New Roman">WM_GETMAXMININFO</FONT>消息来跟踪<FONT
face="Times New Roman">,</FONT></FONT> </P>
<P><FONT size=+0>响应它<FONT face="Times New Roman">,</FONT>在 <FONT
face="Times New Roman">OnGetMAXMININFO </FONT>中写代码<FONT
face="Times New Roman">:</FONT></FONT> </P>
<P><A name=tip15></A><B><FONT size=+2><FONT face="Times New Roman">15.
</FONT><FONT face=黑体>如何使窗口不可见?</FONT></FONT></B> </P>
<P><FONT size=+0>很简单<FONT face="Times New Roman">,</FONT>用<FONT
face="Times New Roman">SW_HIDE </FONT>隐藏窗口,可以结合 <FONT
face="Times New Roman">FindWindow,ShowWindow </FONT>控制<FONT
face="Times New Roman">.</FONT></FONT> </P>
<P><A name=tip16></A><B><FONT size=+2><FONT face="Times New Roman">16.
</FONT><FONT face=黑体>如何使窗口始终在最前方</FONT><FONT
face="Times New Roman">?</FONT></FONT></B> </P>
<P><FONT size=+0>两种途径<FONT face="Times New Roman">.</FONT></FONT> </P>
<P><FONT face="Times New Roman" size=+0>BringWindowToTop(Handle);</FONT> </P>
<P><FONT size=+0><FONT face="Times New Roman">SetWindowPos</FONT>函数,指定窗口的
最顶风格<FONT face="Times New Roman">,</FONT>用<FONT
face="Times New Roman">WS_EX_TOPMOST</FONT>扩展窗口的 风格</FONT> </P>
<P><FONT face="Times New Roman" size=+0>Example:</FONT> </P>
<P><FONT face="Times New Roman" size=+0>void ToggleTopMost( CWnd *pWnd)</FONT>
</P>
<P><FONT face="Times New Roman" size=+0>{</FONT> </P>
<P><FONT face="Times New Roman" size=+0>ASSERT_VALID(pWnd);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>pWnd ->SetWindowPos(pWnd->
GetStyle( ) &WS_EX_TOPMOST)?</FONT> </P>
<P><FONT face="Times New Roman" size=+0>&wndNoTopMOST:
&wndTopMost,0,0,0,0,SSP_NOSIZE|WSP_NOMOVE);</FONT> </P>
<P><FONT face="Times New Roman" size=+0>}</FONT> </P>
<HR>
<P> <A
href="http://www.vchome.net/tech/skill.htm">返回上页</A> </P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -