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

📄 vc编程常用捷径(3).txt

📁 vc编程常见的小技巧
💻 TXT
字号:
32、如何改变对话或窗体视窗的背景颜色

调用CWinApp : : SetDialogBkColor可以改变所有应用程序的背景颜色。第
一个参数指定了背景颜色,第二个参数指定了文本颜色。下例将应用程序对话设置
为蓝色背景和黄色文本。
BOOL CSampleApp : : InitInstance ( )
{

//use blue dialog with yellow text .
SetDialogBkColor (RGB (0, 0, 255 ), RGB ( 255 , 255 , 0 ) ) ;

}
需要重画对话(或对话的子控件)时,Windows向对话发送消息WM_CTLCOLOR,
通常用户可以让Windows选择绘画背景的刷子,也可重置该消息指定刷子。下例说
明了创建一个红色背景对话的步骤。
首先,给对话基类增加一人成员变量CBursh :
class CMyFormView : public CFormView
{

private :
CBrush m_ brush ; // background brush

} ;
其次, 在类的构造函数中将刷子初始化为所需要的背景颜色。
CMyFormView : : CMyFormView ( )
{
// Initialize background brush .
m_brush .CreateSolidBrush (RGB ( 0, 0, 255 ) )
}
最后,使用ClassWizard处理WM_CTLCOLOR消息并返回一个用来绘画对话背景的
刷子句柄。注意:由于当重画对话控件时也要调用该函数,所以要检测nCtlColor
参量。
HBRUSH CMyFormView : : OnCtlColor (CDC* pDC , CWnd*pWnd , UINT nCtlColor )
{
// Determine if drawing a dialog box . If we are , return +handle to
//our own background brush . Otherwise let windows handle it .
if (nCtlColor = = CTLCOLOR _ DLG )
return (HBRUSH) m_brush .GetSafeHandle ( ) ;

return CFormView : : OnCtlColor (pDC, pWnd , nCtlColor );
}
33、如何获取一个对话控件的指针

有两种方法。其一,调用CWnd: : GetDlgItem,获取一个CWnd*指针调用成
员函数。下例调用GetDlgItem,将返回值传给一个CSpinButtonCtrl*以便调用
CSpinButtonCtrl : : SetPos 函数:
BOOL CSampleDialog : : OnInitDialog ( )
{
CDialog : : OnInitDialog ( ) ;

//Get pointer to spin button .
CSpinButtonCtrl * pSpin - ( CSpinButtonCtrl *) GetDlgItem (IDC_SPIN) ;
ASSERT _ VALID (pSpin) ;

//Set spin button's default position .
pSpin —> SetPos (10) ;

return TRUE ;
}
其二, 可以使用ClassWizard将控件和成员变量联系起来。在ClassWizard中简
单地选择Member Variables标签,然后选择Add Variable …按钮。如果在对话资源
编辑器中,按下Ctrl键并双击控件即可转到Add Member Variable对话。
34、如何禁止和使能控件

控件也是窗口,所以可以调用CWnd : : EnableWindow使能和禁止控件。
//Disable button controls .
m_wndOK.EnableWindow (FALSE ) ;
m_wndApply.EnableWindow (FALSE ) ;

35、如何改变控件的字体

由于控件是也是窗口,用户可以调用CWnd: : SetFont指定新字体。该函数用
一个Cfont指针,要保证在控件撤消之前不能撤消字体对象。下例将下压按钮的字
体改为8点Arial字体:
//Declare font object in class declaration (.H file ).
private :
Cfont m_font ;
// Set font in class implementation (.Cpp file ). Note m_wndButton is a
//member variable added by ClassWizard.DDX routines hook the member
//variable to a dialog button contrlo.
BOOL CSampleDialog : : OnInitDialog ( )
{


//Create an 8-point Arial font
m_font . CreateFont (MulDiv (8 , -pDC—> GetDeviceCaps (LOGPIXELSY) , 72).
0 , 0 , 0 , FW_NORMAL , 0 , 0, 0, ANSI_CHARSER, OUT_STROKE_PRECIS ,
CLIP_STROKE _PRECIS , DRAFT _QUALITY
VARIABLE_PITCH |FF_SWISS, _T ("Arial") );

//Set font for push button .
m_wndButton . SetFont (&m _font );

}

