怎样改变BUTTON控件的背景色和字体色?

GHenry 2000-02-14 10:00:00
加精
...全文
1241 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
beni 2000-02-17
  • 打赏
  • 举报
回复
WM_CTLCOLOR对一些控件不起作用,CWnd::OnCtlColor帮助上有
只能用新类
GHenry 2000-02-17
  • 打赏
  • 举报
回复
To yangrunhua:
我用了WM_CTLCOLOR,可以修改静态文本的颜色,
但就是改不了BUTTON的颜色,Why?
DOU 2000-02-15
  • 打赏
  • 举报
回复
也可使用CBitmapButton类
在Dialog中将Button控件的Bitmap属性选中,在Classwizard中绑定控件变量后,
修改CButton m_ctlTest为CBitmapButton m_ctlTest
在Dialog的initdialog事件中使用CBitmapButton的LoadBitmaps装载位图,将需要的背景和字体放在位图里。
应该可以达到需要的功能
GHenry 2000-02-15
  • 打赏
  • 举报
回复
能否直接改变现有的CBUTTON控件而不添加新类?(最好能简单点)
yangrunhua 2000-02-15
  • 打赏
  • 举报
回复
最简单的方法是自己生成一个继承CButton的类,然后生成WM_CTLCOLOR的
处理器.WM_CTLCOLOR是控件请求颜色而 发出的
sintony 2000-02-14
  • 打赏
  • 举报
回复
//定义色彩
const COLORREF CLOUDBLUE = RGB(128, 184, 223);
const COLORREF WHITE = RGB(255, 255, 255);
const COLORREF BLACK = RGB(1, 1, 1);
const COLORREF DKGRAY = RGB(128, 128, 128);
const COLORREF LTGRAY = RGB(192, 192, 192);
const COLORREF YELLOW = RGB(255, 255, 0);
const COLORREF DKYELLOW = RGB(128, 128, 0);
const COLORREF RED = RGB(255, 0, 0);
const COLORREF DKRED = RGB(128, 0, 0);
const COLORREF BLUE = RGB(0, 0, 255);
const COLORREF DKBLUE = RGB(0, 0, 128);
const COLORREF CYAN = RGB(0, 255, 255);
const COLORREF DKCYAN = RGB(0, 128, 128);
const COLORREF GREEN = RGB(0, 255, 0);
const COLORREF DKGREEN = RGB(0, 128, 0);
const COLORREF MAGENTA = RGB(255, 0, 255);
const COLORREF DKMAGENTA = RGB(128, 0, 128);

//在.h文件定义彩色按钮
CColorButton m_btnUp;

//在.cpp文件调用函数着色
VERIFY(m_btnUp.Attach(IDC_BUTTON1, this, RED, WHITE, DKRED));

//CColorButton 类原型
//colorbtn.h
#ifndef __COLORBTN_H__
#define __COLORBTN_H__
class CColorButton : public CButton
{
DECLARE_DYNAMIC(CColorButton)
public:
CColorButton();
virtual ~CColorButton();

BOOL Attach(const UINT nID, CWnd* pParent,
const COLORREF BGColor = RGB(192, 192, 192), // gray button
const COLORREF FGColor = RGB(1, 1, 1), // black text
const COLORREF DisabledColor = RGB(128, 128, 128), // dark gray disabled text
const UINT nBevel = 2
);


protected:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDIS);
void DrawFrame(CDC *DC, CRect R, int Inset);
void DrawFilledRect(CDC *DC, CRect R, COLORREF color);
void DrawLine(CDC *DC, CRect EndPoints, COLORREF color);
void DrawLine(CDC *DC, long left, long top, long right, long bottom, COLORREF color);
void DrawButtonText(CDC *DC, CRect R, const char *Buf, COLORREF TextColor);

COLORREF GetFGColor() { return m_fg; }
COLORREF GetBGColor() { return m_bg; }
COLORREF GetDisabledColor() { return m_disabled; }
UINT GetBevel() { return m_bevel; }

private:
COLORREF m_fg, m_bg, m_disabled;
UINT m_bevel;

};
#endif


//colorbtn.cpp
#include "stdafx.h"
#include "colorbtn.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif


#ifdef CColorButton
#undef CColorButton CColorButton
#endif


IMPLEMENT_DYNAMIC(CColorButton, CButton)

CColorButton::CColorButton()
{
#if (_MFC_VER < 0x0250)
hwndOwner = NULL;
#endif
}



CColorButton::~CColorButton()
{
}



