现在的位置: 首页 > 综合 > 正文

vc可设置字体和颜色的按钮 源代码

2013年09月03日 ⁄ 综合 ⁄ 共 5864字 ⁄ 字号 评论关闭

#if !defined(AFX_MYBUTTON_H__CB4940BF_2442_4FDF_8C7F_6CB245EE1DE8__INCLUDED_)
#define AFX_MYBUTTON_H__CB4940BF_2442_4FDF_8C7F_6CB245EE1DE8__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyButton.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CMyButton window

class CMyButton : public CButton
{
// Construction
public:
 CMyButton();

private:
 int   m_Style;   //°´Å¥ÐÎ×´£¨0-Õý³££¬1-µ±Ç°£¬2-°´Ï£¬3-Ëø¶¨£©
 BOOL  b_InRect;   //Êó±ê½øÈë±êÖ¾
 CString  m_strText;   //°´Å¥ÎÄ×Ö
 COLORREF m_ForeColor;  //Îı¾ÑÕÉ«
 COLORREF m_BackColor;  //±³¾°É«
 COLORREF m_LockForeColor; //Ëø¶¨°´Å¥µÄÎÄ×ÖÑÕÉ«
 CRect  m_ButRect;   //°´Å¥³ß´ç
 CFont*  p_Font;    //×ÖÌå

 void  DrawButton(CDC *pDC);  //»­Õý³£µÄ°´Å¥

// ½Ó¿Úº¯Êý
public:
 void SetText(CString str);
 void SetForeColor(COLORREF color);  //ÉèÖÃÎı¾ÑÕÉ«
 void SetBkColor(COLORREF color);  //ÉèÖñ³¾°ÑÕÉ«
 void SetTextFont(int FontHight,LPCTSTR FontName);  //ÉèÖÃ×ÖÌå

// Attributes
public:

// Operations
public:

// Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CMyButton)
 public:
 virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
 protected:
 virtual void PreSubclassWindow();
 //}}AFX_VIRTUAL

// Implementation
public:
 virtual ~CMyButton();

 // Generated message map functions
protected:
 //{{AFX_MSG(CMyButton)
 afx_msg void OnMouseMove(UINT nFlags, CPoint point);
 afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
 afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
 //}}AFX_MSG

 DECLARE_MESSAGE_MAP()
};

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

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MYBUTTON_H__CB4940BF_2442_4FDF_8C7F_6CB245EE1DE8__INCLUDED_)

 

2。cpp------------------------

// MyButton.cpp : implementation file
//

#include "stdafx.h"
#include "CMyButton.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyButton

CMyButton::CMyButton()
{
 m_Style = 0;   //°´Å¥ÐÎ×´·ç¸ñ
 b_InRect = false;  //Êó±ê½øÈë±êÖ¾
 m_strText = _T("");  //°´Å¥ÎÄ×Ö£¨Ê¹ÓÃĬÈÏÎÄ×Ö£©
 m_ForeColor = RGB(0,0,0);   //ÎÄ×ÖÑÕÉ«£¨ºÚÉ«£©
 m_BackColor = RGB(243,243,243);  //±³¾°É«£¨»Ò°×É«£©
 m_LockForeColor = GetSysColor(COLOR_GRAYTEXT); //Ëø¶¨°´Å¥µÄÎÄ×ÖÑÕÉ«
 p_Font = NULL;       //×ÖÌåÖ¸Õë
}

CMyButton::~CMyButton()
{
 if ( p_Font ) delete p_Font;  //ɾ³ý×ÖÌå
}

BEGIN_MESSAGE_MAP(CMyButton, CButton)
 //{{AFX_MSG_MAP(CMyButton)
 ON_WM_MOUSEMOVE()
 ON_WM_LBUTTONDOWN()
 ON_WM_LBUTTONUP()
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyButton message handlers

void CMyButton::PreSubclassWindow()
{
 ModifyStyle( 0, BS_OWNERDRAW );  //ÉèÖð´Å¥ÊôÐÔΪ×Ô»­Ê½
 
 CButton::PreSubclassWindow();
}

//ÏûÏ¢º¯Êý£º»æÖÆ°´Å¥
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
 CDC *pDC = CDC::FromHandle( lpDrawItemStruct->hDC );
 m_ButRect = lpDrawItemStruct->rcItem; //»ñÈ¡°´Å¥³ß´ç

 if( m_strText.IsEmpty() )
  GetWindowText( m_strText );   //»ñÈ¡°´Å¥Îı¾

 int nSavedDC = pDC->SaveDC();
 VERIFY( pDC );
 DrawButton( pDC );    //»æÖÆ°´Å¥
 pDC->RestoreDC( nSavedDC );
}

//ÏûÏ¢º¯Êý£ºÊó±êÒƶ¯
void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
 if( !b_InRect || GetCapture()!=this ) //Êó±ê½øÈë°´Å¥
 {
  b_InRect = true; //ÉèÖýøÈë±êÖ¾
  SetCapture();  //²¶»ñÊó±ê
  m_Style = 1;  //ÉèÖð´Å¥×´Ì¬
  Invalidate();  //Öػ水ť
 }
 else
 {
  if ( !m_ButRect.PtInRect(point) )   //Êó±êÀ뿪°´Å¥
  {
   b_InRect = false; //Çå³ý½øÈë±êÖ¾
   ReleaseCapture(); //ÊͷŲ¶»ñµÄÊó±ê
   m_Style = 0;  //ÉèÖð´Å¥×´Ì¬
   Invalidate();  //Öػ水ť
  }
 }
 
 CButton::OnMouseMove(nFlags, point);
}

