【问题标题】:change the background color of a dialog box mfc更改对话框的背景颜色 mfc
【发布时间】:2013-06-07 18:25:16
【问题描述】:

我正在尝试更改对话框的背景颜色(win 7、vs2010、c++)。 我试图捕捉 WM_CTLCOLOR ,WM_ERASEBKGND 并改变颜色。 我设法以这种方式更改背景颜色,但是当窗口完成上传自身时,颜色恢复为默认值,但我注意到框架的颜色正确。 我认为我正在更改窗口而不是对话框或类似的东西。 我用 WTL(不是 AFX)来做这个。

我该怎么办?

【问题讨论】:

  • 只需在 google 中输入您的标题,或将结尾更改为 wtl 即可显示非常有希望的结果
  • 已经检查过了,但我没有发现任何有希望的东西
  • 该问题应该显示您已经尝试过的任何内容以及失败的原因,因此答案可以具体

标签: c++ user-interface colors mfc


【解决方案1】:

试试这个:

/////////////////////////////////////////////////////////////////////////////
// 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);
}

看看here

【讨论】:

  • 所有这些注释掉的行(例如 // AFX_MSG_MAP(CAboutDlg))是为了什么?
  • @aquirdturtle 这些 cmets 是由旧版本的 VS 生成的,因此向导知道在哪里插入代码。
【解决方案2】:

仅当您在对话框中没有选项卡时,上述答案才有效,它将为选项卡部分以外的对话框的背景着色。对于选项卡部分,您必须使用基类 CTabCtrl 创建一个新的派生类。

【讨论】:

    【解决方案3】:

    更好的方法是覆盖 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多