[C/C++/Easyx]07 消息處理—漂亮的按鈕

交作業(yè)doge, 自己加了點(diǎn)陰影效



#include<stdio.h>
#include<easyx.h>
#include <graphics.h>
ExMessage msg = { 0 };
bool button(int butx, int buty,int butw, int buth , const char* test) {//butx y為按鈕位置
??//繪制按鈕
??setfillcolor(BLACK);//陰影+
??solidroundrect(butx, buty, butx + butw +3, buty + buth+5, 20, 40);//+
??setlinecolor(BLACK);
??setlinestyle(PS_SOLID, 3);
??setfillcolor(RGB(200, 111, 103));
??fillroundrect(butx, buty, butx + butw, buty + buth, 20, 40);
??//繪制文本
??settextcolor(BLACK);
??int hspace = (butw - textwidth(test)) / 2;
??int wspace = (buth - textheight(test)) / 2;
??outtextxy(butx + hspace, buty + wspace, test);
??//鼠標(biāo)在這個(gè)區(qū)域?
??if (msg.x > butx && msg.x < butx + butw && msg.y > buty && msg.y < buty + buth) {
????setfillcolor(RED);
????fillroundrect(butx, buty, butx + butw, buty + buth, 20, 40);
??}
??//鼠標(biāo)在這個(gè)區(qū)域并且單機(jī)了左鍵?
??if (msg.message==WM_LBUTTONDOWN && msg.x > butx && msg.x < butx + butw && msg.y > buty && msg.y < buty + buth) {
????return true;
??}
??return false;???
}
int main() {
??//建立窗口
??initgraph(640, 480, EX_SHOWCONSOLE );
??setbkcolor(RGB(242, 222, 189));
??cleardevice();
??setbkmode(TRANSPARENT);//設(shè)置文字背景透明
??//
??while (true) {
????if (peekmessage(&msg, EX_MOUSE)) {//獲取鼠標(biāo)信息+
????}
????BeginBatchDraw();//
????if (button(180, 300, 100, 50, "Play")) {//x, y, wide, heigh, text
??????printf("Game Start\n");
????}
????if (button(380, 300, 100, 50, "Quit")) {//x, y, wide, heigh, text
??????printf("Closing...\n");
????}
????EndBatchDraw();
????msg.message = 0;
??}
??//保持顯示
??getchar();
??closegraph();
??return 0;