带个颜色框的按钮

效果图

//GColorButton.h

#pragma once

class GColorButton:public CButton

{

DECLARE_DYNAMIC(GColorButton)

public:

GColorButton(void);

public:

~GColorButton(void);

virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);  COLORREF GetFillColor(){return m_FillColor;}//获取填充框颜色

void SetFillColor(const COLORREF color){m_FillColor=color;}//设置填充框颜色

private:

COLORREF m_FillColor;//填充框颜色

};

//GColorButton.cpp

#include “StdAfx.h”

#include “GColorButton.h”

IMPLEMENT_DYNAMIC(GColorButton, CGridCell) GColorButton::GColorButton(void)

{

m_FillColor=RGB(255,0,0);

}

GColorButton::~GColorButton(void)

{

}

void GColorButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)

{

UINT uStyle = DFCS_BUTTONPUSH;

// If drawing selected, add the pushed style to DrawFrameControl.

if (lpDIS->itemState & ODS_SELECTED)

uStyle |= DFCS_PUSHED;

// Draw the button frame.

::DrawFrameControl(lpDIS->hDC, &lpDIS->rcItem,       DFC_BUTTON, uStyle);

RECT rc=lpDIS->rcItem;

int nHeight=abs(rc.top-rc.bottom);

int nWidth=abs(rc.right-rc.left);

nHeight/=4;

RECT rc1=rc;

rc1.left+=nHeight;

rc1.right=rc1.left+3*nHeight;

rc1.top=rc.top+nHeight;

rc1.bottom=rc.bottom-nHeight;

lpDIS->rcItem.left+=nHeight*4;  // Get the button’s text.

CString strText;

GetWindowText(strText);

::DrawText(lpDIS->hDC, strText, strText.GetLength(),       &lpDIS->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);

::FillRect(lpDIS->hDC,&rc1,CreateSolidBrush(m_FillColor));

}

 

特别注意,一定要在资源编辑时将按钮设置为OwnerDrw

表示自己绘制按钮

此条目发表在C++分类目录。将固定链接加入收藏夹。

发表评论