//ÏûÏ¢º¯Êý£ºµ¥»÷×ó¼ü
void CMyButton::OnLButtonDown(UINT nFlags, CPoint point)
{
 m_Style = 2;
 Invalidate();  //Öػ水ť
 
 CButton::OnLButtonDown(nFlags, point);
}

//ÏûÏ¢º¯Êý£ºµ¯Æð×ó¼ü
void CMyButton::OnLButtonUp(UINT nFlags, CPoint point)
{
 m_Style = 1;
 Invalidate();  //Öػ水ť
 
 CButton::OnLButtonUp(nFlags, point);
}

////////////////////////////////////////////////////////////////////////
//»æÖÆ°´Å¥£º

//»­°´Å¥£¨0-Õý³££¬1-µ±Ç°£¬2-°´Ï£¬3-Ëø¶¨£©
void CMyButton::DrawButton(CDC *pDC)
{
 //µ÷Õû״̬
 if( m_Style==3 ) m_Style = 0;
 if( GetStyle() & WS_DISABLED )
  m_Style = 3; //½ûֹ״̬
 //¸ù¾Ý״̬µ÷Õû±ß¿òÑÕÉ«ºÍÎÄ×ÖÑÕÉ«
 COLORREF bColor, fColor; //bColorΪ±ß¿òÑÕÉ«£¬fColorΪÎÄ×ÖÑÕÉ«
 switch( m_Style )
 {
 case 0: bColor = RGB(192,192,192); fColor = m_ForeColor; break; //Õý³£°´Å¥
 case 1: bColor = RGB(255,255,255); fColor = m_ForeColor; break; //Êó±ê½øÈëʱ°´Å¥
 case 2: bColor = RGB(192,192,192); fColor = m_ForeColor; break; //°´Ïµİ´Å¥
 case 3: bColor = m_BackColor; fColor = m_LockForeColor; break; //Ëø¶¨µÄ°´Å¥
 }
 //»æÖÆ°´Å¥±³¾°
 CBrush Brush;
 Brush.CreateSolidBrush( m_BackColor ); //±³¾°Ë¢
 pDC->SelectObject( &Brush );
 CPen Pen;
 Pen.CreatePen(PS_SOLID, 1, bColor );
 pDC->SelectObject( &Pen );
 pDC->RoundRect(&m_ButRect,CPoint(5,5)); //»­Ô²½Ç¾ØÐÎ
 //»æÖÆ°´Å¥°´ÏÂʱµÄ±ß¿ò
 if( m_Style!=2 )
 {
  CRect Rect;
  Rect.SetRect( m_ButRect.left+2, m_ButRect.top+1, m_ButRect.right, m_ButRect.bottom );
  pDC->DrawEdge( &Rect, BDR_RAISEDINNER, BF_RECT ); //»­±ß¿ò
 }
 //»æÖÆ°´Å¥ÎÄ×Ö
 pDC->SetTextColor( fColor );  //»­ÎÄ×Ö
 pDC->SetBkMode( TRANSPARENT );
 pDC->DrawText( m_strText, &m_ButRect, DT_SINGLELINE | DT_CENTER
  | DT_VCENTER | DT_END_ELLIPSIS);
 //»æÖÆÓµÓн¹µã°´Å¥µÄÐéÏß¿ò
 if( GetFocus()==this )
 {
  CRect Rect;
  Rect.SetRect( m_ButRect.left+3, m_ButRect.top+2, m_ButRect.right-3, m_ButRect.bottom-2 );
  pDC->DrawFocusRect( &Rect ); //»­ÓµÓн¹µãµÄÐéÏß¿ò
 }
}

////////////////////////////////////////////////////////////////////////
//½Ó¿Úº¯Êý

//ÉèÖð´Å¥Îı¾
void CMyButton::SetText(CString str)
{
 m_strText = _T("");
 SetWindowText(str);
}

//ÉèÖÃÎı¾ÑÕÉ«
void CMyButton::SetForeColor(COLORREF color)
{
 m_ForeColor = color;
 Invalidate();
}

//ÉèÖñ³¾°ÑÕÉ«
void CMyButton::SetBkColor(COLORREF color)
{
 m_BackColor = color;
 Invalidate();
}

//ÉèÖÃ×ÖÌå(×ÖÌå¸ß¶È¡¢×ÖÌåÃû)
void CMyButton::SetTextFont(int FontHight,LPCTSTR FontName)
{
 if ( p_Font ) delete p_Font; //ɾ³ý¾É×ÖÌå
 p_Font = new CFont;
 p_Font->CreatePointFont( FontHight, FontName ); //´´½¨ÐÂ×ÖÌå
 SetFont( p_Font );    //ÉèÖÃ×ÖÌå
}

抱歉!评论已关闭.