
按钮自绘,将按钮区域分成三部分,左边、右边、中间都由贴图绘制,可用于手动进度条按钮,或者左右选择项按钮
cpp代码部分:
-
-
-
- #include "stdafx.h"
- #include "CRedrawButtonDemo.h"
- #include "LRSkinButton.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
-
-
-
- CLRSkinButton::CLRSkinButton()
- {
- m_nWidth = 17;
- m_nHeight = 16;
- m_bDrawBorder = TRUE;
- }
-
- CLRSkinButton::~CLRSkinButton()
- {
- }
-
-
- BEGIN_MESSAGE_MAP(CLRSkinButton, CButton)
-
-
-
- ON_WM_CAPTURECHANGED()
- ON_WM_MOUSEMOVE()
- ON_WM_SETCURSOR()
- ON_WM_KILLFOCUS()
- END_MESSAGE_MAP()
-
- void CLRSkinButton::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
- {
-
-
-
- CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
- HDC hDC = pDC->GetSafeHdc();
-
- CRect rectItem(lpDrawItemStruct->rcItem);
- m_nHeight = rectItem.Height();
- m_nWidth = 26;
- CRect rectCenter(rectItem.left+m_nWidth, rectItem.top, rectItem.right-m_nWidth, rectItem.bottom);
- CRect rectLeft(rectItem.left, rectItem.top, rectItem.left+m_nWidth, rectItem.top+m_nHeight);
- CRect rectRight(rectItem.right-m_nWidth,rectItem.top,rectItem.right,rectItem.bottom);
- static int nClrWidth = rectCenter.Width()/10;
- CRect rectColor(rectCenter.left, rectCenter.top, rectCenter.left, rectCenter.bottom);
- rectCenter.DeflateRect(0,1,0,1);
-
-
- HPEN hOldPen;
- UINT action, state;
- action = lpDrawItemStruct->itemAction;
- state = lpDrawItemStruct->itemState;
-
- BOOL bIsPressed, bIsFocus, bIsDisabled;
- bIsPressed = state & ODS_SELECTED;
- bIsDisabled = state & ODS_DISABLED;
- bIsFocus = (state & ODS_FOCUS) == ODS_FOCUS;
-
- ::SetBkColor(hDC, RGB(22, 33, 55));
-
- if (bIsPressed)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- POINT pos;
- GetCursorPos( &pos );
- ScreenToClient(&pos);
- if (PtInRect( &rectLeft, pos))
- {
- TRACE(_T("Clicked Left...\n"));
- nClrWidth -= 5;
- if (nClrWidth <=0)
- {
- nClrWidth = 0;
- }
- }
- if (PtInRect( &rectRight, pos))
- {
- TRACE(_T("Clicked Right...\n"));
- nClrWidth += 5;
- if (nClrWidth >= rectCenter.Width())
- {
- nClrWidth = rectCenter.Width();
- }
- }
-
- }
- else
- {
- if(!m_bIsFlat || (m_bIsFlat && m_MouseOnButton))
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- else{
- if(m_bDrawBorder)
- {
- HPEN penBorder = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_BTNTEXT));
- hOldPen = (HPEN)SelectObject(hDC,penBorder);
- SelectObject(hDC,GetStockObject(NULL_BRUSH));
-
- Rectangle(hDC,rectItem.left, rectItem.top, rectItem.right, rectItem.bottom);
- SelectObject(hDC, hOldPen);
- DeleteObject(penBorder);
- }
- }
- }
-
-
-
-
- CRect rect(rectCenter);
-
- CDC dcMem;
- dcMem.CreateCompatibleDC(pDC);
-
-
- CBitmap bmp;
- BITMAP bitmap;
-
- bmp.LoadBitmap(m_nBmpBK);
- bmp.GetBitmap( &bitmap );
- CBitmap* pOldBmp = dcMem.SelectObject(&bmp);
- pDC->StretchBlt(rectCenter.left, rectCenter.top, rectCenter.Width(), rectCenter.Height(), &dcMem, 0, 0, bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);
- bmp.DeleteObject();
-
-
- bmp.LoadBitmap( m_nBmpLeft );
- dcMem.SelectObject( &bmp );
- bmp.GetBitmap( &bitmap );
-
- pDC->StretchBlt(rectLeft.left, rectLeft.top, rectLeft.Width(), rectLeft.Height(), &dcMem, 0, 0, bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);
- bmp.DeleteObject();
-
-
- bmp.LoadBitmap( m_nBmpRight );
- bmp.GetBitmap( &bitmap );
- dcMem.SelectObject( &bmp );
-
- pDC->StretchBlt(rectRight.left, rectRight.top, rectRight.Width(), rectRight.Height(), &dcMem, 0, 0, bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);
- bmp.DeleteObject();
- dcMem.SelectObject(pOldBmp);
-
-
- bmp.LoadBitmap(IDB_BMP_BTNLEFT);
-
- CBrush brush(RGB(255, 255, 0));
- rectColor.right = rectColor.left+nClrWidth;
- pDC->FillRect(rectColor, &brush);
-
-
-
- bmp.DeleteObject();
-
-
- rect = rectCenter;
-
- rect.InflateRect(0,1,0,1);
- pDC->SetTextColor(RGB(0,0,0));
-
-
-
- CString str;
- GetWindowText( str );
- COLORREF clr = GetSysColor( COLOR_3DHILIGHT);
- clr = RGB(55, 155, 55);
- if (bIsPressed)
- {
- clr = RGB(155, 55, 55);
- }
- else if (bIsFocus )
- {
- clr = RGB(55, 55, 155);
-
- HPEN penBorder = CreatePen(PS_SOLID, 1, RGB(255,0,255));
- hOldPen = (HPEN)SelectObject(hDC,penBorder);
- SelectObject(hDC,GetStockObject(NULL_BRUSH));
-
- Rectangle(hDC,rect.left, rect.top, rect.right, rect.bottom);
- SelectObject(hDC, hOldPen);
- DeleteObject(penBorder);
- }
- pDC->SetBkColor(clr);
- pDC->SetTextColor( RGB(255, 0, 0) );
- pDC->DrawText(str, str.GetLength(),rect, DT_CENTER | DT_VCENTER| DT_SINGLELINE);
-
-
-
-
- }
-
- void CLRSkinButton::SetBtnBmps( UINT uBmpBK, UINT uBmpLeft, UINT uBmpRight )
- {
- m_nBmpBK = uBmpBK;
- m_nBmpLeft = uBmpLeft;
- m_nBmpRight = uBmpRight;
- }
-
- void CLRSkinButton::PreSubclassWindow()
- {
- UINT nBS = GetButtonStyle();
- if(nBS & BS_DEFPUSHBUTTON)
- m_bDefaultBtn = TRUE;
- else
- m_bDefaultBtn = FALSE;
- SetButtonStyle(nBS | BS_OWNERDRAW);
-
- CButton::PreSubclassWindow();
- }
-
-
- void CLRSkinButton::OnCaptureChanged(CWnd *pWnd)
- {
- if(m_MouseOnButton == TRUE)
- {
- ReleaseCapture();
- Invalidate();
- }
-
- CButton::OnCaptureChanged(pWnd);
- }
-
- void CLRSkinButton::OnMouseMove(UINT nFlags, CPoint point)
- {
- CButton::OnMouseMove(nFlags, point);
-
- HWND hParent;
-
-
- if (nFlags & MK_LBUTTON && !m_MouseOnButton)
- return;
-
-
- if (m_bIsFlat)
- {
- hParent = ::GetParent(m_hWnd);
- if ((::GetCapture() != m_hWnd) && (hParent != NULL))
- {
- m_MouseOnButton = TRUE;
- SetCapture();
- Invalidate();
- }
- else
- {
- RECT rc;
- GetClientRect(&rc);
- if (!PtInRect(&rc,point))
- {
-
- if (m_MouseOnButton == TRUE)
- {
- m_MouseOnButton = FALSE;
- Invalidate();
- }
-
- if (!(nFlags & MK_LBUTTON))
- ReleaseCapture();
- }
- }
- }
- }
-
- BOOL CLRSkinButton::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
- {
- if (m_hBtnCursor != NULL)
- {
- ::SetCursor(m_hBtnCursor);
- return TRUE;
- }
-
- return CButton::OnSetCursor(pWnd, nHitTest, message);
- }
-
- void CLRSkinButton::OnKillFocus(CWnd* pNewWnd)
- {
- CButton::OnKillFocus(pNewWnd);
-
- if(!m_bIsFlat)
- {
- m_MouseOnButton = FALSE;
- Invalidate();
- }
- }

vc 按钮自绘,布布扣,bubuko.com
vc 按钮自绘
原文:http://www.cnblogs.com/lidabo/p/3701273.html