CEdit編輯框字體和背景設置
注意事項:當CEdit為“disable”時,設置編輯框的字體和背景會沒有效果。
解決方案:將CEdit的Style設置為“readonly”,這樣設置就能生效了,同時也能達到禁用編輯的功能。
通過發送“WM_CTLCOLOR”消息,調用HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)來對編輯框字體和背景進行設置。
參數nCtlColor用於指定控件的類型,可以是:
.CTLCOLOR_BTN 按鈕控件 //(注意check box 屬於button控件)
.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); // 設置背景色
}
【注】
BLACK_BRUSH:黑色
WHITE_BRUSH:白色
GRAY_BRUSH:灰色
NULL_BRUSH:透明
HOLLOW_BRUSH :透明