⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mybutton.cpp

📁 mfc做的计算器 界面简单 功能基本 初级作品
💻 CPP
字号:
// MyButton.cpp : implementation file
//

#include "stdafx.h"
#include "temp.h"
#include "MyButton.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()

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 );    //夋桳徟揰揑嫊瀥
    }
}
/////////////////////////////////////////////////////////////////////////////
// CMyButton message handlers

void CMyButton::PreSubclassWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	ModifyStyle( 0, BS_OWNERDRAW );			//抲埪BUTTON懏惈帺夋幃
	CButton::PreSubclassWindow();
}

void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	// TODO: Add your code to draw the specified item
	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) 
{
	// TODO: Add your message handler code here and/or call default
			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) 
{
	// TODO: Add your message handler code here and/or call default
	m_Style = 2;
    Invalidate();        //廳埪
	CButton::OnLButtonDown(nFlags, point);
}

void CMyButton::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_Style = 1;
    Invalidate();        //廳?埪?
	CButton::OnLButtonUp(nFlags, point);
}

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 );                //抲帤懱
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -