MFC设置对话框、静态文本框、编辑框的背景及文本

来源:互联网 发布:mysql primary 编辑:程序博客网 时间:2024/05/16 07:30

1、对对话框来说,它上面的每一个控件在绘制时都要向它发送WM_CTLCOLOR消息。

会为每一个控件准备一个DC,该DC将通过pDC参数传递给OnCtlColor函数。该函数将被多次调用。

2、在构造函数中创建一个画刷。

3、在OnCtlColor函数通过ID判断是哪一个控件发送WM_CTLCOLOR消息。////在当前函数创建画刷,总是出问题

CBrush brush;CFont font;CFacadeDlg::CFacadeDlg(CWnd* pParent /*=NULL*/): CDialog(CFacadeDlg::IDD, pParent){//{{AFX_DATA_INIT(CFacadeDlg)// NOTE: the ClassWizard will add member initialization here//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);//创建画刷brush.CreateSolidBrush(RGB(255, 0, 0));//创建文字字体font.CreatePointFont(200, "华文行楷");}


HBRUSH CFacadeDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) {HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);// TODO: Change any attributes of the DC here// TODO: Return a different brush if the default is not desiredif (pWnd->GetDlgCtrlID() ==IDC_HAHA)//静态文本框和编辑框设置方法一样。 按钮不能这样更改//判断是哪个控件{pDC->SetTextColor(RGB(0,0, 255));//设置静态文本框的字体颜色pDC->SetBkMode(TRANSPARENT);//设置文字的背景为透明pDC->SelectObject(&font);//设置文字字体return brush;//设置对话框的背景颜色}return hbr;}





原创粉丝点击