36、如何在OLE控件中使用OLE_COLOR数据类型

诸如COleControl : : GetFortColor和COleControl : : GetBacalog : : 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 ( )
{

// 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 ) ;
}
37、如何访问桌面窗口
静态函数CWnd:: GetDesktopWindow 返回桌面窗口的指针。下例说明了MFC
void CFrameWnd::BeginModalState ()
{
//first count all windows that need to be disabled
UINT nCount=0;
HWND hWnd=:: GetWindow (:: GetDesktopWindow (), GW_CHILD);
while (hWnd!=NULL)
{
if (:: IsWindowEnabled (hwnd) &&
CWnd::FromHandlePermanent (hWnd)!=NULL &&
AfxIsDescendant (pParent->m_hWnd, hWnd) &&
:: SendMessage (hWnd, WM_DISABLEMODAL, 0, 0)==0)
{
++nCount;
}
hWnd=:: GetWindow (hWnd, GW_HWNDNEXT);
}

38、如何确定Windows和Windows系统目录
有两个SDK函数可以完成该功能。GetWindowsDirectory和GetSystemDirectory,
TCHAR szDir [MAX_PATH];
//Get the full path of the windows directory.
:: GetWindowsDirectory (szDir, MAX_PATH);
TRACE ("Windows directory %s\n", szDir);
//Get the full path of the windows system directory.
:: GetSystemDirectory (szDir, MAX_PATH);
:: GetSystemDirectory (szDir, MAX_PATH);
调用SDK函数GetTemPath可以确定临时文件的目录,该函数首先为临时路径
检测TMP环境变量:如果没有指定TMP,检测TMP环境变量,然后返回到当前目录。
下例说明了如何创建一个临时文件。
//get unique temporary file.
CString strFile;
TRY
{
//Create file and write data.Note that file is closed
//in the destructor of the CFile object.
//write data
CATCH (CFileException, e)
CATCH (CFileException, e)
{
//error opening file
}
END_CATCH
Void GetuniqueTempName (CString& strTempName)
{
//Get the temporary files directory.
TCHAR szTempPath [MAX_PATH];
DWORD dwResult=:: GetTempPath (MAX_PATH, szTempPath);
//Create a unique temporary file.
TCHAR szTempFile [MAX_PATH];
UINT nResult=GetTempFileName (szTempPath, _T ("~ex"),0,szTempfile);
strTempName=szTempFile;
}
39.如何检索原先的Task Manager应用程序使用的任务列表
原先的Task Manager应用程序显示顶层窗口的列表。为了显示该列表,窗口
必须可见、包含一个标题以及不能被其他窗口拥有。调用CWnd:: GetWindow可以
检索顶层窗口的列表,调用IsWindowVisible、GetWindowTextLength以及GetOwner
void GetTadkList (CListBox&list)
{
//Get first Window in window list.
ASSERT_VALID (AfxGetMainWnd ());
//Walk window list.
while (pWnd)
{
// I window visible, has a caption, and does not have an owner?
if (pWnd ->IsWindowVisible () &&
if (pWnd ->IsWindowVisible () &&
pWnd ->GetWindowTextLength () &&! pWnd ->GetOwner ())
{
//Add caption o window to list box.
pWnd ->GetWindowText (strCaption);
list.AddString (strCaption);
}
//Get next window in window list.
pWnd=pWnd->GetWindow (GW_HWNDNEXT);
}
40.如何使用一个预定义的Windows光标
调用CWinApp:: LoadStandardCursor并传送光标标识符。
BOOL CSampleDialog:: OnSetCursor (CWnd* pWnd, UINT nHitTest, UINT message)
{
//Display wait cursor if busy.
if (m_bBusy)
{
SetCursor (AfxGetApp () ->LoadStandardCursor (IDC_WAIT));
return TRUE;
return CDialog:: OnSetCursor (pWnd. nHitTest,message);
调用SDK函数GetSystemMetrics,该函数可以检索有关windows显示信息,诸如
//Initialize CSize object with screen size.
CSize sizeScreen (GetSystemMetrics (SM_CXSCREEN))

⌨️ 快捷键说明

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