跳到正文

个人记录

关于鼠标

exdtables约 17 分钟
原文
文章信息与版本

文章信息

发布
2022年8月6日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 17 分钟

版本

  1. v1当前

这是一个鼠标的类

C++
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
#include<io.h>//此处原为dir.h,但dir.h只有#include<io.h>这一行代码。。。
using namespace std;
void GPT(POINT &pt) {//返回相对窗口的坐标,还不算框框 
	//pt.x是横,.y是竖 
	GetCursorPos(&pt);
	ScreenToClient(FindWindow("ConsoleWindowClass", NULL), &pt);
}
void GGT(int x,int y){//GT是 G o T oxy的缩写,写代码方便 
	COORD coord;
	coord.X=y;//这里调换了顺序,否则就是将输出指针移到y行,x列 
	coord.Y=x;
	HANDLE a=GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(a,coord);
}
void colour(int Fx,int Gx,bool If_FHighlight,bool If_GHighlight){
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),Fx|Gx|(If_FHighlight?8:0)|(If_GHighlight?128:0));
}

int ConPosx,ConPosy;//字体大小,分别是长,高 
RECT Rect;//存储窗口坐标
void GetCon(){
	HANDLE hOutput;
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_FONT_INFO consoleCurrentFont;
	GetCurrentConsoleFont(hOutput, FALSE, &consoleCurrentFont);
	//printf("通过GetCurrentConsoleFont()函数获取到的控制台当前字体长度是%d, 高度是%d.\n",consoleCurrentFont.dwFontSize.X,consoleCurrentFont.dwFontSize.Y);
	ConPosx=consoleCurrentFont.dwFontSize.X;
	ConPosy=consoleCurrentFont.dwFontSize.Y;
	/*GT(9,10);
	printf("%d %d",ConPosx,ConPosy);*/
}
class Message{//信息类 
public:
	string Thing;//储存信息
	int x,y;//储存第一的字符的控制台坐标 
	bool If_FHighlight,If_GHighlight,No_while_print;//(1是0否)前景、背景高亮;防止重复打印,设置成每改变一次打印一次 
	bool If_Use_Ground_Color;//是否使用背景颜色 
	bool Is_Touching;//(1是0否)被鼠标触碰 
	short Fcolor,Gcolor;//储存颜色:{前景,背景}->都不高亮 
	POINT pt;//存储鼠标 
	short Left,Right,Top,Bottom;//存储字符串的可点击范围 
	Message(short x1,short y1,string st="无内容  Nothing",short k1=7,short k2=0,bool IUGC=0){
		x=x1;
		y=y1;
		Thing=st;
		Fcolor=k1;
		Gcolor=k2;
		If_Use_Ground_Color=IUGC;
		GetCon();
		HWND hwnd=GetForegroundWindow();
	    GetWindowRect(hwnd,&Rect);
		Left=y*ConPosx;
		Right=Left+Thing.length()*ConPosx;
		Top=x*ConPosy;
		Bottom=Top+ConPosy;
	}
	void print(){
		GGT(x,y);
		colour(Fcolor,Gcolor&If_Use_Ground_Color,If_FHighlight,If_GHighlight&If_Use_Ground_Color);
		cout<<Thing;
	}
	void clean(){
		int len=Thing.length();
		GGT(x,y);
		for(int i=0;i<len;++i)
			putchar(' ');
	}
	/*
	此设计如下:
	①获取窗口位置 
	检索长度,再乘上Conx,最后加上窗口位置的坐标:长+Rect.left,高+Rect.top
	left和top都是RECT类的成员
	
	经过逻辑分析之后,x,y从0开始不影响 
	
	但是不知道为什么,好像不用加窗口坐标 
	2021.7.15知:
	GPT返回相对于窗口的坐标,所以不用加 
	*/
	bool If_Touching(){
		/*Left=y*ConPosx;
		Right=Left+Thing.length()*ConPosx;
		Top=x*ConPosy;
		Bottom=Top+ConPosy;*///高度就一格,可简化 
		GPT(pt);
		if(pt.x>=Left&&pt.x<=Right&&pt.y>=Top&&pt.y<=Bottom){
			If_FHighlight=1;
			If_GHighlight=1;
		}
		else{
			If_FHighlight=0;
			If_GHighlight=0;
		}
		if(If_FHighlight!=No_while_print){
			No_while_print=If_FHighlight;//随便一个 
			print();
		}
		return If_FHighlight;
	}
	short Be_Clicked(){
		if(If_Touching()){
			if(GetAsyncKeyState(1))//左键 
				return 1;
			else if(GetAsyncKeyState(2))//右键 
				return 2;
			else if(GetAsyncKeyState(4))//中键 
				return 3;	
			else//没按键 
				return 0;
		}
	}
};
Message m(5,10,"你好,鼠标!",7,112);
void movewindow(){
	HWND hwnd = GetForegroundWindow();
	GetWindowRect(hwnd, &Rect);
	MoveWindow(hwnd, 100, 100, 1000, 500, TRUE);
}
int main(){
/*	GT(0,0);
	printf("拖动鼠标去触碰并用 左/右/中 键点击“你好,鼠标!”,甚至可以随便改变字体大小以及拖动屏幕!");
	*/
//	movewindow();
	m.print();
	while(1){
		GetCon();
		HWND hwnd=GetForegroundWindow();
	    GetWindowRect(hwnd,&Rect);
	    if(m.Be_Clicked()==1){
	    	GT(10,10);
	    	printf("你用鼠标左键按下了“你好,鼠标!”");
		}
	    else if(m.Be_Clicked()==2){
	    	GT(10,10);
	    	printf("你用鼠标右键按下了“你好,鼠标!”");
		}
		else if(m.Be_Clicked()==3){
	    	GT(10,10);
	    	printf("你用鼠标中键按下了“你好,鼠标!”");
		}
	}
	return 0;
}

