MFC中button控件背景色和字体的设置,或者可以采用添加背景图片的方法;已验证有效MFC其他的简单使用使用

所有的操作针对添加的对话框

添加初始化:类导向--虚函数--OnInitDialog()

1、添加背景色:WM_PAINT->void CAboutDlg::OnPaint()


2、调用子对话框:InitInstance();创建了新的对话框,不要忘记将类添加到头文件中

3、

关于改变AfxMessageBox对话框标题

   其实这个标题,也就是我们的默认的工程名,我们应该怎么在不改变工程名的基础上改变标题呢?其实这个标题在资源StringTable里就能找到,查找AFX_IDS_APP_TITLE,在这里你就能轻而易举的改变标题了

4、MFC中改变button的背景颜色和字体颜色,字体大小等;采用自绘的方式:需要将控件Owner Draw改为TRUE,不然无法没有效果

添加ClrButton.h

#if !defined(AFX_CLRBUTTON_H__3611A4FC_CBDB_11D5_A183_B13329B34330__INCLUDED_)
#define AFX_CLRBUTTON_H__3611A4FC_CBDB_11D5_A183_B13329B34330__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ClrButton.h : header file
//

/
// CClrButton window

const COLORREF BLACK = RGB(1, 1, 1);
const COLORREF GRARY = RGB(122, 122, 122);
const COLORREF DKRED = RGB(128, 0, 0);
const COLORREF GREEN = RGB(0, 255, 0);
const COLORREF RED = RGB(255, 0, 0);
const COLORREF BLUE = RGB(0, 0, 255);
const COLORREF WHITE = RGB(255, 255, 255);

class CClrButton : public CButton
{
// Construction
public:
	CClrButton();

// Attributes
public:
// Operations
public:
	// UK version
	void SetColour(COLORREF text_colour, 
				   COLORREF background_colour);

	// US version
	void SetColor(COLORREF text_colour, 
				  COLORREF background_colour)
	{
		SetColour(text_colour, 
				  background_colour);		
	};

	// UK version
	void SetColour(COLORREF text_colour, 
				   COLORREF background_colour, 
				   COLORREF disabled_background_colour);

	// US version
	void SetColor(COLORREF text_colour, 
				  COLORREF background_colour, 
				  COLORREF disabled_background_colour)
	{
		SetColour(text_colour, 
				  background_colour, 
				  disabled_background_colour);
	};

	void ResetColour();

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CClrButton)
	public:
	virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CClrButton();

private:
	COLORREF text_colour, background_colour, disabled_background_colour;
	COLORREF light, highlight, shadow, dark_shadow;

	// Generated message map functions
protected:
	//{{AFX_MSG(CClrButton)
		// NOTE - the ClassWizard will add and remove member functions here.
	//}}AFX_MSG

	void DrawFrame(CDC *dc, CRect r, int state);
	void DrawFilledRect(CDC *dc, CRect r, COLORREF colour);
	void DrawLine(CDC *dc, long sx, long sy, long ex, long ey, COLORREF colour);
	void DrawButtonText(CDC *dc, CRect r, const char *buf, COLORREF text_colour);

	DECLARE_MESSAGE_MAP()
};

/

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_CLRBUTTON_H__3611A4FC_CBDB_11D5_A183_B13329B34330__INCLUDED_)


添加ClrButton.c(在编码不同时,下面可能产生错误;Unicode和宽字节编码)

// ClrButton.cpp : implementation file
//


#include "stdafx.h"
#include "ClrButton.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


const COLORREF CLR_BTN_WHITE  = RGB(255, 255, 255);
const COLORREF CLR_BTN_BLACK  = RGB(0, 0, 0);
const COLORREF CLR_BTN_DGREY  = RGB(128, 128, 128);
const COLORREF CLR_BTN_GREY   = RGB(192, 192, 192);
const COLORREF CLR_BTN_LLGREY = RGB(223, 223, 223);


const int BUTTON_IN  = 0x01;
const int BUTTON_OUT  = 0x02;
const int BUTTON_BLACK_BORDER = 0x04;


/
// CClrButton


CClrButton::CClrButton()
{
text_colour = GetSysColor(COLOR_BTNTEXT);
background_colour = GetSysColor(COLOR_BTNFACE); 
disabled_background_colour = background_colour;
light = GetSysColor(COLOR_3DLIGHT);
highlight = GetSysColor(COLOR_BTNHIGHLIGHT);
shadow = GetSysColor(COLOR_BTNSHADOW);
dark_shadow = GetSysColor(COLOR_3DDKSHADOW);
}


CClrButton::~CClrButton()
{
}
void CClrButton::SetColour(COLORREF new_text_colour, COLORREF new_background_colour)
{
text_colour = new_text_colour;
background_colour = new_background_colour; 
disabled_background_colour = GetSysColor(COLOR_BTNFACE); 


Invalidate(FALSE);
}


