小职
2018-07-05
来源 :
阅读 1762
评论 0
摘要:本文主要向大家介绍了VC编程之对CBitmapButton的扩展,通过具体的内容向大家展示,希望对大家学习VC编程有所帮助。
本文主要向大家介绍了VC编程之对CBitmapButton的扩展,通过具体的内容向大家展示,希望对大家学习VC编程有所帮助。
在这里用鼠标事件进行捕获显示,另加一个函数LoadListBitmap实现对连续的资源图片进行截取。
代码如下:
/**
* @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();
};
实现文件如下:
/**
* @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);
}
使用如下:
#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;
}本文由职坐标整理并发布,了解更多内容,请关注职坐标编程语言VC/MFC频道!
喜欢 | 1
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号