C++鼠标点击!不需头文件!只需要一个文件,轻轻松松搞定!

目录

先看看效果

        初始状态:

        当鼠标触碰到按钮时:

        点击鼠标时:

想法: 

        头文件:

        KEY_DOWN: 

        GetPos:

        gotoxy:

        color:

        Button:

        NewButton:

         Preserve:

        主函数:

完整代码:

今日小彩蛋:

ヾ( ̄▽ ̄)Bye~Bye~


先看看效果

        初始状态:

        当鼠标触碰到按钮时:

        点击鼠标时:

话说回来,这么复杂的程序该怎麽写呢?

想法: 

        头文件:

                必要的:

#include<bits/stdc++.h>
#include <windows.h>

        KEY_DOWN: 

                KEY_DOWN的意思是判断鼠标是否按下。其中使用了问号表达式做判断。

#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)

        GetPos:

                暂无内容。

void GetPos(POINT &pt)
{
	HWND hwnd = GetForegroundWindow();
	GetCursorPos(&pt);
	ScreenToClient(hwnd, &pt);
	pt.y = pt.y / 16, pt.x = pt.x / 8;
	swap(pt.x, pt.y);
}

        gotoxy:

                将光标移动到(x, y)的位置。

void gotoxy(int x, int y)
{
	COORD pos; pos.X = y; pos.Y = x;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

        color:

                设置a颜色。

void color(int a)
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
}

        Button:

                整个按钮结构体。

struct Button
{
	int x, y, color;
	const char *name;
	int len;
};

        NewButton:

                Button一个按钮创建函数。

Button NewButton(int x, int y, int color, const char *name)
{
	Button t;
	t.x = x, t.y = y, t.name = name;
	t.color = color;
	t.len = strlen(name);
	return t;
}

         Preserve:

                再来一串判断。

bool Preserve(Button A)
{
	gotoxy(A.x, A.y), color(A.color), printf("%s", A.name);
	POINT pt;
	GetPos(pt);
	if (pt.x == A.x && (pt.y >= A.y&&pt.y <= A.y + A.len))
	{
		color(112), gotoxy(A.x, A.y), printf("%s", A.name);
		if (KEY_DOWN(MOUSE_MOVED)) return 1;
	}
	return 0;
}

        主函数:

                之后就是主函数:

int main()
{
	system("mode con cols=15 lines=15");
	Button Exit = NewButton(6, 4, 0 | 19, "快乐星空@");
	while (1)
	{
		if (Preserve(Exit)) exit(0);
		Sleep(25);
	}
	return 0;
}

完整代码:

        穿起来就是啦!

#include<bits/stdc++.h>
#include <windows.h>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
using namespace std;
void GetPos(POINT &pt)
{
	HWND hwnd = GetForegroundWindow();
	GetCursorPos(&pt);
	ScreenToClient(hwnd, &pt);
	pt.y = pt.y / 16, pt.x = pt.x / 8;
	swap(pt.x, pt.y);
}
void gotoxy(int x, int y)
{
	COORD pos; pos.X = y; pos.Y = x;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void color(int a)
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
}
struct Button
{
	int x, y, color;
	const char *name;
	int len;
};
Button NewButton(int x, int y, int color, const char *name)
{
	Button t;
	t.x = x, t.y = y, t.name = name;
	t.color = color;
	t.len = strlen(name);
	return t;
}
bool Preserve(Button A)
{
	gotoxy(A.x, A.y), color(A.color), printf("%s", A.name);
	POINT pt;
	GetPos(pt);
	if (pt.x == A.x && (pt.y >= A.y&&pt.y <= A.y + A.len))
	{
		color(112), gotoxy(A.x, A.y), printf("%s", A.name);
		if (KEY_DOWN(MOUSE_MOVED)) return 1;
	}
	return 0;
}

int main()
{
	system("mode con cols=15 lines=15");
	Button Exit = NewButton(6, 4, 0 | 19, "快乐星空@");
	while (1)
	{
		if (Preserve(Exit)) exit(0);
		Sleep(25);
	}
	return 0;
}

今日小彩蛋:

试试这个!

#include<bits/stdc++.h>
#include <windows.h>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
using namespace std;
void GetPos(POINT &pt)
{
	HWND hwnd = GetForegroundWindow();
	GetCursorPos(&pt);
	ScreenToClient(hwnd, &pt);
	pt.y = pt.y / 16, pt.x = pt.x / 8;
	swap(pt.x, pt.y);
}
void gotoxy(int x, int y)
{
	COORD pos; pos.X = y; pos.Y = x;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void color(int a)
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
}
struct Button
{
	int x, y, color;
	const char *name;
	int len;
};
Button NewButton(int x, int y, int color, const char *name)
{
	Button t;
	t.x = x, t.y = y, t.name = name;
	t.color = color;
	t.len = strlen(name);
	return t;
}
bool Preserve(Button A)
{
	gotoxy(A.x, A.y), color(A.color), printf("%s", A.name);
	POINT pt;
	GetPos(pt);
	if (pt.x == A.x && (pt.y >= A.y&&pt.y <= A.y + A.len))
	{
		color(112), gotoxy(A.x, A.y), printf("%s", A.name);
		if (KEY_DOWN(MOUSE_MOVED)) return 1;
	}
	return 0;
}

int main()
{
	system("mode con cols=15 lines=15");
	Button Exit = NewButton(6, 4, 0 | 19, "接受祝福\n  (长按)");
	while (1)
	{
		if (Preserve(Exit)) 
		{
			system("cls");
			A:
			Button aaa = NewButton(6, 4, 0 | 19, "你是SB");
			if (Preserve(aaa))
				goto A;
		}
	}
	return 0;
}

()Bye~Bye~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐星空Maker

你的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值