在 CEdit 和 CStatic 中使用颜色






4.83/5 (82投票s)
2001年3月29日
1分钟阅读

386981

13965
从 CEdit 和 CStatic 派生的类。它使得更改文本和背景颜色变得容易。
引言
我创建这两个类是为了方便地更改编辑框文本和静态文本的颜色。我不需要 CRichEditCtrl
的所有开销,但确实需要更改文本颜色以及框的背景颜色。CStatic
也没有提供一种简单的方法来更改文本颜色。
这些类是从 CEdit
和 CStatic
派生的。
如何使用
如果只是使用编辑框,请在项目中包含文件 ColorEdit.cpp, ColorEdit.h 和 Color.h。 如果要同时包含彩色静态文本,则添加文件 ColorStatic.cpp、ColorStatic.h。
在对话框的头文件中,添加
#include "ColorEdit.h"
#include "ColorStatic.h" //only if using colored static text.
public:
CColorEdit m_ebCtl;
CColorStatic m_stText; //only if using colored static text.
有两种方法可以将您的控件 ID 与类关联起来。 从现在开始,我将假设您正在使用这两个类。
在对话框的 .cpp 文件中,添加
void YourDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CYourDlg)
//}}AFX_DATA_MAP
DDX_Control(pDX, IDC_ST_TEXT, m_stText);
DDX_Control(pDX, IDC_EB_CTL, m_ebCtl);
}
或
BOOL CYourDlg::OnInitDialog()
{
// TODO: Add extra initialization here
m_ebCtl.SubclassDlgItem(IDC_EB_CTL,this);
m_stText.SubclassDlgItem(IDC_ST_TEXT,this);
}
现在完成这些设置后,就可以使用该类了。 对于编辑框,有三个函数可用,对于静态文本,有两个函数可用。
它们如下
There are three functions available Currently:
SetBkColor(COLORREF crColor) // Works for both classes
SetTextColor(COLORREF crColor) // Works for both classes
SetReadOnly(BOOL flag = TRUE) //This function is for CColorEdit only.
在文件 Color.h 中包含以下代码
// Color.h
// Colorref's to use with your Programs
#define RED RGB(127, 0, 0)
#define GREEN RGB( 0,127, 0)
#define BLUE RGB( 0, 0,127)
#define LIGHTRED RGB(255, 0, 0)
#define LIGHTGREEN RGB( 0,255, 0)
#define LIGHTBLUE RGB( 0, 0,255)
#define BLACK RGB( 0, 0, 0)
#define WHITE RGB(255,255,255)
#define GRAY RGB(192,192,192)
这些只是我挑选的几个颜色,您可以根据需要添加更多颜色。
使用起来非常简单,如下所示
m_ebCtl.SetTextColor(BLUE); //Changes the Edit Box text to Blue
m_ebCtl.SetBkColor(WHITE); //By default your background color is the
//same as your system color(color of dialog)
m_ebCtl.SetReadOnly(); //This makes it so nobody can edit the text.
//If you disable the box it does not let you
//change colors.
m_stText.SetTextColor(RED); //Changes the Static Text to Red
m_stText.SetBkColor(GREEN); //You probably will not use it, but it's here.
希望有人觉得这有用 :)
许可证
本文未附加明确的许可证,但可能在文章文本或下载文件本身中包含使用条款。如有疑问,请通过下面的讨论区联系作者。
作者可能使用的许可证列表可以在此处找到。