枚举窗口、鼠标、键盘

一、窗口

一个应用程序是有一堆窗口组成的

二、修改程序标题

1、FindWindow

参数:

  • 窗口类名
  • 窗口标题

2、SetWindowText

参数:

  • 句柄
  • 要修改标题的字符串

3、修改标题

  • 正常的程序

枚举窗口、鼠标、键盘

  • 修改后的程序(前提是程序不能关闭,可以最小化,不然找不到窗口)
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <TlHelp32.h>

void test()
{
									
	TCHAR szTitle[MAX_PATH] = {0};								
	HWND hwnd = FindWindow(TEXT("#32770"),TEXT("飞鸽传书  IP Messenger"));								
	if(hwnd != NULL)								
	{								
		//修改窗口标题							
		SetWindowText(hwnd,"新的窗口标题");							
	}								
	else								
	{								
		MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK);							
	}								
									


};

int main(int argc, char* argv[])
{
	test();
	return 0;
}

枚举窗口、鼠标、键盘

三、窗口控制

1、LoadLibrary

参数:

  • 加载的dll

2、GetProcAddress

参数:

  • 句柄
  • 需要查找的函数

3、窗口控制

让最小化的程序切出来,然后三秒钟关闭(关闭是右上角的x,不是退出程序)

#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <TlHelp32.h>

void test()
{
									
	TCHAR szTitle[MAX_PATH] = {0};								
	HWND hwnd = FindWindow(TEXT("#32770"),TEXT("飞鸽传书  IP Messenger"));								
	if(hwnd != NULL)								
	{								
		typedef void (WINAPI *PSWITCHTOTHISWINDOW) (HWND,BOOL);							
		PSWITCHTOTHISWINDOW SwitchToThisWindow;							
		HMODULE hUser32=LoadLibrary("user32.dll");							
		SwitchToThisWindow=(PSWITCHTOTHISWINDOW)GetProcAddress(hUser32,"SwitchToThisWindow");							
									
		//切换窗口							
		SwitchToThisWindow(hwnd,false);							
									
		Sleep(3000);							
		//关闭窗口 							
		SendMessage(hwnd,WM_CLOSE,0,0);							
	}								
	else								
	{								
		::MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK);							
	}																																				
};

int main(int argc, char* argv[])
{
	test();
	return 0;
}

四、修改子窗口的内容

1、SendMessage

参数:

  • 句柄
  • 消息类型
  • 为空就行
  • 发送的内容

2、修改子窗口的内容

#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <TlHelp32.h>

void test()
{								
									
	TCHAR szTitle[MAX_PATH] = {0};								
	HWND hwnd = FindWindow(TEXT("#32770"),TEXT("飞鸽传书  IP Messenger"));								
	if(hwnd != NULL)								
	{								
		//查找子窗口							
		HWND hEdit = FindWindowEx(hwnd,NULL,"Edit","");							
		//设置标题							
		SetWindowText(hEdit,"文本框新的标题");							
		//修改内容							
		SendMessage(hEdit,WM_SETTEXT,0,(LPARAM)"新的内容");							
	}								
	else								
	{								
		MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK);							
	}								
																																
};

int main(int argc, char* argv[])
{
	test();
	return 0;
}

枚举窗口、鼠标、键盘

3、修改子窗口的内容

每个窗口被创建时,都会有个id,用来识别身份的

枚举窗口、鼠标、键盘

#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <TlHelp32.h>

void test()
{								
									
	HWND hwnd = FindWindow(TEXT("#32770"),TEXT("飞鸽传书  IP Messenger"));								
	if(hwnd != NULL)								
	{								
		//查找子窗口							
		HWND hEdit =::GetDlgItem(hwnd,0x3E9);							
		//获取内容							
		//SendMessage(hEdit,WM_GETTEXT,MAX_PATH,(LPARAM)szTitle);							
		//修改内容							
		SendMessage(hEdit,WM_SETTEXT,0,(LPARAM)"*******");							
	}								
	else								
	{								
		MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK);							
	}																																																														
};

int main(int argc, char* argv[])
{
	test();
	return 0;
}

枚举窗口、鼠标、键盘

四、枚举子窗口

1、EnumChildWindows

参数:

  • 父窗口的句柄(其子窗口将被枚举)
  • 回调函数的指针
  • 为空即可
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <TlHelp32.h>

											
BOOL CALLBACK EnumChildProc(HWND hWnd,LPARAM lParam)  										
{  										
	TCHAR szTitle[MAX_PATH] = {0};										
	GetWindowText(hWnd,szTitle,MAX_PATH); 										
	MessageBox(NULL,szTitle,"[子窗口]",MB_OK);
        //返回值是true一直枚举,直到返回值是false,将会中止枚举 										
	return true;  										
}  										
										
