目录
先看看效果
初始状态:
当鼠标触碰到按钮时:
点击鼠标时:
话说回来,这么复杂的程序该怎麽写呢?
想法:
头文件:
必要的:
#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;
}