关于三台按钮的问题

G1398256223 2013-08-26 12:13:44
小弟做了一个三态按钮的类,根据贴的图的不同分成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;
}
...全文
114 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
G1398256223 2013-08-28
  • 打赏
  • 举报
回复
左下角的按钮消失不见了
G1398256223 2013-08-28
  • 打赏
  • 举报
回复
G1398256223 2013-08-28
  • 打赏
  • 举报
回复
我又写了一段代码,但是还是没有效果啊!
CPaintDC dc(this);
	A.Detach();
A.LoadBitmap(Aaa);

BITMAP bmInfo;
::GetObject( A.m_hObject, sizeof(BITMAP), &bmInfo );
INT nWidth, nHeigh;
nWidth = bmInfo.bmWidth;
nHeigh = bmInfo.bmHeight;

	CDC pDC;
	pDC.CreateCompatibleDC(&dc);
	pDC.SelectObject(&A);
	dc.BitBlt(0, 0, nWidth, nHeigh, &pDC, 0, 0, SRCCOPY);
schlafenhamster 2013-08-26
  • 打赏
  • 举报
回复
CBitmapButton 才 SetBitmap((HBITMAP)A.GetSafeHandle()); 你 是 CButton ,直接 画吧
schlafenhamster 2013-08-26
  • 打赏
  • 举报
回复
bitblt 而已!
G1398256223 2013-08-26
  • 打赏
  • 举报
回复
引用 1 楼 schlafenhamster 的回复:
CBitmapButton 才 SetBitmap((HBITMAP)A.GetSafeHandle()); 你 是 CButton ,直接 画吧
跪求具体实现方法

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