#include <graphics.h>
class ThisButton {
protected:
int _x;
int _y;
int _w;
int _h;
int _bg_color;
int _font_color;
const char *_text;
void (*_func)();
bool _mouse_hover=false;
bool _mouse_clicked=false;
public:
ThisButton(const char *t="NEW",void(* func)()=nullptr,int x=0,int y=0,int w=100,int h=30,int bg_color=BLUE,int font_color=WHITE) {
_x=x;
_y=y;
_w=w;
_h=h;
_bg_color=bg_color;
_font_color=font_color;
_text=t;
_func=func;
On_Draw();
}
~ThisButton() {
}
void On_Input(const ExMessage *msg){
Mouse_Hover(msg);
Mouse_Clicked(msg);
On_Draw();
}
void On_Draw(){
setfillcolor(_bg_color);
bar(_x,_y,_x+_w,_y+_h);
int text_h=textheight(_text);
int text_w=textwidth(_text);
setbkmode(TRANSPARENT);
settextcolor(_font_color);
if(_mouse_hover) settextcolor(RED);
outtextxy((_w-text_w)/2+_x,(_h-text_h)/2+_y,_text);
}
void Mouse_Hover(const ExMessage *msg) {
if(msg->x>_x && msg->y>_y && msg->x<_x+_w && msg->y<_y+_h){
_mouse_hover=true;
}else{
_mouse_hover=false;
}
}
void Mouse_Clicked(const ExMessage *msg){
if(_mouse_hover && msg->message==WM_LBUTTONUP){
_func();
}
}
};
class ThisEdit {
protected:
int _x;
int _y;
int _w;
int _h;
int _bg_color;
int _font_color;
bool _mouse_hover=false;
bool _mouse_clicked=false;
int EnterX;
int EnterY;
public:
ThisEdit(int x=0,int y=0,int w=100,int h=30,int bg_color=BLUE,int font_color=WHITE) {
_x=x;
_y=y;
_w=w;
_h=h;
EnterX=_x+5;
EnterY=_y+5;
_bg_color=bg_color;
_font_color=font_color;
On_Draw();
}
~ThisEdit() {
}
void Mouse_Hover(const ExMessage *msg) {
if(msg->x>_x && msg->y>_y && msg->x<_x+_w && msg->y<_y+_h){
_mouse_hover=true;
}else{
_mouse_hover=false;
}
}
void On_Input(const ExMessage *msg){
Mouse_Hover(msg);
Mouse_Clicked(msg);
On_Draw();
}
void On_Draw(){
setcolor(BLUE);
rectangle(_x,_y,_x+_w,_y+_h);
setbkmode(TRANSPARENT);
settextcolor(_font_color);
}
void Mouse_Clicked(const ExMessage *msg) {
if(_mouse_hover && msg->message==WM_LBUTTONUP){
setcolor(YELLOW);
rectangle(_x,_y,_x+_w,_y+_h);
outtextxy(EnterX,EnterY,"|");
}
}
};
void Button1_Clicked(){
outtextxy(0,0,"Button1 clicked.");
}
int main(){
initgraph(400,300);
HWND Me=GetForegroundWindow();
SetWindowPos(Me,HWND_TOPMOST,300,300,300,300, SWP_NOSIZE | SWP_SHOWWINDOW);
ThisButton Button1("Button",Button1_Clicked,20,20);
ThisEdit e1(130,20);
ExMessage msg;
BeginBatchDraw();
while(1){
while(peekmessage(&msg)){
Button1.On_Input(&msg);
e1.On_Input(&msg);
}
FlushBatchDraw();
}
EndBatchDraw();
closegraph();
return 0;
}
EASYX 一个测试用的BUTTON 类
最新推荐文章于 2024-10-06 17:00:10 发布