如何让程序判断鼠标点击是否在指定区域内
最简朴的判断,假设在(200,200)到(300,300)范围内
bool isClick(ExMessage msg) {
if (msg.x > 200 && msg.x < 300 && msg.y > 200 && msg.y < 300)
return 1;
}
利用参数简化
bool isClick2(ExMessage msg,int x1,int y1,int x2,int y2) {
if (x1 > 200 && x1< 300 && y1 > 200 && y2 < 300)
return 1;
}
这样函数就能判断某一个区域是否被点击。
void start() {
ExMessage msg;
while (1) {
msg = getmessage();
if (msg.message == WM_LBUTTONDOWN)
if (isClick2(msg, 200, 200, 300, 300))
circle(msg.x, msg.y, 30);
}
}
只能在某个区域画圆
如果有多个区域需要判断,就在后面添加代码,改变区域范围。
void start() {
ExMessage msg;
while (1) {
msg = getmessage();
if (msg.message == WM_LBUTTONDOWN) {
if (isClick2(msg, 200, 200, 300, 300))
circle(msg.x, msg.y, 30);
if (isClick2(msg, 300, 300, 500, 500))
fillcircle(msg.x, msg.y, 30);
}
}
}
每次传一个按钮(区域)进去,都得传左上角右下角xy,太麻烦了,因此可以用结构体存这些变量,之后每次传参只传结构体。
typedef struct Button {
int x1;
int y1;
int x2;
int y2;
}Button;
bool isClick3(ExMessage msg,Button button) {
if (msg.x > button.x1 && msg.x < button.x2 && msg.y > button.y1 && msg.y < button.y2)
return 1;
return 0;
}
//使用结构体
void start2() {
//声明两个按钮并初始化
Button A = { 200,200,300,300 };
Button B = { 400,400,500,500 };
ExMessage msg;
while (1) {
msg = getmessage();
if (msg.message == WM_LBUTTONDOWN) {
if (isClick2(msg, 200, 200, 300, 300))
circle(msg.x, msg.y, 30);
if (isClick2(msg, 300, 300, 500, 500))
fillcircle(msg.x, msg.y, 30);
}
}
}
同样可以
呃呃把显示再改好点,利用矩形填充表示按钮
//使用结构体
void start2() {
//声明两个按钮并初始化
Button A = { 200,200,300,300 };
Button B = { 400,400,500,500 };
//画矩形
rectangle(A.x1, A.y1, A.x2, A.y2);
rectangle(B.x1, B.y1, B.x2, B.y2);
ExMessage msg;
while (1) {
msg = getmessage();
if (msg.message == WM_LBUTTONDOWN) {
if (isClick3(msg, A))
floodfill(msg.x, msg.y, WHITE, FLOODFILLBORDER);
if (isClick3(msg, B))
floodfill(msg.x, msg.y, WHITE, FLOODFILLBORDER);
}
}
}
因为按钮可能很多,我希望传入按钮结构体数组作为参数,遍历这个数组判断所有的按钮。
//使用结构体
void start3(vector<Button> buttons) {
ExMessage msg;
while (1) {
msg = getmessage();
if (msg.message == WM_LBUTTONDOWN) {
for (Button button:buttons) {
floodfill(msg.x, msg.y, WHITE, FLOODFILLBORDER);
}
}
}
}
void test() {
initgraph(1200, 600);
//声明两个按钮并初始化,丢到vector数组里(vector好用...)
Button A = { 200,200,300,300 };
Button B = { 400,400,500,500 };
vector<Button> buttons= { A,B };
//画所有按钮
for (Button button : buttons) {
rectangle(button.x1, button.y1, button.x2, button.y2);
}
start3(buttons);
}
继续改,现在我希望拿某张图片作为我的按钮,那我按钮范围自然是这张图片的大小。
不过在那之前,把代码逻辑整理一下...
void mouseEvent(vector<Button> buttons,ExMessage msg) {
msg = getmessage();
if (msg.message == WM_LBUTTONDOWN) {
for (Button button : buttons) {
floodfill(msg.x, msg.y, WHITE, FLOODFILLBORDER);
}
}
}
vector<Button> createButtons() {
//声明两个按钮并初始化,丢到vector数组里(vector好用...)
vector<Button> buttons;
Button A = { 200,200,300,300 };
Button B = { 400,400,500,500 };
buttons = { A,B };
}
void drawButtons(vector<Button> buttons) {
for (Button button : buttons) {
rectangle(button.x1, button.y1, button.x2, button.y2);
}
}
void Run() {
initgraph(1200, 600);
//创建按钮,画按钮
vector<Button> buttons = createButtons();
drawButtons(buttons);
//鼠标消息监听
ExMessage msg;
while (1) {
mouseEvent(buttons, msg);
}
}
不断地修改....我脑子已经烧掉了,程序能跑,但我快整理不出来逻辑了
struct Button {
int x1;
int y1;
int x2;
int y2;
Button(int x1, int y1, int x2, int y2) :x1(x1), y1(y1), x2(x2), y2(y2){}
//传图片定义按钮,img为IMAGE对象,x,y为图片左上角坐标
Button(IMAGE img,int x,int y) :x1(x),y1(y),x2(x+img.getwidth()),y2(y+img.getheight()){}
};
bool isInArea(ExMessage msg, Button button) {
if (msg.x > button.x1 && msg.x < button.x2 && msg.y > button.y1 && msg.y < button.y2)
return 1;
return 0;
}
void mouseEvent(vector<Button> buttons,ExMessage msg) {
if (msg.message == WM_LBUTTONDOWN) {
for (Button button : buttons) {
if(isInArea( msg,button))
floodfill(msg.x, msg.y, WHITE, FLOODFILLBORDER);
}
}
}
vector<Button> createButtons() {
//声明两个按钮并初始化,丢到vector数组里(vector好用...)
vector<Button> buttons;
Button A = { 200,200,300,300 };
Button B = { 400,400,500,500 };
IMAGE img;
loadimage(&img, L"image/Tenko (1).png");
putimage(500, 100, &img);
Button pic(img, 500, 100);
buttons = { A,B,pic };
return buttons;
}
void drawButtons(vector<Button> buttons) {
for (Button button : buttons) {
rectangle(button.x1, button.y1, button.x2, button.y2);
}
}
void Run() {
initgraph(1200, 600);
//创建按钮,画按钮
vector<Button> buttons = createButtons();
drawButtons(buttons);
//鼠标消息监听
ExMessage msg;
while (1) {
msg = getmessage();
mouseEvent(buttons, msg);
}
}
woc,几个种脑子快烧掉才想出来的,可以更方便添加按钮的函数
//判断是否在按钮区
bool isInArea(ExMessage msg, Button button) {
if (msg.x > button.x1 && msg.x < button.x2 && msg.y > button.y1 && msg.y < button.y2)
return 1;
return 0;
}
//鼠标点击判断区域
void mouseEvent(vector<Button> buttons,ExMessage msg) {
if (msg.message == WM_LBUTTONDOWN) {
for (Button button : buttons) {
if(isInArea( msg,button))
floodfill(msg.x, msg.y, RED, FLOODFILLBORDER);//填充
}
}
}
//创建一个按钮,同时记录到 IMAGE Button数组
void createButton(int x,int y,const wchar_t picName[],vector<Button>& buttons, vector<IMAGE>& images) {
IMAGE img;
loadimage(&img, picName);
Button picButton(img, x, y);
buttons.push_back(picButton);
images.push_back(img);
}
//完成所有按钮的初始化,在此处添加按钮
//注意!找图片名字没找到,会导致获取宽高为随机数,最后异常
void createButtons(vector<IMAGE>& images, vector<Button>& buttons) {
//声明N个按钮并初始化,丢到vector数组里(vector好用...)
createButton(100, 100, L"image/呵呵.png", buttons,images);
createButton(400, 100, L"image/呵呵.png", buttons,images);
createButton(700, 300, L"image/哈哈.png", buttons, images);
}
//画区域,为了填充演示效果用
void drawButtons(vector<Button> buttons) {
for (Button button : buttons) {
setlinecolor(RED);
rectangle(button.x1, button.y1, button.x2, button.y2);
}
}
//重载画图片
void drawButtons(vector<IMAGE>& images,vector<Button>& buttons) {
for (int i = 0; i < images.size();i++) {
putimage(buttons[i].x1, buttons[i].y1, &images[i]);
}
}
void Run() {
initgraph(1200, 600);
//创建按钮,画按钮
vector<Button>buttons;
vector<IMAGE>images;
createButtons(images, buttons);
drawButtons(images,buttons);
drawButtons(buttons);
//鼠标消息监听
ExMessage msg;
while (1) {
msg = getmessage();
mouseEvent(buttons, msg);
}
}
int main() {
Run();
}
最终效果就像这样。。。。点某块按钮就会填白。。不是很难,但是卡了很久
到这里就可以在createButtons里创建自己要的Button。