自己画了一个小工具栏,增加了鼠标单击/释放的处理程序。 附: mytool.cpp mytool.h
mytool.h
#ifndef __MYTOOL_H
#define __MYTOOL_H

enum e_button...{
E_LEVEL,
E_HELP,
E_SCORE
};
class MYTOOL
...{
public:
MYTOOL();
~MYTOOL();
void SetPressDown(int x,int y);
int GetButtonPress(int x,int y);
void Draw();
void LoadPic();
void SetHINSTANCE(HINSTANCE h);
void SetHWND(HWND h);
void SetHDC(HDC h);
void SetPos(int x,int y);
private:
int iPressDown;
int button_num;
POINT pos;
HBITMAP hbm_tool;
HWND hwnd;
HDC hdc;
HDC hdcmem;
HINSTANCE hinstance;
};
#endif
mytool.cpp
#include "stdafx.h"
#include "resource.h"
#include "mytool.h"

MYTOOL::MYTOOL()
...{
pos.x=0;
pos.y=0;
iPressDown=-1;
button_num=3;
}
MYTOOL::~MYTOOL()
...{
DeleteObject(hbm_tool);
}
void MYTOOL::Draw()
...{
//hdc=GetDC(hwnd);
SelectObject(hdcmem,hbm_tool);
BitBlt(hdc,pos.x,pos.y,button_num*32,32,hdcmem,0,0,SRCCOPY);
if(iPressDown!=-1)
...{
BitBlt(hdc,pos.x+iPressDown*32,pos.y,32,32,hdcmem,iPressDown*32,32,SRCCOPY);
}
}
void MYTOOL::LoadPic()
...{
hbm_tool=LoadBitmap(hinstance,MAKEINTRESOURCE(IDB_TOOL_NUMBER)); 
hdc=GetDC(hwnd);
hdcmem=CreateCompatibleDC(hdc);
ReleaseDC(hwnd,hdc);
}
void MYTOOL::SetHINSTANCE(HINSTANCE h)
...{
hinstance=h;
}
void MYTOOL::SetHWND(HWND h)
...{
hwnd=h;
}
void MYTOOL::SetHDC(HDC h)
...{
hdc=h;
}
void MYTOOL::SetPressDown(int x,int y)
...{
int i=0;
//按钮个数
for(i=0;i<button_num;i++)
...{
if(x>=pos.x+i*32 && x<=pos.x+i*32+32
&& y>=pos.y && y<=pos.y+32)
...{
iPressDown=i;
break;
}
}
}
int MYTOOL::GetButtonPress(int x,int y)
...{
int i=0;
//按钮个数
for(i=0;i<button_num ;i++)
...{
if(x>=pos.x+i*32 && x<=pos.x+i*32+32
&& y>=pos.y && y<=pos.y+32)
...{
iPressDown=-1;
return i;
}
}
return -1;
}
void MYTOOL::SetPos(int x,int y)
...{
pos.x=x;
pos.y=y;
}
本文介绍了一种自定义工具栏的设计与实现方法,包括鼠标点击事件处理、按钮状态更新及绘制等功能。通过C++代码实现了工具栏的交互与显示效果。
2756

被折叠的 条评论
为什么被折叠?