BOOL CColorButton::Attach(const UINT nID, CWnd* pParent, const COLORREF BGColor, const COLORREF FGColor, const COLORREF DisabledColor, const UINT nBevel)
{
if (!SubclassDlgItem(nID, pParent))
return FALSE;

m_fg = FGColor;
m_bg = BGColor;
m_disabled = DisabledColor;
m_bevel = nBevel;

return TRUE;
}


void CColorButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
CDC* pDC = CDC::FromHandle(lpDIS->hDC);

UINT state = lpDIS->itemState;
CRect focusRect, btnRect;
focusRect.CopyRect(&lpDIS->rcItem);
btnRect.CopyRect(&lpDIS->rcItem);

focusRect.left += 4;
focusRect.right -= 4;
focusRect.top += 4;
focusRect.bottom -= 4;
const int bufSize = 512;
TCHAR buffer[bufSize];
GetWindowText(buffer, bufSize);


DrawFilledRect(pDC, btnRect, GetBGColor());
DrawFrame(pDC, btnRect, GetBevel());
DrawButtonText(pDC, btnRect, buffer, GetFGColor());
if (state & ODS_FOCUS) {
DrawFocusRect(lpDIS->hDC, (LPRECT)&focusRect);
if (state & ODS_SELECTED){
DrawFilledRect(pDC, btnRect, GetBGColor());
DrawFrame(pDC, btnRect, -1);
DrawButtonText(pDC, btnRect, buffer, GetFGColor());
DrawFocusRect(lpDIS->hDC, (LPRECT)&focusRect);
}
}
else if (state & ODS_DISABLED) {
DrawButtonText(pDC, btnRect, buffer, GetDisabledColor());
}
}


void CColorButton::DrawFrame(CDC *DC, CRect R, int Inset)
{
COLORREF dark, light, tlColor, brColor;
int i, m, width;
width = (Inset < 0)? -Inset : Inset;

for (i = 0; i < width; i += 1) {
m = 255 / (i + 2);
dark = PALETTERGB(m, m, m);
m = 192 + (63 / (i + 1));
light = PALETTERGB(m, m, m);

if ( width == 1 ) {
light = RGB(255, 255, 255);
dark = RGB(128, 128, 128);
}

if ( Inset < 0 ) {
tlColor = dark;
brColor = light;
}
else {
tlColor = light;
brColor = dark;
}

DrawLine(DC, R.left, R.top, R.right, R.top, tlColor); // Across top
DrawLine(DC, R.left, R.top, R.left, R.bottom, tlColor); // Down left

if ( (Inset < 0) && (i == width - 1) && (width > 1) ) {
DrawLine(DC, R.left + 1, R.bottom - 1, R.right, R.bottom - 1, RGB(1, 1, 1));// Across bottom
DrawLine(DC, R.right - 1, R.top + 1, R.right - 1, R.bottom, RGB(1, 1, 1)); // Down right
}
else {
DrawLine(DC, R.left + 1, R.bottom - 1, R.right, R.bottom - 1, brColor); // Across bottom
DrawLine(DC, R.right - 1, R.top + 1, R.right - 1, R.bottom, brColor); // Down right
}
InflateRect(R, -1, -1);
}
}



void CColorButton::DrawFilledRect(CDC *DC, CRect R, COLORREF color)
{
CBrush B;
B.CreateSolidBrush(color);
DC->FillRect(R, &B);
}


void CColorButton::DrawLine(CDC *DC, CRect EndPoints, COLORREF color)
{
CPen newPen;
newPen.CreatePen(PS_SOLID, 1, color);
CPen *oldPen = DC->SelectObject(&newPen);
DC->MoveTo(EndPoints.left, EndPoints.top);
DC->LineTo(EndPoints.right, EndPoints.bottom);
DC->SelectObject(oldPen);
newPen.DeleteObject();
}

void CColorButton::DrawLine(CDC *DC, long left, long top, long right, long bottom, COLORREF color)
{
CPen newPen;
newPen.CreatePen(PS_SOLID, 1, color);
CPen *oldPen = DC->SelectObject(&newPen);
DC->MoveTo(left, top);
DC->LineTo(right, bottom);
DC->SelectObject(oldPen);
newPen.DeleteObject();
}


void CColorButton::DrawButtonText(CDC *DC, CRect R, const char *Buf, COLORREF TextColor)
{
COLORREF prevColor = DC->SetTextColor(TextColor);
DC->SetBkMode(TRANSPARENT);
DC->DrawText(Buf, strlen(Buf), R, DT_CENTER and DT_VCENTER and DT_SINGLELINE);
DC->SetTextColor(prevColor);
}

sintony 2000-02-14
  • 打赏
  • 举报
回复
对不起,长了一点。
DOU 2000-02-14
  • 打赏
  • 举报
回复
使用CButton的SetBitmap和SetFont函数

16,466

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