123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- // SkinButton.cpp : implementation file
- //
- #include "stdafx.h"
- //#include "dialog.h"
- #include "SkinButton.h"
- #include "EnBitmap.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- extern CToolTipCtrl g_ToolTip;
- /////////////////////////////////////////////////////////////////////////////
- // CSkinButton
- CSkinButton::CSkinButton()
- {
- m_bMouseIn = m_bDown = FALSE;
- m_nStyle=STYLE_BITMAP;
- }
- CSkinButton::~CSkinButton()
- {
- }
- BEGIN_MESSAGE_MAP(CSkinButton, CButton)
- //{{AFX_MSG_MAP(CSkinButton)
- ON_WM_PAINT()
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- ON_WM_ERASEBKGND()
- ON_WM_MOUSEMOVE()
- ON_WM_LBUTTONDBLCLK()
- ON_WM_SETFOCUS()
- ON_WM_KILLFOCUS()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CSkinButton message handlers
- void CSkinButton::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
- m_bFocus = GetFocus()->GetSafeHwnd() == m_hWnd;
- m_bEnable = !(GetWindowLong(m_hWnd,GWL_STYLE) & WS_DISABLED);
- CEnBitmap bmp;
- if (m_nStyle & STYLE_BITMAP)
- {
- if ( !m_bEnable )
- {
- //disable
- m_bmpButton.DrawImage(bmp,4,1,4,1);
- }
- else
- if ( m_bMouseIn &&m_bDown )
- {
- //down
- m_bmpButton.DrawImage(bmp,3,1,4,1);
- }
- else if ( m_bMouseIn )
- {
- //hover
- m_bmpButton.DrawImage(bmp,2,1,4,1);
- }
- else if ( m_bFocus )
- {
- //normal with focus
- m_bmpButton.DrawImage(bmp,1,1,4,1);
- }
- else
- {
- //normal
- m_bmpButton.DrawImage(bmp,1,1,4,1);
- }
- //设置区域
- SetWindowRgn(bmp.BitmapToRegion(RGB(255,0,255)), TRUE );
- HRGN hRgn = CreateRectRgn( 0, 0, 0, 0);
- GetWindowRgn(hRgn);
- //选入位图绘画区域
- ::SelectClipRgn(dc.GetSafeHdc(), hRgn);
- bmp.Draw( &dc, CRect(0,0,bmp.GetWidth(),bmp.GetHeight()));
- dc.SelectClipRgn(NULL);
- DeleteObject(hRgn);
- DrawText( &dc ,bmp.GetRect() ,RGB(0,0,0));
- }
- else if (m_nStyle & STYLE_TOOLBAR)
- {
- CRect rcButton;
- GetClientRect(&rcButton);
- CRect rcImage,rcText;
- rcImage=m_bmpButton.GetRect();
- int nMaigin=(rcButton.Height()- rcImage.Height())/2;
- rcImage.OffsetRect(nMaigin,nMaigin);
- rcText=rcButton;
- rcText.left =rcImage.right ;
- //先更新背景
- CRect rc=rcButton;
- ClientToScreen(&rcButton);
- GetParent()->ScreenToClient(&rcButton);
- GetParent()->RedrawWindow(&rcButton,NULL,RDW_ERASE|RDW_UPDATENOW |RDW_INVALIDATE|RDW_NOCHILDREN );
- rc.DeflateRect(1,1);
- if ( !m_bEnable )
- {
- //disable
- }
- else
- if ( m_bMouseIn &&m_bDown )
- {
- //down
- dc.Draw3dRect(&rc,GetSysColor(COLOR_3DDKSHADOW),GetSysColor(COLOR_3DHILIGHT));
- }
- else if ( m_bMouseIn )
- {
- dc.Draw3dRect(&rc,GetSysColor(COLOR_3DHILIGHT),GetSysColor(COLOR_3DDKSHADOW));
- }
- else if ( m_bFocus )
- {
- }
- else
- {
- }
- m_bmpButton.TransparentBlt(dc, &rcImage,RGB(255,0,255));
- DrawText( &dc ,rcText,m_crText);
- }
- }
- void CSkinButton::LoadBitmap(LPCTSTR szImagePath)
- {
- m_bmpButton.LoadImage(szImagePath);
- }
- BOOL CSkinButton::LoadBitmap(UINT uIDRes,COLORREF crBack)
- {
- return m_bmpButton.LoadBitmap(uIDRes);
- }
- void CSkinButton::OnLButtonDown(UINT nFlags, CPoint point)
- {
- m_bDown = TRUE;
- m_bMouseIn = TRUE;
- Invalidate();
- UpdateWindow();
- SetFocus();
- }
- void CSkinButton::OnLButtonUp(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- ReleaseCapture();
- m_bDown = FALSE;
- if ( m_bMouseIn )
- {
- m_bMouseIn = FALSE;
- //GetRegion();
- Invalidate();
- UpdateWindow();
- GetParent()->SendMessage( WM_COMMAND, GetDlgCtrlID(), (LPARAM)m_hWnd );
- }
- else
- {
- //GetRegion();
- Invalidate();
- UpdateWindow();
- }
- }
- BOOL CSkinButton::OnEraseBkgnd(CDC* pDC)
- {
- return TRUE;
- }
- void CSkinButton::OnMouseMove(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- if (m_nStyle & STYLE_BITMAP)
- {
- HRGN hRgn = CreateRectRgn(0, 0, 0, 0);
- GetWindowRgn(hRgn);
- if ( PtInRegion( hRgn, point.x, point.y ))
- {
- ::SetCursor(AfxGetApp()->LoadCursor(IDC_HANDCUR ));
- if ( !m_bMouseIn)
- {
- m_bMouseIn = TRUE;
- Invalidate();
- UpdateWindow();
- SetCapture();
- }
- }
- else
- {
- if ( m_bMouseIn)
- {
- m_bMouseIn = FALSE;
- //GetRegion();
- Invalidate();
- UpdateWindow();
- if ( !m_bDown )
- ReleaseCapture();
- }
- }
- DeleteObject(hRgn);
- }
- else if (m_nStyle & STYLE_TOOLBAR)
- {
- CRect rc;
- GetClientRect(&rc);
- if ( rc.PtInRect( point ))
- {
- //移入
- if ( !m_bMouseIn) {
- m_bMouseIn = TRUE;
- Invalidate();
- UpdateWindow();
- SetCapture();
- }
- }
- else
- {
- //移出
- if ( m_bMouseIn) {
- m_bMouseIn = FALSE;
- //GetRegion();
- Invalidate();
- UpdateWindow();
- if ( !m_bDown )
- ReleaseCapture();
- }
- }
- }
- }
- void CSkinButton::OnLButtonDblClk(UINT nFlags, CPoint point)
- {
- }
- void CSkinButton::OnSetFocus(CWnd* pOldWnd)
- {
-
- }
- void CSkinButton::OnKillFocus(CWnd* pNewWnd)
- {
-
- }
- BOOL CSkinButton::DrawText(CDC *pDC,CRect rc ,COLORREF crText)
- {
- CRect r;
- CString str;
- CRect rcButton;
- CString strText;
- UINT nId=GetDlgCtrlID( ) ;
- if (strText.LoadString(nId)) {
- CFont *ofont;
- ofont = pDC->SelectObject( GetParent()->GetFont() );
- pDC->SetTextColor(crText);
- pDC->SetBkMode(TRANSPARENT);
- pDC->DrawText( strText, rc, DT_CENTER | DT_SINGLELINE | DT_VCENTER );
- pDC->SelectObject(ofont);
- }
- return TRUE;
- }
- HBITMAP CSkinButton::SetBitmap(HBITMAP hBitmap)
- {
- return m_bmpButton.SetBitmap(hBitmap) ;
- }
- void CSkinButton::SetTextColor(COLORREF cr)
- {
- m_crText=cr;
- }
|