建立SWING風格的按鈕控制元件 (轉)

worldblog發表於2007-12-04
建立SWING風格的按鈕控制元件 (轉)[@more@]

本文介紹如何建立風格的按鈕

基本思想:(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;  //義文字
  CBrush nActiveBrush,nInactiveBrush;  義畫刷
  CPen  nDarkBorder,nWhiteBorder,nBorder;  義畫筆
 

// Operations
public:

// Overrs
 // ClassWizard generated virtual function overrides
 AFX_VIRTUAL(CSwingButton)
 public:
 virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
 protected:
 virtual void PreSubclassWindow();
 AFX_VIRTUAL

// Implementation
public:
 void SetButtonText(CString pString);
 void DrawButton(LPDRAWITEMSTRUCT lpDrawItemStruct);
 CString GetButtonText();
 virtual ~CSwingButton();
 
 // Generated message map functions
protected:
 AFX_MSG(CSwingButton)
 // NOTE - the ClassWizard will add and remove member functions here.
 AFX_MSG

 DECLARE_MESSAGE_MAP()
public:
};

/////////////////////////////////////////////////////////////////////////////

AFX_INSERT_LOCATION}}
// 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 _DE
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSwingButton

CSwingButton::CSwingButton()
{//初始化
 nActiveBrush.CreateSolirush(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)
 AFX_MSG_MAP(CSwingButton)
 // NOTE - the ClassWizard will add and remove map macrhere.
 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->Select(&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->Lo(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;

}


 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-987926/,如需轉載,請註明出處,否則將追究法律責任。

相關文章