void CClrButton::SetColour(COLORREF new_text_colour, COLORREF new_background_colour, COLORREF new_disabled_background_colour)
{
text_colour = new_text_colour;
background_colour = new_background_colour; 
disabled_background_colour = new_disabled_background_colour; 


Invalidate(FALSE);
}


void CClrButton::ResetColour()
{
text_colour = GetSysColor(COLOR_BTNTEXT);
background_colour = GetSysColor(COLOR_BTNFACE); 
disabled_background_colour = background_colour;
light = GetSysColor(COLOR_3DLIGHT);
highlight = GetSysColor(COLOR_BTNHIGHLIGHT);
shadow = GetSysColor(COLOR_BTNSHADOW);
dark_shadow = GetSysColor(COLOR_3DDKSHADOW);


Invalidate(FALSE);
}


BEGIN_MESSAGE_MAP(CClrButton, CButton)
//{{AFX_MSG_MAP(CClrButton)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/
// CClrButton message handlers


void CClrButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
CDC *dc;
CRect focus_rect, button_rect, text_rect, offset_text_rect;
UINT state;

dc    = CDC::FromHandle(lpDrawItemStruct->hDC);
state = lpDrawItemStruct->itemState;


focus_rect.CopyRect(&lpDrawItemStruct->rcItem); 
button_rect.CopyRect(&lpDrawItemStruct->rcItem); 


text_rect = button_rect;
text_rect.OffsetRect(-1, -1); 
offset_text_rect = text_rect;
offset_text_rect.OffsetRect(1, 1);


// Set the focus rectangle to just past the border decoration
focus_rect.left   += 4;
    focus_rect.right  -= 4;
    focus_rect.top    += 4;
    focus_rect.bottom -= 4;
      
// Retrieve the button's caption
    const int bufSize = 512;
    TCHAR buffer[bufSize];
    GetWindowText(buffer, bufSize);


if (state & ODS_DISABLED)
    {
DrawFilledRect(dc, button_rect, disabled_background_colour);
}
else
{
DrawFilledRect(dc, button_rect, background_colour);
}

if (state & ODS_SELECTED)

    DrawFrame(dc, button_rect, BUTTON_IN);
}
else
{
if ((state & ODS_DEFAULT) || (state & ODS_FOCUS))
{
DrawFrame(dc, button_rect, BUTTON_OUT | BUTTON_BLACK_BORDER);
}
else
{
DrawFrame(dc, button_rect, BUTTON_OUT);
}
}


if (state & ODS_DISABLED)
{
DrawButtonText(dc, offset_text_rect, (LPSTR)buffer, CLR_BTN_WHITE);
DrawButtonText(dc, text_rect, (LPSTR)buffer, CLR_BTN_DGREY);//宽字节编码,将前面的强制转换去掉(LPSTR)
    }
else
{
if (state & ODS_SELECTED)
{
DrawButtonText(dc, offset_text_rect, (LPSTR)buffer, text_colour);
}
else
{
  DrawButtonText(dc, text_rect, (LPSTR)buffer, text_colour);
}
}


if (state & ODS_FOCUS)
{
DrawFocusRect(lpDrawItemStruct->hDC, (LPRECT)&focus_rect);
}



void CClrButton::DrawFrame(CDC *dc, CRect r, int state)

COLORREF colour;


if (state & BUTTON_BLACK_BORDER)
{
colour = CLR_BTN_BLACK;


DrawLine(dc, r.left, r.top, r.right, r.top,    colour); // Across top
DrawLine(dc, r.left, r.top, r.left,  r.bottom, colour); // Down left


DrawLine(dc, r.left,      r.bottom - 1, r.right,     r.bottom - 1, colour); // Across bottom
DrawLine(dc, r.right - 1, r.top,        r.right - 1, r.bottom,     colour); // Down right


r.InflateRect(-1, -1);
}

if (state & BUTTON_OUT)
{
colour = highlight;


DrawLine(dc, r.left, r.top, r.right, r.top,    colour); // Across top
DrawLine(dc, r.left, r.top, r.left,  r.bottom, colour); // Down left


colour = dark_shadow;

DrawLine(dc, r.left,      r.bottom - 1, r.right,     r.bottom - 1, colour); // Across bottom
DrawLine(dc, r.right - 1, r.top,        r.right - 1, r.bottom,     colour); // Down right

r.InflateRect(-1, -1);


colour = light;

DrawLine(dc, r.left, r.top, r.right, r.top,    colour); // Across top
DrawLine(dc, r.left, r.top, r.left,  r.bottom, colour); // Down left


colour = shadow;

DrawLine(dc, r.left,      r.bottom - 1, r.right,     r.bottom - 1, colour); // Across bottom
DrawLine(dc, r.right - 1, r.top,        r.right - 1, r.bottom,     colour); // Down right
}


