CEdit CPropertyPage中的只读背景颜色

时间:2012-05-17 05:56:32

标签: visual-c++ mfc

某些CPropertyPage中放置的CEdit控件的默认背景颜色是什么? 让我感到困惑的是,当更改CEdit主机时,背景颜色会有所不同。也就是说,如果其中有一个带有CEdit的CTabCtrl对话框,则只读CEdit的背景为灰色(我想这是Windows中的默认值)。但是,如果使用CPropertySheet和CPropertyPage而不是CDialog,则只读CEdit的背景为白色。 使用CDialog: Default behaviour in CDialog

使用CPropertySheet: enter image description here

用于构建这些窗口的代码:

基于CDialog

// DialogWithEdit.h
#pragma once

class CDialogWithEdit : public CDialog
{
    DECLARE_DYNAMIC(CDialogWithEdit)    
public:
    CDialogWithEdit(CWnd* pParent = NULL);   // standard constructor
    virtual ~CDialogWithEdit();
    enum { IDD = IDD_DIALOG1 };
protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    virtual BOOL OnInitDialog();
    DECLARE_MESSAGE_MAP()    
private:
    CEdit m_regularEdit;
    CEdit m_readOnlyEdit;
};

// DialogWithEdit.cpp : implementation file

#include "stdafx.h"
#include "PropertySheetTest.h"
#include "DialogWithEdit.h"
#include "afxdialogex.h"

IMPLEMENT_DYNAMIC(CDialogWithEdit, CDialog)

CDialogWithEdit::CDialogWithEdit(CWnd* pParent /*=NULL*/)
    : CDialog(CDialogWithEdit::IDD, pParent){    }

CDialogWithEdit::~CDialogWithEdit(){    }

void CDialogWithEdit::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CDialogWithEdit, CDialog)
END_MESSAGE_MAP()

BOOL CDialogWithEdit::OnInitDialog ()
    {
    BOOL retVal = CDialog::OnInitDialog ();

    WCHAR tabStr[5] = {0};
    wcscpy (tabStr, L"Tab1");

    TCITEM tab1 = {0};
    tab1.mask = TCIF_TEXT;
    tab1.cchTextMax = 5;
    tab1.pszText = tabStr;
    CTabCtrl * tabCtrl = (CTabCtrl*) GetDlgItem (IDC_TAB1);
    tabCtrl->InsertItem (0, &tab1);
    RECT tabRc = {0};
    tabCtrl->GetItemRect (0, &tabRc);

    m_readOnlyEdit.Create (WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL|ES_READONLY, CRect (10, 30, 100, 50), GetDlgItem (IDC_TAB1), 5);
    m_readOnlyEdit.SetWindowText (L"read only");
    m_regularEdit.Create (WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL, CRect (110, 30, 200, 50), GetDlgItem (IDC_TAB1), 5+1);
    m_regularEdit.SetWindowText (L"editable");
    return retVal;
    }

基于CPropertySheet

// SheetX.h
#pragma once
#include "PageX.h"

class CSheetX : public CPropertySheet
{
    DECLARE_DYNAMIC(CSheetX)    
public:
    CSheetX(UINT nIDCaption, CWnd* pParentWnd = NULL, UINT iSelectPage = 0);
    CSheetX(LPCTSTR pszCaption, CWnd* pParentWnd = NULL, UINT iSelectPage = 0);
    virtual ~CSheetX();    
protected:
    DECLARE_MESSAGE_MAP()    
private:
    CPageX m_pageX;
};

// SheetX.cpp : implementation file

#include "stdafx.h"
#include "PropertySheetTest.h"
#include "SheetX.h"

IMPLEMENT_DYNAMIC(CSheetX, CPropertySheet)

CSheetX::CSheetX(UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage)
    :CPropertySheet(nIDCaption, pParentWnd, iSelectPage)
{ AddPage (&m_pageX);   }

CSheetX::CSheetX(LPCTSTR pszCaption, CWnd* pParentWnd, UINT iSelectPage)
    :CPropertySheet(pszCaption, pParentWnd, iSelectPage)
{ AddPage (&m_pageX);   }

CSheetX::~CSheetX() { }    

BEGIN_MESSAGE_MAP(CSheetX, CPropertySheet)
END_MESSAGE_MAP()

// PaheX.h
#pragma once

class CPageX : public CPropertyPage
{
    DECLARE_DYNAMIC(CPageX)    
public:
    CPageX();
    virtual ~CPageX();
    enum { IDD = IDD_PAGEX };    
protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    virtual BOOL OnInitDialog ();
    DECLARE_MESSAGE_MAP()    
private:
    CEdit m_readOnlyEdit;
    CEdit m_regularEdit;
};
// PageX.cpp : implementation file

#include "stdafx.h"
#include "PropertySheetTest.h"
#include "PageX.h"    

#define ID_EDITCTRL     151    
IMPLEMENT_DYNAMIC(CPageX, CPropertyPage)

CPageX::CPageX()
    : CPropertyPage(CPageX::IDD){    }

CPageX::~CPageX(){    }

void CPageX::DoDataExchange(CDataExchange* pDX)
{
    CPropertyPage::DoDataExchange(pDX);
}

BOOL CPageX::OnInitDialog ()
    {
    BOOL retVal = CPropertyPage::OnInitDialog ();
    m_readOnlyEdit.Create (WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL|ES_READONLY, CRect (10,10,100,30), this, ID_EDITCTRL);
    m_readOnlyEdit.SetWindowText (L"read only");
    m_regularEdit.Create (WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL, CRect (110,10,200,30), this, ID_EDITCTRL+1);
    m_regularEdit.SetWindowText (L"editable");
    return retVal;
    }

BEGIN_MESSAGE_MAP(CPageX, CPropertyPage)
END_MESSAGE_MAP()

我可以通过在CPropertyPage中处理WM_CTLCOLORSTATIC并返回默认对话框背景颜色的画笔来为白色背景颜色添加变通方法。但这对我来说似乎不对。另一方面,msdn表示将WM_CTLCOLORSTATIC发送给父级,以便指定背景颜色。由于有问题的CEdit控件的父级是CPropertyPage,我猜这就是它返回白色画笔的原因。 或者我可能在CPropertyPage上做错了什么?

1 个答案:

答案 0 :(得分:0)

这是默认的Windows行为。因此,必须选择:坚持使用默认的窗口行为或将CP_pertLC中的WM_CTLCOLORSTATIC消息传送到返回其背景颜色(默认为灰色)的CPropertySheet。在经典主题上,一切都正常,因为CPropertyPage的颜色是灰色的。