对CBitmapButton的扩展

    很久以前的一篇文章《 VC 位图按钮CBitmapButton的使用》,里面用定时器对位图按钮进行移进移出的效果进行显示,不大好。在这里用鼠标事件进行捕获显示,另加一个函数LoadListBitmap实现对连续的资源图片进行截取。

代码如下:

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(00, nWidth, nHeight, &dcSrc, 00, SRCCOPY); 
    dcDest.SelectObject(pOldBmpDest); 
     
    if (nBitmapContainCount > 1
    { 
        m_bitmapSel.CreateCompatibleBitmap(&dcSrc, nWidth, nHeight); 
        pOldBmpDest = dcDest.SelectObject(&m_bitmapSel); 
        dcDest.BitBlt(00, 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(00, nWidth, nHeight, &dcSrc, nWidth * 20, SRCCOPY); 
        dcDest.SelectObject(pOldBmpDest); 
    } 
    if (nBitmapContainCount > 3
    { 
        m_bitmapDisabled.CreateCompatibleBitmap(&dcSrc, nWidth, nHeight); 
        pOldBmpDest = dcDest.SelectObject(&m_bitmapDisabled); 
        dcDest.BitBlt(00, nWidth, nHeight, &dcSrc, nWidth * 30, 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, 00, 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;   
效果如下所示:

  • 1
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 15
    评论
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值