关于三台按钮的问题

小弟做了一个三态按钮的类,根据贴的图的不同分成ICON和BITMAP。通过CButtonEx()、LoadButtonIcon()、LoadButtonBitmap()加载贴图。重载DrawItem()来实现贴图。但是用BITMAP贴的图是一片空白,这是怎么回事?还有,这个类好像没有响应WM_LBUTTONUP啊。

BITMAP 贴图代码:
this->SetBitmap((HBITMAP)A.GetSafeHandle());


以下是全部代码:
CButtonEx.h
#pragma once
#include "stdafx.h"
#include "resource.h"

#ifndef CBUTTONEX_
#define CBUTTONEX_
class CButtonEx :public CButton
{
public:
CButtonEx();
CButtonEx(UINT hIcon,bool isIconIc);
CButtonEx(UINT hIconU,UINT hIconD,UINT hIconF,UINT hIconDs,bool isIconIc);

~CButtonEx();

public:
void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
int LoadButtonIcon(UINT IconId);
int LoadButtonIcon(UINT IconIdu,UINT IconIdd,UINT IconIdf,UINT IconIdds);
int LoadButtonBitmap(UINT IconIdu,UINT IconIdd,UINT IconIdf,UINT IconIdds);
int DisableButton();
int UnDisableButton();
bool IsDisable();

private:
bool b,isDisable;
HICON Icon1,Icon2,Icon3,Icon4;
CBitmap A,B,C,D;
bool isIconIc;

virtual afx_msg void OnMouseHover(UINT nFlags,CPoint point);
virtual afx_msg void OnMouseLeave();
virtual afx_msg void OnMouseMove(UINT nFlags, CPoint point);
virtual afx_msg void OnLButtonDown(UINT nFlags,CPoint point);
virtual afx_msg void OnLButtonUp(UINT nFlags,CPoint point);
virtual afx_msg void OnPaint();
DECLARE_MESSAGE_MAP()
};
#endif

CButtonEx.cpp
#include "stdafx.h"
#include "CButtonEx.h"


BEGIN_MESSAGE_MAP(CButtonEx,CButton)
ON_WM_MOUSELEAVE()
ON_WM_MOUSEMOVE()
ON_WM_MOUSEHOVER()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

CButtonEx::~CButtonEx()
{
CObject::~CObject();
}
void CButtonEx::OnMouseHover(UINT nFlags, CPoint point)
{
if(isIconIc==true){
LPRECT rect=new RECT;
this->GetClientRect(rect);
CClientDC dc(this);
DrawIconEx (dc.GetSafeHdc(),0,0,Icon3,\
rect->right-rect->left,rect->bottom-rect->top,0,0,DI_NORMAL);
}
else this->SetBitmap((HBITMAP)C.GetSafeHandle());
}
void CButtonEx::OnMouseLeave()
{
if(isIconIc==true){
LPRECT rect=new RECT;
this->GetClientRect(rect);
CClientDC dc(this);
DrawIconEx (dc.GetSafeHdc(),0,0,Icon1,\
rect->right-rect->left,rect->bottom-rect->top,0,0,DI_NORMAL);
}
else this->SetBitmap((HBITMAP)A.GetSafeHandle());

b=false;
}
CButtonEx::CButtonEx()
{
    b=false;
isIconIc=true;
}
void CButtonEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if(isIconIc==true){
LPRECT rect=new RECT;
this->GetClientRect(rect);
CClientDC dc(this);
DrawIconEx (dc.GetSafeHdc(),0,0,Icon1,\
rect->right-rect->left,rect->bottom-rect->top,0,0,DI_NORMAL);
}
else this->SetBitmap((HBITMAP)A.GetSafeHandle());

}

