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

📄 窗口控制.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">如何实现一个橡皮区矩形 </a></font></td></tr><tr><td><p></Br>CRectTracker是一个很有用的类,可以通过调用CRectTracker:: TrackRubberBand响应WM_LBUTTONDOWN消息来创建一个橡皮区矩形。下例表明使用CRectTracker移动和重置视窗中的蓝色椭圆的大小是很容易的事情。<Br></Br>首先,在文件档中声明一个CRectTracker数据成员:<Br></Br>class CSampleView : Public CView<Br></Br>{<Br></Br>…<Br></Br>public :<Br></Br>CrectTracker m_tracker;<Br></Br>…<Br></Br>};<Br></Br>其次,在文档类的构造函数中初始化CRectTracker 对象:<Br></Br>CSampleDoc:: CSampleDOC ()<Br></Br>{<Br></Br>//Initialize tracker position, size and style.<Br></Br>m_tracker.m_rect.SetRect (0, 0, 10, 10);<Br></Br>m_tracker.m_nStyle=CRectTracker:: resizeInside |<Br></Br>CRectTracker:: dottedLine;<Br></Br>}<Br></Br>然后,在OnDraw函数中画椭圆和踪迹矩形:<Br></Br>void CSampleView:: OnDraw (CDC* pDC)<Br></Br>{<Br></Br>CSampleDoc* pDoc=GetDocument ();<Br></Br>ASSERT_VALID (pDoc);<Br></Br>//Select blue brush into device context.<Br></Br>CBrush brush (RGB (0, 0, 255));<Br></Br>CBrush* pOldBrush=pDC->SelectObject (&brush);<Br></Br>//draw ellipse in tracking rectangle.<Br></Br>Crect rcEllipse;<Br></Br>pDoc->m_tracker.GetTrueRect (rcEllipse);<Br></Br>pDC->Ellipse (rcEllipse);<Br></Br>//Draw tracking rectangle.<Br></Br>pDoc->m_tracker.Draw (pDC);<Br></Br>//Select blue brush out of device context.<Br></Br>pDC->Selectobject (pOldBrush);<Br></Br>}<Br></Br>最后,使用ClassWizard处理WM_LBUTTONDOWN消息,并增加下述代码。该段代码根据鼠标击键情况可以拖放、移动或者重置椭圆的大小。<Br></Br>void CSampleView::OnLButtonDown (UINT nFlags, CPoint point)<Br></Br>{<Br></Br>//Get pointer to document.<Br></Br>CSampleDoc* pDoc=GetDocument ();<Br></Br>ASSERT_VALID (pDoc);<Br></Br>//If clicked on ellipse, drag or resize it. Otherwise create a<Br></Br>//rubber-band rectangle nd create a new ellipse.<Br></Br>BOOL bResult=pDoc->m_tracker.HitTest (point)!=<Br></Br>CRectTracker::hitNothing;<Br></Br>//Tracker rectangle changed so update views.<Br></Br>if (bResult)<Br></Br>{<Br></Br>pDoc->m_tracker.Track (this,point,TRue);<Br></Br>pDoc->SetModifiedFlag ();<Br></Br>pDoc->UpdateAllViews (NULL);<Br></Br>}<Br></Br>else<Br></Br>pDoc->m-tracker.TrackRubberBand (this,point,TRUE);<Br></Br>CView:: onLButtonDown (nFlags,point);<Br></Br>}<Br></Br></p></td></tr><tr><td><font color="0000FF"><a name="2">如何改变窗口的缺省风格? </a></font></td></tr><tr><td><p></Br>重栽 CWnd:: PreCreateWindow 并修改CREATESTRUCT结构来指定窗口风格和其他创建信息.<Br></Br>Example: Delete "Max" Button and Set Original Window's Position and Size<Br></Br>BOOL CMainFrame:: PreCreateWindow (CREATESTRUCT &cs)<Br></Br>{<Br></Br>cs.style &=~WS_MAXINIZEMOX;<Br></Br>cs.x=cs.y=0;<Br></Br>cs.cx=GetSystemMetrics(SM_CXSCREEN/2);<Br></Br>cs.cy=GetSystemMetrics(SM_CYSCREEN/2);<Br></Br>return CMDIFramewnd ::PreCreateWindow(cs);<Br></Br>}<Br></Br></p></td></tr><tr><td><font color="0000FF"><a name="3">如何将窗口居中显示? </a></font></td></tr><tr><td><p></Br>Easy, Call Function CWnd:: Center Windows<Br></Br>Example(1): Center Window( ); //Relative to it's parent<Br></Br>// Relative to Screen<Br></Br>Example(2): Center Window(CWnd:: GetDesktopWindow( ));<Br></Br>//Relative to Application's MainWindow<Br></Br>AfxGetMainWnd( ) -> Center Window( );<Br></Br></p></td></tr><tr><td><font color="0000FF"><a name="4">如何让窗口和 MDI窗口一启动就最大化和最小化? </a></font></td></tr><tr><td><p></Br>先说窗口。<Br></Br>在 InitStance 函数中设定 m_nCmdShow的 取值.<Br></Br>m_nCmdShow=SW_SHOWMAXMIZED ; //最大化<Br></Br>m_nCmdShow=SW_SHOWMINMIZED ; //最小化<Br></Br>m_nCmdShow=SW_SHOWNORMAL ; //正常方式<Br></Br>MDI窗口:<Br></Br>如果是创建新的应用程序,可以用 MFC AppWizard 的Advanced 按钮并在<Br></Br>MDI子窗口风格组中检测最大化或最小化; 还可以重载 MDI Window 的<Br></Br>PreCreateWindow函数,设置WS_MAXMIZE or WS_MINMIZE;<Br></Br>如果从 CMDIChildWnd 派生,调用 OnInitialUpdate函数中的 CWnd::Show<Br></Br>Window来指定 MDI Child Window的 风格。<Br></Br></p></td></tr><tr><td><font color="0000FF"><a name="5">如何使程序保持极小状态? </a></font></td></tr><tr><td><p></Br>很有意思的 问题<Br></Br>这么办: 在恢复程序窗体大小时, Windows会发送WM_QUERY-OPEN消息,<Br></Br>用 ClassWizard设置成员函数 OnQueryOpen() ,add following code:<Br></Br>Bool CMainFrame:: OnQueryOpen( )<Br></Br>{<Br></Br>Return false;<Br></Br>}<Br></Br></p></td></tr><tr><td><font color="0000FF"><a name="6">如何限制窗口的大小? </a></font></td></tr><tr><td><p></Br>也就是 FixedDialog形式。 Windows 发送 WM_GETMAXMININFO消息来跟踪,<Br></Br>响应它,在 OnGetMAXMININFO 中写代码:<Br></Br></p></td></tr><tr><td><font color="0000FF"><a name="7">如何使窗口不可见? </a></font></td></tr><tr><td><p></Br>很简单,用SW_HIDE 隐藏窗口,可以结合 FindWindow,ShowWindow 控制.<Br></Br></p></td></tr><tr><td><font color="0000FF"><a name="8">如何使窗口始终在最前方? </a></font></td></tr><tr><td><p></Br>两种途径.<Br></Br>BringWindowToTop(Handle);<Br></Br>SetWindowPos函数,指定窗口的最顶风格,用WS_EX_TOPMOST扩展窗口的风格<Br></Br>Example:<Br></Br>void ToggleTopMost( CWnd *pWnd)<Br></Br>{<Br></Br>ASSERT_VALID(pWnd);<Br></Br>pWnd ->SetWindowPos(pWnd-> GetStyle( ) &WS_EX_TOPMOST)?<Br></Br>&wndNoTopMOST: &wndTopMost,0,0,0,0,SSP_NOSIZE|WSP_NOMOVE);<Br></Br>}<Br></Br></Br></p></td></tr><tr><td><font color="0000FF"><a name="9">移动窗口 </a></font></td></tr><tr><td><p></Br>调用CWnd : : SetWindowPos并指定SWP_NOSIZE标志。目的位置与父窗口<Br></Br>有关(顶层窗口与屏幕有关)。调用CWnd : : MoveWindow时必须要指定窗口<Br></Br>的大小。<Br></Br>//Move window to positoin 100 , 100 of its parent window .<Br></Br>SetWindowPos (NULL, 100 , 100 , 0 , 0 , SWP_NOSIZE |SWP_NOAORDER);<Br></Br></p></td></tr><tr><td><font color="0000FF"><a name="10">重置窗口的大小 </a></font></td></tr><tr><td><p></Br>调用CWnd: : SetWindowPos并指定SWP_NOMOVE标志,也可调用<Br></Br>CWnd : : MoveWindow 但必须指定窗口的位置。<Br></Br>// Get the size of the window .<Br></Br>Crect reWindow ;<Br></Br>GetWindowRect (reWindow );<Br></Br>//Make the window twice as wide and twice as tall .<Br></Br>SetWindowPos (NULL , 0 , 0 , reWindow . Width ( ) *2,<Br></Br>reWindow . Height () * 2,<Br></Br>SWP_NOMOVE |SWP_NOZORDER );<Br></Br></p></td></tr><tr><td><font color="0000FF"><a name="11">如何单击除了窗口标题栏以外的区域使窗口移动 </a></font></td></tr><tr><td><p></Br>当窗口需要确定鼠标位置时Windows向窗口发送WM_NCHITTEST信息,可以处理<Br></Br>该信息使Windows认为鼠标在窗口标题上。对于对话框和基于对话的应用程序,可<Br></Br>以使用ClassWizard处理该信息并调用基类函数,如果函数返回HTCLIENT 则表明<Br></Br>鼠标在客房区域,返回HTCAPTION表明鼠标在Windows的标题栏中。<Br></Br>UINT CSampleDialog : : OnNcHitTest (Cpoint point )<Br></Br>{<Br></Br>UINT nHitTest =Cdialog: : OnNcHitTest (point );<Br></Br>return (nHitTest = =HTCLIENT)? HTCAPTION : nHitTest ;<Br></Br>}<Br></Br>上述技术有两点不利之处, 其一是在窗口的客户区域双击时,窗口将极大;<Br></Br>其二, 它不适合包含几个视窗的主框窗口。还有一种方法,当用户按下鼠标左键<Br></Br>使主框窗口认为鼠标在其窗口标题上,使用ClassWizard在视窗中处理WM_LBUTTODOWN<Br></Br>信息并向主框窗口发送一个WM_NCLBUTTONDOWN信息和一个单击测试HTCAPTION。<Br></Br>void CSampleView : : OnLButtonDown (UINT nFlags , Cpoint point )<Br></Br>{<Br></Br>CView : : OnLButtonDow (nFlags , pont );<Br></Br>//Fool frame window into thinking somene clicked on<Br></Br>its caption bar .<Br></Br>GetParentFrame ( ) —> PostMessage (<Br></Br>WM_NCLBUTTONDOWN , HTCAPTION , MAKELPARAM (poitn .x , point .y) );<Br></Br>}<Br></Br>该技术也适用于对话框和基于对的应用程序,只是不必调用CWnd : : GetParentFrame 。<Br></Br>void CSampleDialog : : OnLbuttonDown (UINT nFlags, Cpoint point )<Br></Br>{<Br></Br>Cdialog : : OnLButtonDow (nFlags, goint );<Br></Br>//Fool dialog into thinking simeone clicked on its caption bar .<Br></Br>PostMessage (WM_NCLBUTTONDOWN , HTCAPTION , MAKELPARM (point.x , point. y ) )<Br></Br>}<Br></p></td></tr><tr><td><font color="0000FF"><a name="12">如何改变窗口标题 </a></font></td></tr><tr><td><p></Br>调用CWnd : : SetWindowText可以改变任何窗口(包括控件)的标题。<Br></Br>//Set title for application's main frame window .<Br></Br>AfxGetMainWnd ( ) —> SetWindowText (_T("Application title") );<Br></Br>//Set title for View's MDI child frame window .<Br></Br>GetParentFrame ( ) —> SetWindowText ("_T ("MDI Child Frame new title") );<Br></Br>//Set title for dialog's push button control.<Br></Br>GetDigitem (IDC_BUTTON) —> SetWindowText (_T ("Button new title ") );<Br></Br>如果需要经常修改窗口的标题(注:控件也是窗口),应该考虑使用半文档化<Br></Br>的函数AfxSetWindowText。该函数在AFXPRIV.H中说明,在WINUTIL.CPP中实现,在<Br></Br>联机帮助中找不到它,它在AFXPRIV.H中半文档化,在以后发行的MFC中将文档化。<Br></Br>AfxSetWindowText的实现如下:<Br></Br>voik AFXAPI AfxSetWindowText (HWND hWndCtrl , LPCTSTR IpszNew )<Br></Br>{<Br></Br>itn nNewLen= Istrlen (Ipaznew);<Br></Br>TCHAR szOld [256];<Br></Br>//fast check to see if text really changes (reduces flash in the controls )<Br></Br>if (nNewLen >_contof (szOld) ||<Br></Br>: : GetWindowText (hWndCrtl , szOld , _countof (szOld) !=nNewLen ||<Br></Br>Istrcmp (szOld , IpszNew )! = 0<Br></Br>{<Br></Br>//change it<Br></Br>: : SetWindowText (hWndCtrl , IpszNew );<Br></Br>}<Br></Br>}<Br></Br></p></td></tr><tr><td><font color="0000FF"><a name="13">如何防止主框窗口在其说明中显示活动的文档名 </a></font></td></tr><tr><td><p></Br>创建主框窗口和MDI子窗口进通常具有FWS_ADDTOTITLE风格位,如果不希望在<Br></Br>说明中自动添加文档名, 必须禁止该风格位, 可以使用ClassWizard重置<Br>

⌨️ 快捷键说明

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