这是一些鼠标操作的实例(甚至连点器)

C++
/*//移动鼠标 
#include <iostream>
#include <windows.h>
#include <cstdlib>
using namespace std;
int main()
{
	char n;
	POINT p;
	//获取鼠标位置
	while((n=getchar())!='\n')
	{
		GetCursorPos(&p);
		system("cls");//包含在cstdlib中
		cout<<p.x<<" "<<p.y<<endl;
	}
	Sleep(5000);
	for(int i=1;i<=100;++i)
	{
		SetCursorPos(200,341);//移动到指定位置
		mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);//按下左键
		mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);//松开左键
		Sleep(1);
	}
	
	mouse_event(MOUSEEVENTF_RIGHTDOWN,0,0,0,0);//按下右键
	mouse_event(MOUSEEVENTF_RIGHTUP,0,0,0,0);//松开右键
	return 0; 
}*/
/*
//点击判断 
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <cstdio> 
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
using namespace std;
int main()
{
	char n;
	POINT p;
	//获取鼠标位置
	Sleep(1000);
	while(1)
	{
		if(KEY_DOWN(VK_LBUTTON))
		{
			GetCursorPos(&p);
			cout<<p.x<<" "<<p.y<<endl;
			break;
		}
	}
	system("pause");
	return 0;
}*/