void CButtonEx::OnMouseMove(UINT nFlags, CPoint point)
{
if(b==false){
TRACKMOUSEEVENT tme;
tme.cbSize =sizeof( tme);
tme.dwFlags =TME_LEAVE | TME_HOVER;
tme.dwHoverTime = HOVER_DEFAULT;
tme.hwndTrack =m_hWnd;
::TrackMouseEvent( &tme);
b=true;
}
}
void CButtonEx::OnLButtonDown(UINT nFlags,CPoint point)
{
if(isIconIc==true){
LPRECT rect=new RECT;
this->GetClientRect(rect);
CClientDC dc(this);
DrawIconEx (dc.GetSafeHdc(),0,0,Icon2,\
rect->right-rect->left,rect->bottom-rect->top,0,0,DI_NORMAL);
}
else this->SetBitmap((HBITMAP)B.GetSafeHandle());
}
void CButtonEx::OnPaint()
{
if(isDisable==true){
if(isIconIc==true){
LPRECT rect=new RECT;
this->GetClientRect(rect);
CClientDC dc(this);
DrawIconEx (dc.GetSafeHdc(),0,0,Icon4,\
rect->right-rect->left,rect->bottom-rect->top,0,0,DI_NORMAL);
}
else this->SetBitmap((HBITMAP) D.GetSafeHandle());
}
else{
if(isIconIc==true){
LPRECT rect=new RECT;
this->GetClientRect(rect);
CClientDC dc(this);
DrawIconEx (dc.GetSafeHdc(),0,0,Icon1,\
rect->right-rect->left,rect->bottom-rect->top,0,0,DI_NORMAL);
}
else this->SetBitmap((HBITMAP)A.GetSafeHandle());
}
}
CButtonEx::CButtonEx(UINT hIcon,bool isIconIc)
{
b=false;
if(isIconIc==true){Icon1=AfxGetApp()->LoadIconA(hIcon);this->isIconIc=true;}
else A.LoadBitmapA(hIcon);
}
CButtonEx::CButtonEx(UINT hIconU,UINT hIconD,UINT hIconF,UINT hIconDs,bool isIconIc)
{
b=false;
if(isIconIc==true){this->isIconIc=true;
if(hIconU==NULL) { TRACE0("error #1 hIconU is NULL!"); ExitProcess(0);}
Icon1=AfxGetApp()->LoadIconA(hIconU);
if(hIconD!=NULL){ Icon2=AfxGetApp()->LoadIconA(hIconD);}
if(hIconF!=NULL){Icon3=AfxGetApp()->LoadIconA(hIconF);}
if(hIconDs!=NULL){ Icon4=AfxGetApp()->LoadIconA(hIconDs);}}
else {
if(hIconU==NULL) { TRACE0("error #1 hIconU is NULL!"); ExitProcess(0);}
A.LoadBitmapA(hIconU);
if(hIconD!=NULL){ B.LoadBitmapA(hIconD);}
if(hIconF!=NULL){C.LoadBitmapA(hIconF);}
if(hIconDs!=NULL){ D.LoadBitmapA(hIconDs);}
}

}
int CButtonEx::LoadButtonIcon(UINT IconId)
{
try{Icon1=AfxGetApp()->LoadIconA(IconId);}
catch(...){return -1;}
isIconIc=true;
return 0;
}
int CButtonEx::LoadButtonBitmap(UINT IconIdu,UINT IconIdd,UINT IconIdf,UINT IconIdds)
{
if(IconIdu==NULL) { TRACE0("error #1 hIconU is NULL!"); ExitProcess(0);}
A.LoadBitmapA(IconIdu);
if(IconIdd!=NULL){ B.LoadBitmapA(IconIdd);}
if(IconIdf!=NULL){C.LoadBitmapA(IconIdf);}
if(IconIdds!=NULL){ D.LoadBitmapA(IconIdds);}
isIconIc=false;
return 0;
}
int CButtonEx::LoadButtonIcon(UINT IconIdu,UINT IconIdd,UINT IconIdf,UINT IconIdds)
{
try{
if(IconIdu==NULL) { TRACE0("error #1 hIconU is NULL!"); ExitProcess(0);}
Icon1=AfxGetApp()->LoadIconA(IconIdu);
if(IconIdd!=NULL){ Icon2=AfxGetApp()->LoadIconA(IconIdd);}
if(IconIdf!=NULL){Icon3=AfxGetApp()->LoadIconA(IconIdf);}
if(IconIdds!=NULL){ Icon4=AfxGetApp()->LoadIconA(IconIdds);}
}
catch(...){return -1;}
isIconIc=true;
return 0;
}
void CButtonEx::OnLButtonUp(UINT nFlags,CPoint point)
{
if(isIconIc==true){
LPRECT rect=new RECT;
this->GetClientRect(rect);
CClientDC dc(this);
DrawIconEx (dc.GetSafeHdc(),0,0,Icon1,\
rect->right-rect->left,rect->bottom-rect->top,0,0,DI_NORMAL);}
else this->SetBitmap((HBITMAP)A.GetSafeHandle());
}

int CButtonEx::DisableButton()
{
isDisable=true;
return 0;
}
int CButtonEx::UnDisableButton()
{
isDisable=false;
return 0;
}
bool CButtonEx::IsDisable()
{
return isDisable;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值