您的位置:首页 > 其它

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

2017-09-25 09:36 1151 查看
所有的操作针对添加的对话框

添加初始化:类导向--虚函数--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、
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