工控编程吧
标题:
上位机MFC彩色按钮实例
[打印本页]
作者:
qq263946146
时间:
2019-7-3 16:59
标题:
上位机MFC彩色按钮实例
效果演示
(, 下载次数: 0)
上传
点击文件名下载附件
例程实现不同颜色按钮显示功能,效果如图。
可以通过下面代码复制使用,或直接下载源代码使用:
(, 下载次数: 0)
上传
点击文件名下载附件
实现过程:
1.新建基于对话框工程,如上图添加几个按钮按钮用于测试。
2.将工程根目录两文件,复制到自己工程,并加载到自己工程。
这样就多出一新类
(, 下载次数: 0)
上传
点击文件名下载附件
3.在主对话框中包含类CColorButton的头文件
#include "colorbtn.h"
添加变量
CColorButton m_btnCancel;
CColorButton m_btnOK;
CColorButton m_btnUp;
CColorButton m_btnDown;
CColorButton m_btnDelete;
CColorButton m_btnAdd;
初始初始化,注意按钮控件ID的对应
VERIFY(m_btnOK.Attach(IDOK, this, CYAN, BLUE, DKCYAN));
VERIFY(m_btnCancel.Attach(IDCANCEL, this, BLUE, WHITE, DKBLUE));
VERIFY(m_btnUp.Attach(IDC_BUTTON1, this, RED, WHITE, DKRED));
VERIFY(m_btnDown.Attach(IDC_BUTTON2, this, GREEN, BLUE, DKYELLOW));
VERIFY(m_btnDelete.Attach(IDC_BUTTON3, this, YELLOW, BLUE, GREEN));
VERIFY(m_btnAdd.Attach(IDC_BUTTON4, this, MAGENTA, WHITE, DKMAGENTA));
这样简单的操作后,就可以运行例程了。
下面是两文件的代码,可以复制使用
#ifndef __COLORBTN_H__
#define __COLORBTN_H__
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);
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
复制代码
#include "stdafx.h"
#include "colorbtn.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
// no automatic class substitution for this file!
#ifdef CColorButton
#undef CColorButton CColorButton
#endif
// CColorButton
IMPLEMENT_DYNAMIC(CColorButton, CButton)
CColorButton::CColorButton()
{
#if (_MFC_VER < 0x0250)
hwndOwner = NULL; // initialize hwndOwner for GetOwner() and SetOwner() support in MFC < 2.5
#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);
//
// Set the focus rectangle to just past the border decoration
//
focusRect.left += 4;
focusRect.right -= 4;
focusRect.top += 4;
focusRect.bottom -= 4;
//
// Retrieve the button's caption
//
const int bufSize = 512;
TCHAR buffer[bufSize];
GetWindowText(buffer, bufSize);
//
// Draw and label the button using draw methods
//
DrawFilledRect(pDC, btnRect, GetBGColor());
DrawFrame(pDC, btnRect, GetBevel());
DrawButtonText(pDC, btnRect, buffer, GetFGColor());
//
// Now, depending upon the state, redraw the button (down image) if it is selected,
// place a focus rectangle on it, or redisplay the caption if it is disabled
//
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) {
//COLORREF disabledColor = bg ^ 0xFFFFFF; // contrasting color
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|DT_VCENTER|DT_SINGLELINE);
DC->SetTextColor(prevColor);
}
复制代码
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4