VOID EnumChildWindow()										
{										
	TCHAR szTitle[MAX_PATH] = {0};									
	HWND hWnd = FindWindow(TEXT("#32770"),TEXT("飞鸽传书  IP Messenger"));									
	if(hWnd != NULL)									
	{									
		EnumChildWindows(hWnd,EnumChildProc,0);  								
	}									
	else									
	{									
		MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK);								
	}									
}										

int main(int argc, char* argv[])
{
	EnumChildWindow();
	return 0;
}

五、枚举当前操作系统打开的窗口

#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <TlHelp32.h>
																		
BOOL CALLBACK EnumOpenWindowProc(HWND hWnd,LPARAM lParam)  								
{  								
	TCHAR szTitle[MAX_PATH] = {0};								
	GetWindowText(hWnd,szTitle,MAX_PATH); 								
	MessageBox(NULL,szTitle,"[窗口]",MB_OK);  								
	if(strcmp(szTitle,"飞鸽传书  IP Messenger") == 0)							
	{							
		MessageBox(NULL,szTitle,"[窗口]",MB_OK);  						
		return FALSE;						
	}							
	return TRUE;  								
}  		
						
VOID EnumOpenWindows()								
{								
	EnumWindows(EnumOpenWindowProc,NULL);							
}								
							
int main(int argc, char* argv[])
{
	EnumOpenWindows();
	return 0;
}

六、模拟鼠标点击

#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <TlHelp32.h>
//隐藏控制台
#pragma   comment(linker,"/subsystem:\"windows\"  /entry:\"mainCRTStartup\""   ) 	
																				
void test()
{				
	TCHAR szTitle[MAX_PATH] = {0};	
	RECT r;	
	HWND hwnd = FindWindow(TEXT("#32770"),TEXT("飞鸽传书  IP Messenger"));	
	if(hwnd != NULL)	
	{	
		HWND hButton = FindWindowEx(hwnd,NULL,"Button","刷新(&R)");
		
		//获取窗口坐标
		GetWindowRect(hButton,&r);
		
		printf("%d %d",r.left,r.top);
		
		//设置鼠标的位置(窗口左上角点不了,所以加点偏移)
		SetCursorPos(r.left+10,r.top+10);
		Sleep(2000);
		//鼠标左键单击
		mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);//点下左键 
		mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);//松开左键
	}	
	else	
	{	
		MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK);
	}	
};						

int main(int argc, char* argv[])
{
	test();
	return 0;
}

七、模拟键盘

#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <TlHelp32.h>
//隐藏控制台
#pragma   comment(linker,"/subsystem:\"windows\"  /entry:\"mainCRTStartup\""   ) 	
																				
void test()
{				
	TCHAR szTitle[MAX_PATH] = {0};	
	RECT r;	
	HWND hwnd = FindWindow(TEXT("#32770"),TEXT("飞鸽传书  IP Messenger"));	
	if(hwnd != NULL)	
	{	
		//HWND hButton = FindWindowEx(hwnd,NULL,"Button","刷新(&R)");
		HWND hEdit = GetDlgItem(hwnd,0x3E9);
		
		//获取窗口坐标1bC
		GetWindowRect(hEdit,&r);
		
			
		printf("%d %d\n",r.left,r.top);

		//设置鼠标的位置
		SetCursorPos(r.left+10,r.top+10);

		Sleep(1000);
		
		//鼠标左键单击
		mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);//点下左键 
		mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);//松开左键
		

		//模拟键盘
		keybd_event(97,0,0,0);
		keybd_event(97,0,KEYEVENTF_KEYUP,0);
		Sleep(1000);
		keybd_event(66,0,0,0);
		keybd_event(66,0,KEYEVENTF_KEYUP,0);
		Sleep(1000);
		keybd_event(16,0,0,0);
		keybd_event(67,0,0,0);
		keybd_event(67,0,KEYEVENTF_KEYUP,0);
		keybd_event(16,0,KEYEVENTF_KEYUP,0);

	}	
	else	
	{	
		::MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK);
	}	

};						

int main(int argc, char* argv[])
{
	test();
	return 0;
}
上一篇:Windows -- Qt 利用RAWINPUT来获取外设输入


下一篇:2021-11-06