放置在某些 CPropertyPage 中的 CEdit 控件的默认背景颜色是什么?令我困惑的是,当大量 CEdit 更改时,背景颜色会有所不同。也就是说,如果其中有一个带有 CEdit 的 CTabCtrl 对话框,则只读 CEdit 的背景是灰色的(我想它是 Windows 中的默认设置)。但是,如果使用 CPropertySheet 和 CPropertyPage 而不是 CDialog,则只读 CEdit 的背景为白色。使用 CDialog:
并使用 CPropertySheet:
用于构建这些窗口的代码:
基于 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 上做错了什么?