• 로그인 함 해보끄나?

  • Sarangnamu.net June 17, 2003
    Home Login Profile Study Open Project Q&A Google Code
    CButtonExFlat
    Last Modify : 27 December, 2010(02:42)
    // ButtonExFlat.cpp : implementation file
    //
    
    // DATE		: March 9, 2007
    // CODER	: Kurome (aucd29@gmail.com)
    // VERSION	: 1.0
    //
    //
    
    #include "stdafx.h"
    #include "propertyTest.h"
    #include "ButtonExFlat.h"
    
    
    // CButtonExFlat
    
    IMPLEMENT_DYNAMIC(CButtonExFlat, CButton)
    
    CButtonExFlat::CButtonExFlat()
    : m_szWindowText(_T(""))
    , m_bIsLoad(false)
    , m_szFont(L"Tahoma")		// Vista에서는 폰트가 다르므로 예상치 못한 결과가 생길 수 있으므로
    , m_bFontBold(false)
    , m_nHeight(13)
    , m_nAlign(DT_LEFT)
    , m_nLeftPadding(20)
    , m_nMouseStatus(_MOUSEOUT)
    , m_bPressButton(false)
    {
    	m_crFontColor = RGB(0, 0, 0);
    	m_crBackground = RGB(255, 255, 255);
    	m_nAlign = DT_VCENTER | DT_SINGLELINE | DT_LEFT;
    	m_crMiniCircle = RGB(153, 204, 51);
    
    	m_crActiveBackground	= RGB(231, 239, 247);
    	m_crActiveBorder		= RGB(156, 181, 231);
    	m_crOverBackground		= RGB(255, 255, 204);
    	m_crOverBorder			= RGB(255, 204, 102);
    
    	//m_brCircle.CreateSolidBrush();
    	m_pen.CreatePen(PS_SOLID,3,m_crMiniCircle);
    }
    
    CButtonExFlat::~CButtonExFlat()
    {
    	m_oTextFont.DeleteObject();
    	mBitmap.DeleteObject();
    	mDC.DeleteDC();
    }
    
    
    BEGIN_MESSAGE_MAP(CButtonExFlat, CButton)
    	ON_WM_PAINT()
    	ON_WM_MOUSEMOVE()
    	ON_WM_LBUTTONDOWN()
    	ON_WM_LBUTTONUP()
    	ON_WM_LBUTTONDBLCLK()
    END_MESSAGE_MAP()
    
    
    
    // CButtonExFlat message handlers
    
    
    
    void CButtonExFlat::OnPaint()
    {
    	CPaintDC dc(this); // device context for painting
    	InitObject(&dc);
    
    	switch(m_nMouseStatus)
    	{
    	case _MOUSEOUT:
    		mDC.FillSolidRect(m_rect, RGB(255, 255, 255));		
    		break;
    	case _MOUSEDOWN:
    		mDC.FillSolidRect(m_rect, m_crActiveBackground);
    		mDC.FrameRect(m_rect, &CBrush(m_crActiveBorder));
    		break;
    	case _MOUSEOVER:
    		mDC.FillSolidRect(m_rect, m_crOverBackground);
    		mDC.FrameRect(m_rect, &CBrush(m_crOverBorder));
    		break;
    	default:
    		break;
    	}
    
    	mDC.SelectObject(m_pen);
    	mDC.Ellipse(m_rectCircle);
    	mDC.DrawText(m_szWindowText,m_rectText,m_nAlign);
    	dc.BitBlt(0, 0, m_rect.Width(), m_rect.Height(), &mDC, 0, 0, SRCCOPY);
    }
    
    void CButtonExFlat::OnMouseMove(UINT nFlags, CPoint point)
    {
    	if (m_rect.PtInRect(point))
    	{
    		if (m_bPressButton)
    		{
    			// 마우스는 클릭한 상태에서 버튼 위치일 경우
    			m_nMouseStatus = _MOUSEDOWN;
    			Invalidate();
    		}
    		else
    		{
    			// 마우스는 클릭하지 않은 상태 에서의 버튼 위치 일 경우	
    			m_nMouseStatus = _MOUSEOVER;
    			Invalidate();
    			SetCapture();
    		}
    	}
    	else
    	{
    		m_nMouseStatus = _MOUSEOUT;
    		Invalidate();
    
    		if (!m_bPressButton)
    		{
    			ReleaseCapture();
    		}
    	}
    
    	CButton::OnMouseMove(nFlags, point);
    }
    
    void CButtonExFlat::InitObject(CDC* pDC)
    {
    	if (!m_bIsLoad)
    	{
    		try
    		{
    			SetFont();
    			GetWindowText(m_szWindowText);
    			GetClientRect(m_rect);
    			mDC.CreateCompatibleDC(pDC);
    			mBitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
    
    			mDC.SetBkMode(TRANSPARENT);
    			mDC.SelectObject(&mBitmap);
    			mDC.SelectObject(&m_oTextFont);
    			mDC.SetTextColor(m_crFontColor);
    			mDC.FillSolidRect(m_rect, m_crBackground);
    
    			m_rectText		= m_rect;
    			m_rectText.left = m_nLeftPadding;
    
    			int nPos = static_cast<int>(m_rect.Height() / 2.0) - 1;
    			m_rectCircle.SetRect(8, nPos, 12, nPos + 4);
    		}
    		catch (CResourceException* e)
    		{
    			e->ReportError();
    			e->Delete();
    		}
    
    		// 재 로드를 방지하기 위해서 작업
    		m_bIsLoad = true;
    	}
    }
    
    void CButtonExFlat::SetFont(void)
    {
    	// 시스템 폰트 가져와서 적용하기
    	LOGFONT hfont;
    	memset(&hfont, 0, sizeof(LOGFONT));
    	wcscpy_s(hfont.lfFaceName, m_szFont);
    	hfont.lfHeight = m_nHeight;
    
    	if (m_bFontBold)
    	{
    		hfont.lfWeight = FW_BOLD;
    	}	
    
    	m_oTextFont.CreateFontIndirect(&hfont);
    }
    
    void CButtonExFlat::SetFontStyle(CString szFont, int nHeight, COLORREF crColor)
    {
    	// 폰트 변경
    	m_szFont = szFont;
    
    	// 폰트 크기 변경
    	if (nHeight)
    	{
    		m_nHeight = nHeight;
    	}
    
    	// 폰트 색 변경
    	if (crColor)
    	{
    		m_crFontColor = crColor;
    	}
    }
    
    void CButtonExFlat::SetFontBold(void)
    {
    	m_bFontBold = true;
    }
    
    void CButtonExFlat::SetFontAlign(int nAlign)
    {
    	m_nAlign = nAlign;
    }
    
    void CButtonExFlat::OnLButtonDown(UINT nFlags, CPoint point)
    {
    	m_bPressButton = true;
    	m_nMouseStatus = _MOUSEDOWN;
    	Invalidate();
    
    	CButton::OnLButtonDown(nFlags, point);
    }
    
    void CButtonExFlat::OnLButtonUp(UINT nFlags, CPoint point)
    {
    	m_nMouseStatus = _MOUSEOUT;
    	Invalidate();
    
    	CButton::OnLButtonUp(nFlags, point);
    
    	ReleaseCapture();
    	m_bPressButton = false;
    }
    
    void CButtonExFlat::OnLButtonDblClk(UINT nFlags, CPoint point)
    {
    	OnLButtonDown(nFlags, point);
    	CButton::OnLButtonDblClk(nFlags, point);
    }
    
    
    #pragma once
    
    enum ButtonMouseStatus
    {
    	_MOUSEOUT = 0,
    	_MOUSEOVER,
    	_MOUSEDOWN
    };
    
    // CButtonExFlat
    
    class CButtonExFlat : public CButton
    {
    	DECLARE_DYNAMIC(CButtonExFlat)
    
    public:
    	CButtonExFlat();
    	virtual ~CButtonExFlat();
    
    protected:
    	DECLARE_MESSAGE_MAP()
    
    	int m_nHeight, m_nAlign, m_nLeftPadding;
    	CDC mDC;
    	bool m_bFontBold, m_bIsLoad, m_bPressButton;
    	CRect m_rect, m_rectText, m_rectCircle;
    	CFont m_oTextFont;
    	CString m_szWindowText, m_szFont;
    	CBitmap mBitmap;
    	COLORREF m_crFontColor, m_crBackground;
    	COLORREF m_crMiniCircle;
    	void SetFont(void);
    	int m_nMouseStatus;
    	CBrush m_brCircle;
    	CPen m_pen;
    
    	COLORREF m_crActiveBackground;
    	COLORREF m_crActiveBorder;
    	COLORREF m_crOverBackground;
    	COLORREF m_crOverBorder;
    
    
    public:
    	afx_msg void OnPaint();
    	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
    	virtual void InitObject(CDC* pDC);
    
    	void SetFontStyle(CString szFont, int nHeight = 0, COLORREF crColor = NULL);
    	void SetFontBold(void);
    	void SetFontAlign(int nAlign = DT_LEFT);
    	void SetOpaque(void);
    	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
    	afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
    };
    
    
    

    Comment


    입력하3 1508772169



    Locations of visitors to this page