简体   繁体   English

更改对话框的背景颜色 mfc

[英]change the background color of a dialog box mfc

I'm trying to change the background color of a dialog box (win 7, vs2010,c++).我正在尝试更改对话框的背景颜色(win 7、vs2010、c++)。 I tried to catch WM_CTLCOLOR ,WM_ERASEBKGND and change the color.我试图捕捉 WM_CTLCOLOR ,WM_ERASEBKGND 并改变颜色。 I manged to change in this way the backgroung color, but when the window is finish to upload itself, the color is back to default but I noticed that the frame is in the right color.我设法以这种方式更改背景颜色,但是当窗口完成上传时,颜色恢复为默认值,但我注意到框架的颜色正确。 I think that I'm changing the window and not the dialog box or something like that.我认为我正在更改窗口而不是对话框或类似的东西。 I'm doing this with WTL (not AFX).我是用 WTL(不是 AFX)做的。

What should I do?我该怎么办?

Try this:尝试这个:

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
    CAboutDlg();

// Dialog Data
    //{{AFX_DATA(CAboutDlg)
    enum { IDD = IDD_ABOUTBOX };
    //}}AFX_DATA

    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAboutDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL

// Implementation
protected:
    //{{AFX_MSG(CAboutDlg)
    //}}AFX_MSG
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
    DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
    //{{AFX_DATA_INIT(CAboutDlg)
    //}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CAboutDlg)
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    //{{AFX_MSG_MAP(CAboutDlg)
    ON_WM_ERASEBKGND()
    //}}AFX_MSG_MAP

END_MESSAGE_MAP()




BOOL CAboutDlg::OnEraseBkgnd(CDC* pDC)
{
    CRect rect;
    GetClientRect(&rect);
    CBrush myBrush(RGB(255, 255, 255));    // dialog background color
    CBrush *pOld = pDC->SelectObject(&myBrush);
    BOOL bRes  = pDC->PatBlt(0, 0, rect.Width(), rect.Height(), PATCOPY);
    pDC->SelectObject(pOld);    // restore old brush
    return bRes;                       // CDialog::OnEraseBkgnd(pDC);
}

And have a look here看看这里

The better way will be to override WM_CTLCOLOR, background of controls such as STATIC will be fill with your color too.更好的方法是覆盖 WM_CTLCOLOR,STATIC 等控件的背景也将填充您的颜色。

BEGIN_MESSAGE_MAP(YourDlg, CDialogEx)
    ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
...
HBRUSH YourDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    return (HBRUSH)GetStockObject(WHITE_BRUSH);
}

The above answer will work only if you don't have a tab inside dialog box it will color background of dialog box other the tab portion.仅当对话框内没有选项卡时,上述答案才有效,它将为选项卡部分以外的对话框背景着色。 For the tab portion you have to create a new derived class with base class CTabCtrl.对于选项卡部分,您必须使用基类 CTabCtrl 创建一个新的派生类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM