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

📄 vc编程指南68篇(4).txt

📁 VC编程指南68篇
💻 TXT
字号:
21、如何单击除了窗口标题栏以外的区域使窗口移动 
  
   当窗口需要确定鼠标位置时Windows向窗口发送WM_NCHITTEST信息,可以处理 
该信息使Windows认为鼠标在窗口标题上。对于对话框和基于对话的应用程序,可 
以使用ClassWizard处理该信息并调用基类函数, 如果函数返回HTCLIENT 则表明 
鼠标在客房区域,返回HTCAPTION表明鼠标在Windows的标题栏中。 
UINT CSampleDialog : : OnNcHitTest  (Cpoint point ) 

{ 
    UINT nHitTest =Cdialog: : OnNcHitTest (point ); 
    return (nHitTest = =HTCLIENT)? HTCAPTION : nHitTest ; 
} 
    上述技术有两点不利之处, 其一是在窗口的客户区域双击时, 窗口将极大; 
其二, 它不适合包含几个视窗的主框窗口。还有一种方法,当用户按下鼠标左键 
使主框窗口认为鼠标在其窗口标题上,使用ClassWizard在视窗中处理WM_LBUTTODOWN 
信息并向主框窗口发送一个WM_NCLBUTTONDOWN信息和一个单击测试HTCAPTION。 
void CSampleView : : OnLButtonDown  (UINT nFlags , Cpoint point ) 
{ 
    CView : : OnLButtonDow  (nFlags , pont ); 
  
    //Fool frame window into thinking somene clicked on 
    its caption bar . 
    GetParentFrame ( ) —> PostMessage ( 
         WM_NCLBUTTONDOWN , HTCAPTION , MAKELPARAM (poitn .x , point .y) ); 
} 
    该技术也适用于对话框和基于对的应用程序,只是不必调用CWnd : : GetParentFrame 。 
void CSampleDialog : : OnLbuttonDown (UINT nFlags, Cpoint point ) 
{ 
    Cdialog : : OnLButtonDow (nFlags, goint ); 
    //Fool dialog into thinking simeone clicked on its caption bar . 
  PostMessage (WM_NCLBUTTONDOWN , HTCAPTION , MAKELPARM (point.x , point. y ) ) 
} 
  
22、如何改变视窗的背景颜色 
  
    Windows向窗口发送一个WM_ERASEBKGND消息通知该窗口擦除背景,可以使用 
ClassWizard重载该消息的缺省处理程序来擦除背景(实际是画),并返回TRUE以 
防止Windows擦除窗口。 
//Paint area that needs to be erased. 
BOOL CSampleView : : OnEraseBkgnd (CDC* pDC) 
{ 
// Create a pruple brush. 
CBrush Brush (RGB (128 , 0 , 128) ); 
  
// Select the brush into the device context . 
CBrush* pOldBrush = pDC—>SelcetObject (&brush); 
  
// Get the area that needs to be erased . 
CRect reClip ; 
pDC—>GetCilpBox (&rcClip); 
  
//Paint the area. 
pDC—> PatBlt (rcClip.left , rcClip.top , 
        rcClip.Width ( ) , rcClip.Height ( ) , PATCOPY ); 
  
//Unselect brush out of device context . 
pDC—>SelectObject (pOldBrush ); 
  
// Return nonzero to half fruther processing . 
return TRUE; 
} 
  
23、如何改变窗口标题 
  
    调用CWnd : : SetWindowText可以改变任何窗口(包括控件)的标题。 
//Set title for application's main frame window . 
AfxGetMainWnd ( ) —> SetWindowText (_T("Application title") ); 
  
//Set title for View's MDI child frame window . 
GetParentFrame ( ) —> SetWindowText ("_T ("MDI Child Frame new title") ); 
  
//Set title for dialog's push button control. 
GetDigitem  (IDC_BUTTON) —> SetWindowText  (_T ("Button new title ") ); 
    如果需要经常修改窗口的标题(注:控件也是窗口),应该考虑使用半文档化 
的函数AfxSetWindowText。该函数在AFXPRIV.H中说明,在WINUTIL.CPP中实现,在 
联机帮助中找不到它,它在AFXPRIV.H中半文档化, 在以后发行的MFC中将文档化。 
AfxSetWindowText的实现如下: 
voik AFXAPI AfxSetWindowText (HWND hWndCtrl , LPCTSTR IpszNew ) 
{ 
    itn nNewLen= Istrlen (Ipaznew); 
    TCHAR szOld [256]; 
    //fast check to see if text really changes (reduces flash in the controls ) 
    if (nNewLen >_contof (szOld) || 
        : : GetWindowText (hWndCrtl , szOld , _countof (szOld) !=nNewLen || 
        Istrcmp (szOld , IpszNew )! = 0 
{ 
    //change it 
         : : SetWindowText (hWndCtrl , IpszNew ); 
    } 
} 
  
24、如何防止主框窗口在其说明中显示活动的文档名 
  
    创建主框窗口和MDI子窗口进通常具有FWS_ADDTOTITLE风格位, 如果不希望在 
说明中自动添加文档名, 必须禁止该风格位, 可以使用ClassWizard重置 
CWnd: : PreCreateWindow并关闭FWS_ADDTOTITLE风格。 
BOOL CMainFrame : : PreCreateWindow  (CREATESTRUCT&cs) 
{ 
    //Turn off  FWS_ADDTOTITLE in main frame . 
    cs.styel & = ~FWS_ADDTOTITLE ;  
    return CMDIFrameWnd : : PreCreateWindow (cs ); 
} 
    关闭MDI子窗口的FWS _ADDTOTITLE风格将创建一个具有空标题的窗口,可以调 
用CWnd: : SetWindowText来设置标题。记住自己设置标题时要遵循接口风格指南。 
  
25、如何获取有关窗口正在处理的当前消息的信息 
  

    调用CWnd: : GetCurrentMessage可以获取一个MSG指针。例如,可以使用 
ClassWizard将几个菜单项处理程序映射到一个函数中,然后调用GetCurrentMessage 
来确定所选中的菜单项。 
viod CMainFrame : : OnCommmonMenuHandler ( ) 
{ 
    //Display selected menu item in debug window . 
    TRACE ("Menu item %u was selected . \n" , 
                                 GetCruuentMessage ( ) —> wParam ); 
} 

⌨️ 快捷键说明

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