//连点器 
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <cstdio>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
using namespace std;
HWND hwnd=GetForegroundWindow();
int main()
{
	system("mode con cols=20 lines=5");
	POINT p;
	int x,y;
	int n;
	while(1)
	{
		printf("次数:"); 
		cin>>n;
		printf("点击位置:\n"); 
		while(1)
		{
			if(KEY_DOWN(VK_LBUTTON))
			{
				GetCursorPos(&p);
				cout<<p.x<<" "<<p.y<<endl;
				break;
			}
		}
		printf("按两次任意键后开始\n");
		printf("按esc键终止\n");
		system("pause");
		for(int i=1;i<=n;++i)
		{
			SetCursorPos(p.x,p.y);//移动到指定位置
			mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);//按下左键
			Sleep(3);
			mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);//松开左键
			if(GetAsyncKeyState( 0x1B )& 0x8000)
				exit(0);
			Sleep(3);
		}
	}
	return 0; 
}
/*
//判断按下 
#include <cstdio> 
#include<windows.h> 
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
  
int main()
{
    while(1)
    {
        printf("鼠标左键是否按下:");
        if(KEY_DOWN(VK_LBUTTON))printf("是");
        else printf("否");
        printf("\n");
         
        printf("鼠标右键是否按下:");
        if(KEY_DOWN(VK_RBUTTON))printf("是");
        else printf("否");
        printf("\n");
         
        Sleep(1);
    }
    return 0;
}*/
/*
寻找位置 
#include <stdio.h>
#include <windows.h>

int main(void)
{
	POINT pt;
	
	GetCursorPos(&pt);
	printf("%ld %ld\n",pt.x,pt.y);
	
	return 0;
}*/

//4: 2与3结合,判断被点击的位置
/*#include <cstdio> 
#include<windows.h> 
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
  
int main()
{
	system("mode con cols=160 lines=42");
	system("color 70");
	POINT pt;
	while(1)
	{
		while(1)
	    {
			GetCursorPos(&pt);
	        if(KEY_DOWN(VK_LBUTTON))
	        {
	        	printf("左键被按下  :%ld %ld\n",pt.x,pt.y);
	        	break;
			}
	        if(KEY_DOWN(VK_RBUTTON))
	        {
	        	printf("右键被按下  :%ld %ld\n",pt.x,pt.y);
	        	break;
			}
		}
		printf("\n");
		Sleep(30);
	}
    return 0;
}*/

/*//5: 4的变形,判断是否按在某个点 
#include <cstdio> 
#include<windows.h> 
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
int main()
{
	system("cls");
	system("color 70");
	system("mode con cols=160 lines=42");
	POINT pt;
	SetCursorPos(10,30);
	while(1)
	{
		while(1)
	    {
			GetCursorPos(&pt);
	        if(KEY_DOWN(VK_LBUTTON))
	        {
		        if(pt.x==7&&pt.y==10) 
		        	printf("左键按到指定位置  :%ld %ld\n",pt.x,pt.y);
	        	break;
			} 
	        if(KEY_DOWN(VK_RBUTTON))
	        {
	        	if(pt.x==7&&pt.y==10) 
	        		printf("右键按到指定位置  :%ld %ld\n",pt.x,pt.y);
	        	break;
			}
		}
		printf("\n");
		Sleep(750);
	}
    return 0;
}*/
/*
//6: 5改,判断在某区域点击 
#include <cstdio> 
#include<windows.h> 
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
int main()
{
	system("mode con cols=160 lines=42");
	POINT pt;
	SetCursorPos(25,25);
	while(1)
	{
		while(1)
	    {
			GetCursorPos(&pt);
	        if(KEY_DOWN(VK_LBUTTON))
	        {
		        if(pt.x>20&&pt.x<40)
		        	if(pt.y>20&&pt.y<40)
			        	printf("左键按到指定区域  :%ld %ld\n",pt.x,pt.y);
		       	break; 
			} 
	        if(KEY_DOWN(VK_RBUTTON))
	        {
	        	if(pt.x>20&&pt.x<40)
		        	if(pt.y>20&&pt.y<40)
	        			printf("右键按到指定区域  :%ld %ld\n",pt.x,pt.y);
	        	break;
			}
		}
		printf("\n");
		Sleep(750);
	}
    return 0;
}*/

还有,使用鼠标要先关闭快捷操作等

C++
	HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);  
    DWORD mode;  
    GetConsoleMode(hStdin, &mode);  
    mode &= ~ENABLE_QUICK_EDIT_MODE;  //移除快速编辑模式
    mode &= ~ENABLE_INSERT_MODE;      //移除插入模式
    mode &= ~ENABLE_MOUSE_INPUT;
    SetConsoleMode(hStdin, mode);
	HideCursor();

但是这套鼠标系统在处理字号方面不太可行,有一篇博客貌似可以解决

原文