if (state & BUTTON_IN)
{
colour = dark_shadow;


DrawLine(dc, r.left, r.top, r.right, r.top,    colour); // Across top
DrawLine(dc, r.left, r.top, r.left,  r.bottom, colour); // Down left
DrawLine(dc, r.left,      r.bottom - 1, r.right,     r.bottom - 1, colour); // Across bottom
DrawLine(dc, r.right - 1, r.top,        r.right - 1, r.bottom,     colour); // Down right

r.InflateRect(-1, -1);


colour = shadow;

DrawLine(dc, r.left, r.top, r.right, r.top,    colour); // Across top
DrawLine(dc, r.left, r.top, r.left,  r.bottom, colour); // Down left
DrawLine(dc, r.left,      r.bottom - 1, r.right,     r.bottom - 1, colour); // Across bottom
DrawLine(dc, r.right - 1, r.top,        r.right - 1, r.bottom,     colour); // Down right
}
}


void CClrButton::DrawFilledRect(CDC *dc, CRect r, COLORREF colour)

CBrush B;


B.CreateSolidBrush(colour);
dc->FillRect(r, &B);
}


void CClrButton::DrawLine(CDC *dc, long sx, long sy, long ex, long ey, COLORREF colour)

CPen new_pen;
CPen *old_pen;


new_pen.CreatePen(PS_SOLID, 1, colour);
old_pen = dc->SelectObject(&new_pen);
dc->MoveTo(sx, sy);
dc->LineTo(ex, ey);
dc->SelectObject(old_pen);
    new_pen.DeleteObject();
}




void CClrButton::DrawButtonText(CDC *dc, CRect r, const char *Buf, COLORREF text_colour)
{
    COLORREF previous_colour;


previous_colour = dc->SetTextColor(text_colour);
    dc->SetBkMode(TRANSPARENT);
dc->DrawText((LPCTSTR)Buf, (int)(strlen(Buf)), r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
dc->SetTextColor(previous_colour);
}

在引用时不要忘记添加 #include"ClrButton.h"

使用方法:添加一个类 ClrButton m_ButtonNext;

添加

const COLORREF CLR_BTN_WHITE  = RGB(255, 255, 255);
const COLORREF CLR_BTN_BLACK  = RGB(0, 0, 0);
const COLORREF CLR_BTN_DGREY  = RGB(128, 128, 128);
const COLORREF CLR_BTN_GREY   = RGB(192, 192, 192);
const COLORREF CLR_BTN_LLGREY = RGB(223, 223, 223);


使用

	CFont * f;
	f = new CFont;
	f->CreateFont(18, // nHeight   
		0, // nWidth   
		0, // nEscapement   
		0, // nOrientation   
		FW_BOLD, // nWeight   
		FALSE, // bItalic   斜体
		FALSE, // bUnderline   
		0, // cStrikeOut   
		ANSI_CHARSET, // nCharSet   
		OUT_DEFAULT_PRECIS, // nOutPrecision   
		CLIP_DEFAULT_PRECIS, // nClipPrecision   
		DEFAULT_QUALITY, // nQuality   
		DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily   
		//_T("Arial")); // lpszFac 
		_T("微软雅黑"));

												
	m_ButtonHome.SubclassDlgItem(IDC_BUTTON1, this);
	m_ButtonHome.SetColour(WHITE, BLACK);//字体颜色,背景颜色
	m_ButtonHome.SetFont(f,TRUE);//字体大小



5、WCHAR*类型与const char*不兼容:(LPSTR)buffer强制转换一下;这时unicode类型与宽字节类型的区别

6、添加背景色,button的BitMap改为True,给button起个变量名;

HBITMAP hBmp=::LoadBitmap(AfxGetInstanceHandle(),  MAKEINTRESOURCE(IDB_BITMAP1));   
   m_Btn.SetBitmap(hBmp);


7、

8、

9、

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MFC,我们可以通过以下几种方式来修改Button控件字体字体大小、背景色背景图片。 1. 修改字体字体大小: 通过Button控件的SetFont函数可以设置字体字体大小。举例如下: ``` CButton* pBtn = (CButton*)GetDlgItem(IDC_BUTTON1); CFont font; font.CreateFont(16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, _T("Arial")); pBtn->SetFont(&font); ``` 上述例子,创建了一个高度为16的Arial字体,并将其应用到ID为IDC_BUTTON1的Button控件。 2. 修改背景色: 可以通过Button控件的SetBkColor函数设置背景色。举例如下: ``` CButton* pBtn = (CButton*)GetDlgItem(IDC_BUTTON1); pBtn->SetBkColor(RGB(255, 0, 0)); ``` 上述例子,将ID为IDC_BUTTON1的Button控件背景色设置为红色。 3. 修改背景图片: 可以通过Button控件的SetBitmap函数设置背景图片。举例如下: ``` CButton* pBtn = (CButton*)GetDlgItem(IDC_BUTTON1); HBITMAP hBitmap = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), _T("path_to_image.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION); pBtn->SetBitmap(hBitmap); ``` 上述例子,从文件加载一张位图图片,并将其设置为ID为IDC_BUTTON1的Button控件背景图片。 总结: 通过以上三种方法,我们可以在MFC方便地修改Button控件字体字体大小、背景色背景图片。注意在使用时,需将代码放在相应的初始化函数,如OnInitDialog()。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一枚努力的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值