代码如下:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/** * @file KofBitmapButton.h * @brief 图片按钮(四态) * @author 无幻 * @date 2012-5-7 * @details 在CBitmapButton的基础上,增加移进移出效果,增加连续的图像资源 */ #pragma once class CKofBitmapButton : public CBitmapButton { DECLARE_DYNAMIC(CKofBitmapButton) public: CKofBitmapButton(); BOOL LoadListBitmap(UINT nIDBitmapResource, UINT nBitmapContainCount); protected: BOOL m_bTracked; virtual void DrawItem(LPDRAWITEMSTRUCT lpDIS); virtual void PreSubclassWindow(); DECLARE_MESSAGE_MAP() public: afx_msg void OnMouseMove(UINT nFlags, CPoint point); afx_msg void OnMouseLeave(); }; |
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
/** * @file KofBitmapButton.cpp * @brief 图片按钮(四态) * @author 无幻 * @date 2012-5-7 * @details */ #include "stdafx.h" #include "KofBitmapButton.h" IMPLEMENT_DYNAMIC(CKofBitmapButton, CBitmapButton) CKofBitmapButton::CKofBitmapButton() :m_bTracked(FALSE) { } BOOL CKofBitmapButton::LoadListBitmap( UINT nIDBitmapResource, UINT nBitmapContainCount ) { // delete old bitmaps (if present) m_bitmap.DeleteObject(); m_bitmapSel.DeleteObject(); m_bitmapFocus.DeleteObject(); m_bitmapDisabled.DeleteObject(); if (nBitmapContainCount < 1) { TRACE(traceAppMsg, 0, "Failed to load bitmap.\n"); return FALSE; } CBitmap bmpSrc; if (!bmpSrc.LoadBitmap(nIDBitmapResource)) { TRACE(traceAppMsg, 0, "Failed to load bitmap.\n"); return FALSE; // need this one image } BITMAP bmpInfo; bmpSrc.GetBitmap(&bmpInfo); CClientDC dc(this); CDC dcSrc, dcDest; dcSrc.CreateCompatibleDC(&dc); CBitmap *pOldBmpSrc = dcSrc.SelectObject(&bmpSrc); int nWidth = bmpInfo.bmWidth / nBitmapContainCount; int nHeight = bmpInfo.bmHeight; dcDest.CreateCompatibleDC(&dc); m_bitmap.CreateCompatibleBitmap(&dcSrc, nWidth, nHeight); CBitmap *pOldBmpDest = dcDest.SelectObject(&m_bitmap); dcDest.BitBlt(0, 0, nWidth, nHeight, &dcSrc, 0, 0, SRCCOPY); dcDest.SelectObject(pOldBmpDest); if (nBitmapContainCount > 1) { m_bitmapSel.CreateCompatibleBitmap(&dcSrc, nWidth, nHeight); pOldBmpDest = dcDest.SelectObject(&m_bitmapSel); dcDest.BitBlt(0, 0, nWidth, nHeight, &dcSrc, nWidth, 0, SRCCOPY); dcDest.SelectObject(pOldBmpDest); } if (nBitmapContainCount > 2) { m_bitmapFocus.CreateCompatibleBitmap(&dcSrc, nWidth, nHeight); pOldBmpDest = dcDest.SelectObject(&m_bitmapFocus); dcDest.BitBlt(0, 0, nWidth, nHeight, &dcSrc, nWidth * 2, 0, SRCCOPY); dcDest.SelectObject(pOldBmpDest); } if (nBitmapContainCount > 3) { m_bitmapDisabled.CreateCompatibleBitmap(&dcSrc, nWidth, nHeight); pOldBmpDest = dcDest.SelectObject(&m_bitmapDisabled); dcDest.BitBlt(0, 0, nWidth, nHeight, &dcSrc, nWidth * 3, 0, SRCCOPY); dcDest.SelectObject(pOldBmpDest); } dcSrc.SelectObject(pOldBmpSrc); return TRUE; } BEGIN_MESSAGE_MAP(CKofBitmapButton, CBitmapButton) ON_WM_MOUSEMOVE() ON_WM_MOUSELEAVE() END_MESSAGE_MAP() void CKofBitmapButton::DrawItem( LPDRAWITEMSTRUCT lpDIS ) { ASSERT(lpDIS != NULL); // must have at least the first bitmap loaded before calling DrawItem ASSERT(m_bitmap.m_hObject != NULL); // required // use the main bitmap for up, the selected bitmap for down CBitmap* pBitmap = &m_bitmap; UINT state = lpDIS->itemState; if ((state & ODS_SELECTED) && m_bitmapSel.m_hObject != NULL) pBitmap = &m_bitmapSel; //else if ((state & ODS_FOCUS) && m_bitmapFocus.m_hObject != NULL) //pBitmap = &m_bitmapFocus; // third image for focused else if ((state & ODS_DISABLED) && m_bitmapDisabled.m_hObject != NULL) pBitmap = &m_bitmapDisabled; // last image for disabled else if (m_bTracked && m_bitmapFocus.m_hObject != NULL) pBitmap = &m_bitmapFocus; // third image for focused // draw the whole button CDC* pDC = CDC::FromHandle(lpDIS->hDC); CDC memDC; memDC.CreateCompatibleDC(pDC); CBitmap* pOld = memDC.SelectObject(pBitmap); if (pOld == NULL) return; // destructors will clean up CRect rect; rect.CopyRect(&lpDIS->rcItem); pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY); memDC.SelectObject(pOld); } void CKofBitmapButton::OnMouseMove(UINT nFlags, CPoint point) { if (!m_bTracked) { TRACKMOUSEEVENT tme; tme.cbSize = sizeof(tme); tme.dwFlags = TME_LEAVE; tme.dwHoverTime = 0; tme.hwndTrack = m_hWnd; TrackMouseEvent(&tme); m_bTracked = TRUE; Invalidate(FALSE); } CBitmapButton::OnMouseMove(nFlags, point); } void CKofBitmapButton::OnMouseLeave() { m_bTracked = FALSE; Invalidate(FALSE); CBitmapButton::OnMouseLeave(); } void CKofBitmapButton::PreSubclassWindow() { CBitmapButton::PreSubclassWindow(); ModifyStyle(0, BS_OWNERDRAW); } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include "KofBitmapButton.h" CKofBitmapButton m_Btn; CKofBitmapButton m_Btn2; void CTestButtonDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_BUTTON1, m_Btn); DDX_Control(pDX, IDC_BUTTON2, m_Btn2); } BOOL CTestButtonDlg::OnInitDialog() { CDialog::OnInitDialog(); m_Btn.LoadListBitmap(IDB_BITMAP6, 3); m_Btn.SizeToContent(); m_Btn2.LoadBitmaps(IDB_BITMAP1, IDB_BITMAP2, IDB_BITMAP3, IDB_BITMAP4); m_Btn2.SizeToContent(); return TRUE; } |