VC 使用OnCtlColor函數來改變控件顏色(引用)

在MFC類庫提供了CWnd::OnCtlColor函數,在工做框架的子窗口被重畫時將調用該成員函數.所以能夠重載WM_CTLCOLOR消息的響應函數.此函數的原型:
afx_msg HBRUSH OnCtlColor(CDC *pDC,CWnd *pWnd,UINT nCtlColor);
參數nCtlColor用於指定控件的類型,能夠是:
.CTLCOLOR_BTN 按鈕控件
.CTLCOLOR_DLG 對話框
.CTLCOLOR_EDIT 編輯框
.CTLCOLOR_LISTBOX 列表控件
.CTLCOLOR_MSGBOX 消息控件
.CTLCOLOR_SCROLLBAR 滾動條控件
.CTLCOLOR_STATIC 靜態控件

[程序實現]
假設你已有了名爲My的對話框工程.你有了一個STATIC的控件,ID爲IDC_STATIC1.
框架

HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if (nCtlColor==CTLCOLOR_STATIC)

{
pDC-> SetTextColor(RGB(255,0,0)); //字體顏色
pDC-> SetBkColor(RGB(0, 0, 255)); //字體背景色 

}
// TODO: Return a different brush if the default is not desired
return hbr;
}
若是要指定某個特定控件能夠這樣寫: ID爲IDC_STATIC1
if (pWnd-> GetDlgCtrlID()==IDC_STATIC1)
{
pDC-> SetTextColor(RGB(255,0,0));   //設置字體顏色
pDC-> SetBkMode(TRANSPARENT); //設置字體背景爲透明
// TODO: Return a different brush if the default is not desired
return (HBRUSH)::GetStockObject(BLACK_BRUSH);   // 設置背景色
}
else
return hbr;

【注】 函數

BLACK_BRUSH:黑色 字體

WHITE_BRUSH:白色 spa

GRAY_BRUSH:灰色 code

NULL_BRUSH:透明 blog

HOLLOW_BRUSH :透明 原型

相關文章
相關標籤/搜索