本文介绍如何创建SWING风格的按钮控件
基本思想:(1)按钮选中时,绘制已按钮中点为中心的蓝色矩形;
(2)按钮预定义为自画风格;
(3)以按钮本身矩形错位画线,采用不同的画刷,计划笔,产生 SWING效果。
函数定义:
CSwingButton::DrawButton(LPDRAWITEMSTRUCT lpDrawItemStruct)//画按钮
void CSwingButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) //重载
void CSwingButton::PreSubclassWindow() //通知风格自画
CString CSwingButton::GetButtonText()//获得按钮文本---可以去除
CSwingButton::CSwingButton()//设置按钮文本---可以去除
void CSwingButton::SetButtonText(CString pString)
源程序如下:
#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 // SwingButton.h : header file //
///////////////////////////////////////////////////////////////////////////// // CSwingButton window
class CSwingButton : public CButton { // Construction public: CSwingButton();
// Attributes public: protected: CString m_strCaption; //file://定义文本 CBrush nActiveBrush,nInactiveBrush; file://定义画刷 CPen nDarkBorder,nWhiteBorder,nSelectBorder; file://定义画笔
// Operations public:
// Overrides // ClassWizard generated virtual function overrides file://{{AFX_VIRTUAL(CSwingButton) public: virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); protected: virtual void PreSubclassWindow(); file://}}AFX_VIRTUAL
// Implementation public: void SetButtonText(CString pString); void DrawButton(LPDRAWITEMSTRUCT lpDrawItemStruct); CString GetButtonText(); virtual ~CSwingButton(); // Generated message map functions protected: file://{{AFX_MSG(CSwingButton) // NOTE - the ClassWizard will add and remove member functions here. file://}}AFX_MSG
DECLARE_MESSAGE_MAP() public: };
/////////////////////////////////////////////////////////////////////////////
file://{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_SWINGBUTTON_H__4C72E29F_5844_4E65_9E87_CB96602E4733__INCLUDED_)
#include "stdafx.h" #include "TestSwing.h" #include "SwingButton.h"
#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif
///////////////////////////////////////////////////////////////////////////// // CSwingButton
CSwingButton::CSwingButton() {//初始化 nActiveBrush.CreateSolidBrush(RGB(110,110,110)); nInactiveBrush.CreateSolidBrush(RGB(204,204,204)); nDarkBorder.CreatePen(PS_SOLID,1,RGB(128,128,128)); nWhiteBorder.CreatePen(PS_SOLID,1,RGB(255,255,255)); nSelectBorder.CreatePen(PS_SOLID,1,RGB(153,153,204));
}
CSwingButton::~CSwingButton() { }
BEGIN_MESSAGE_MAP(CSwingButton, CButton) file://{{AFX_MSG_MAP(CSwingButton) // NOTE - the ClassWizard will add and remove mapping macros here. file://}}AFX_MSG_MAP END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////// // CSwingButton message handlers
void CSwingButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { // TODO: Add your code to draw the specified item //调用画按钮函数
DrawButton(lpDrawItemStruct); }
void CSwingButton::PreSubclassWindow() { // TODO: Add your specialized code here and/or call the base class CButton::PreSubclassWindow(); ModifyStyle(0,BS_OWNERDRAW);//通知自画风格 }
CString CSwingButton::GetButtonText() { return m_strCaption;//获得按钮文本 }
void CSwingButton::DrawButton(LPDRAWITEMSTRUCT lpDrawItemStruct) { CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC); CRect rectItem(lpDrawItemStruct->rcItem);
//获得按钮位置
CPoint TopLeft(rectItem.left, rectItem.top); CPoint BottomRight(rectItem.right - 1, rectItem.bottom - 1); CPoint TopRight(rectItem.right - 1, rectItem.top); CPoint BottomLeft(rectItem.left, rectItem.bottom - 1);
//选中情况下的绘制
if (lpDrawItemStruct->itemState & ODS_SELECTED) { pDC->SelectObject(&nActiveBrush); pDC->SelectStockObject(NULL_PEN); pDC->Rectangle(rectItem); goto WRITE_TEXT; }
//绘制
pDC->SelectObject(&nInactiveBrush); pDC->SelectStockObject(NULL_PEN); pDC->Rectangle(rectItem);
pDC->SelectObject(&nDarkBorder); pDC->MoveTo(TopLeft); pDC->LineTo(TopRight); pDC->MoveTo(TopLeft); pDC->LineTo(BottomLeft);
pDC->MoveTo(BottomLeft.x, BottomLeft.y - 1); pDC->LineTo(BottomRight.x, BottomRight.y - 1); pDC->MoveTo(BottomRight.x - 1, BottomRight.y); pDC->LineTo(TopRight.x - 1, TopRight.y);
pDC->SelectObject(&nWhiteBorder);
pDC->MoveTo(BottomLeft); pDC->LineTo(BottomRight); pDC->MoveTo(BottomRight); pDC->LineTo(TopRight);
pDC->MoveTo(TopLeft.x + 1, TopLeft.y + 1); pDC->LineTo(TopRight.x - 1, TopRight.y + 1); pDC->MoveTo(TopLeft.x + 1, TopLeft.y + 1); pDC->LineTo(BottomLeft.x + 1, BottomLeft.y - 1);
WRITE_TEXT: pDC->SetBkMode(TRANSPARENT); pDC->SelectStockObject(DEFAULT_GUI_FONT); CSize pExtent = pDC->GetTextExtent(m_strCaption);
//以下为绘制按钮文本
CPoint centerPoint = rectItem.CenterPoint(); CPoint drawText; drawText.x = centerPoint.x - (pExtent.cx / 2); drawText.y = centerPoint.y - (pExtent.cy / 2);
pDC->TextOut(drawText.x, drawText.y, m_strCaption);
if (lpDrawItemStruct->itemState & ODS_FOCUS) { pDC->SelectObject(&nSelectBorder); pDC->SelectStockObject(NULL_BRUSH); pDC->Rectangle(drawText.x - 1, drawText.y - 1, centerPoint.x + (pExtent.cx / 2) + 3, centerPoint.y + (pExtent.cy / 2) + 3); } }
void CSwingButton::SetButtonText(CString pString) {
//设置按钮文本 m_strCaption=pString;
}

|