VC++编程常见问题解答五 26、如何创建一个不规则形状的窗口 可以使用新的SDK函数SetWindowRgn。该函数将绘画和鼠标消息限定在窗口的 一个指定的区域,实际上使窗口成为指定的不规则形状。 使用AppWizard创建一个基于对的应用程序并使用资源编辑器从主对话资源中 删除所在的缺省控件、标题以及边界。 给对话类增加一个CRgn数据成员,以后要使用该数据成员建立窗口区域。 Class CRoundDlg : public CDialog { … private : Crgn m_rgn : // window region … } ; 修改OnInitDialog函数建立一个椭圆区域并调用SetWindowRgn将该区域分配给窗口: BOOL CRoundDlg : : OnInitDialog ( ) { CDialog : : OnInitDialog ( ) ; //Get size of dialog . CRect rcDialog ; GetClientRect (rcDialog ); // Create region and assign to window . m_rgn . CreateEllipticRgn (0 , 0 , rcDialog.Width ( ) , rcDialog .Height ( ) ); SetWindowRgn (GetSafeHwnd ( ) , (HRGN) m_ rgn , TRUE ); return TRUE ; } 通过建立区域和调用SetWindowRgn,已经建立一个不规则形状的窗口,下面 的例子程序是修改OnPaint函数使窗口形状看起来象一个球形体。 voik CRoundDlg : : OnPaint ( ) { CPaintDC de (this) ; // device context for painting . //draw ellipse with out any border dc. SelecStockObject (NULL_PEN); //get the RGB colour components of the sphere color COLORREF color= RGB( 0 , 0 , 255); BYTE byRed =GetRValue (color); BYTE byGreen = GetGValue (color); BYTE byBlue = GetBValue (color); // get the size of the view window Crect rect ; GetClientRect (rect); // get minimun number of units int nUnits =min (rect.right , rect.bottom ); //calculate he horiaontal and vertical step size float fltStepHorz = (float) rect.right /nUnits ; float fltStepVert = (float) rect.bottom /nUnits ; int nEllipse = nUnits/3; // calculate how many to draw int nIndex ; // current ellipse that is being draw CBrush brush ; // bursh used for ellipse fill color CBrush *pBrushOld; // previous brush that was selected into dc //draw ellipse , gradually moving towards upper-right corner for (nIndex = 0 ; nIndes < + nEllipse ; nIndes ++) { //creat solid brush brush . CreatSolidBrush (RGB ( ( (nIndex *byRed ) /nEllipse ). ( ( nIndex * byGreen ) /nEllipse ), ( (nIndex * byBlue) /nEllipse ) ) ); //select brush into dc pBrushOld= dc .SelectObject (&brhsh); //draw ellipse dc .Ellipse ( (int) fltStepHorz * 2, (int) fltStepVert * nIndex , rect. right -( (int) fltStepHorz * nIndex )+ 1, rect . bottom -( (int) fltStepVert * (nIndex *2) ) +1) ; //delete the brush brush.DelecteObject ( ); } } 最后,处理WM_NCHITTEST消息,使当击打窗口的任何位置时能移动窗口。 UINT CRoundDlg : : OnNchitTest (Cpoint point ) { //Let user move window by clickign anywhere on the window . UINT nHitTest = CDialog : : OnNcHitTest (point) ; rerurn (nHitTest = = HTCLIENT)? HTCAPTION: nHitTest ; } 27、如何在代码中获取工具条和状态条的指针 缺省时, 工作框创建状态条和工具条时将它们作为主框窗口的子窗口,状态 条有一个AFX_IDW_STATUS_BAR标识符,工具条有一个AFX_IDW_TOOLBAR标识符,下 例说明了如何通过一起调用CWnd: : GetDescendantWindow和AfxGetMainWnd来获 取这些子窗口的指针: //Get pointer to status bar . CStatusBar * pStatusBar = (CStatusBar *) AfxGetMainWnd ( ) —> GetDescendantWindow (AFX_IDW_STUTUS_BAR); //Get pointer to toolbar . CToolBar * pToolBar = (CToolBar * ) AfxGetMainWnd ( ) —> GetDescendantWindow (AFX_IDW_TOOLBAR); 28、如何使能和禁止工具条的工具提示 如果设置了CBRS_TOOLTIPS风格位,工具条将显示工具提示,要使能或者禁止 工具提示,需要设置或者清除该风格位。下例通过调用CControlBar : : GetBarStyle 和CControlBar : : SetBarStyle建立一个完成此功能的成员函数: void CMainFrame : : EnableToolTips ( BOOL bDisplayTips ) { ASSERT_VALID (m_wndToolBar); DWORD dwStyle = m _wndToolBar.GetBarStyle ( ) ; if (bDisplayTips) dwStyle |=CBRS_TOOLTIPS ; else dwStyle & = ~ CBRS_TOOLTIPS ; m_wndToolBar.SetBarStyle (dwStyle ); } 29、如何设置工具条标题 工具条是一个窗口,所以可以在调用CWnd : : SetWindowText来设置标题,例子如下: int CMainFrame : : OnCreate (LPCREATESTRUCT lpCreateStruct ) { … // Set the caption of the toolbar . m_wndToolBar.SetWindowText (_T "Standdard"); 30、如何创建和使用无模式对话框 MFC将模式和无模式对话封装在同一个类中,但是使用无模式对话需要几个对 话需要几个额处的步骤。首先,使用资源编辑器建立对话资源并使用ClassWizard 创建一个CDialog的派生类。模式和无模式对话的中止是不一样的: 模式对话通过调用CDialog : : EndDialog 来中止,无模式对话则是调用CWnd: : DestroyWindow来中止的,函数CDialog : : OnOK和CDialog : : OnCancel调用 EndDialog ,所以需要调用DestroyWindow并重置无模式对话的函数。 void CSampleDialog : : OnOK ( ) { // Retrieve and validate dialog data . if (! UpdateData (TRUE) ) { // the UpdateData rountine will set focus to correct item TRACEO (" UpdateData failed during dialog termination .\n") ; return ; } //Call DestroyWindow instead of EndDialog . DestroyWindow ( ) ; } void CSampleDialog : : OnCancel ( ) { //Call DestroyWindow instead of EndDialog . DestroyWindow ( ) ; } 其次,需要正确删除表示对话的C++对象。对于模式对来说,这很容易,需要 创建函数返回后即可删除C++对象;无模式对话不是同步的,创建函数调用后立即 返回,因而用户不知道何时删除C++对象。撤销窗口时工作框调用CWnd : : PostNcDestroy, 可以重置该函数并执行清除操作,诸如删除this指针。 void CSampleDialog : : PostNcDestroy ( ) { // Declete the C++ object that represents this dialog . delete this ; } 最后,要创建无模式对话。可以调用CDialog : : DoModal创建一个模式对放 ,要创建一个无模式对话则要调用CDialog: : Create。下面的例子说明 了应用 程序是如何创建无模式对话的: void CMainFrame : : OnSampleDialog ( ) { //Allocate a modeless dialog object . CSampleDilog * pDialog =new CSampleDialog ; ASSERT_VALID (pDialog) ; //Create the modeless dialog . BOOL bResult = pDialog —> Creste (IDD_IDALOG) ; ASSERT (bResult ) ; }