设置对话框背景(总结)

来源:互联网 发布:淘宝零食推荐 知乎 编辑:程序博客网 时间:2024/06/17 02:32


方法一:调用CWinApp类的成员函数SetDialogBkColor来实现。
---- 其中函数的第一个参数指定了背景颜色,第二个参数指定了文本颜色。下面的例子是将应用程序对话 框设置为蓝色背景和红色文本,步骤如下:
---- ① 新建一个基于Dialog的MFC AppWizard应用程序ExampleDlg。
---- ② 在CExampleDlgApp ::InitInstance()中添加如下代码:

BOOL CExampleDlgApp: : InitInstance ( ){…    CExampleDlgDlg dlg;    m_pMainWnd = &dlg;//先于DoModal()调用,将对话框设置为蓝色背景、红色文本    SetDialogBkColor(RGB(0,0,255),RGB(255,0,0));    int nResponse = dlg.DoModal();…}

---- 编译并运行,此时对话框的背景色和文本色已发生了改变。值得注意的是:在调用DoModal()之前必须 先调用SetDialogBkColor,且此方法是将改变应用程序中所有的对话框颜色,并不能针对某一个指定的对 话框

方法二:重载OnPaint(),即WM_PAINT消息

void CExampleDlgDlg::OnPaint(){    if (IsIconic())…   else   {        CRect rect;        CPaintDC dc(this);        GetClientRect(rect);        dc.FillSolidRect(rect,RGB(0,255,0));  //设置为绿色背景        CDialog::OnPaint();   }
方法三:重载WM_CTLCOLOR响应函数
HBRUSH   CDlgTest::OnCtlColor(CDC*   pDC,   CWnd*   pWnd,   UINT   nCtlColor)       {     //HBRUSH   hbr   =   CDialog::OnCtlColor(pDC,   pWnd,   nCtlColor);         //   TODO:   Change   any   attributes   of   the   DC   here     switch   (nCtlColor)     {     case   CTLCOLOR_DLG:     {         CBrush*           back_brush;         COLORREF         color;         color   =   RGB(255,255,255);         back_brush   =   new   CBrush(color);         return   (HBRUSH)   (back_brush->m_hObject);     }     }     return(CDialog::OnCtlColor(pDC,   pWnd,   nCtlColor));     //   TODO:   Return   a   different   brush   if   the   default   is   not   desired     //return   hbr;     } 





原创粉丝点击