零基础逆向工程40_Win32_14_枚举窗口_模拟鼠标键盘

Posted FLATCC

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了零基础逆向工程40_Win32_14_枚举窗口_模拟鼠标键盘相关的知识,希望对你有一定的参考价值。

1 查找窗口

1.1 代码案例

//查找指定窗口			
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);		
}
			
//窗口控制						
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);
}

2 查找子窗口

2.1 使用FindWindowEx()函数查找

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);	
}		

2.2 使用资源ID查找

TCHAR szTitle[MAX_PATH] = {0};
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);
}

3 枚举子窗口控件

BOOL CALLBACK EnumChildProc(HWND hWnd,LPARAM lParam)  			
{  			
    TCHAR szTitle[MAX_PATH] = {0};			
    ::GetWindowText(hWnd,szTitle,MAX_PATH); 			
    MessageBox(NULL,szTitle,"[子窗口]",MB_OK);  			
    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);	
	}		
}

4 枚举所有打开窗口

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);
}

5 模拟鼠标单击

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);
}	

6 模拟键盘点击

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);
	
	//获取窗口坐标
	::GetWindowRect(hEdit,&r);
	
	//设置鼠标的位置
	::SetCursorPos(r.left+1,r.top+1);
	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);
}	

键盘键与虚拟键码对照表
MSDN:https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
Blog:https://www.cnblogs.com/del/archive/2007/12/07/987364.html

以上是关于零基础逆向工程40_Win32_14_枚举窗口_模拟鼠标键盘的主要内容,如果未能解决你的问题,请参考以下文章

零基础逆向工程37_Win32_10_事件_线程同步

零基础逆向工程36_Win32_10_互斥体_ForSingleObject_WaitForMultipleObjects

零基础逆向工程39_Win32_13_进程创建_句柄表_挂起方式创建进程

14win32_14_button按钮

Java零基础_零基础学Java必备的学习笔记开发环境搭建-常见DOS命令

定期使用 python 使窗口聚焦