今天我们学习如何获取到其他进程窗体信息如: 类名,窗体内容,大小以及进程和线程ID等; 但我们做的练习将会比我们所要学的更有意思; 哈哈… 我们通过仿VS著名工具SPY++中拖拉鼠标到指定窗体上来截获窗体句柄,根据窗体句柄后获取特点窗体的信息(例程中屏避了对本进程的窗体), 根据这个例子我们对其中技术一起学习, 先看一下SPY++效果:
一、 改变鼠标光标
1. LoadCursor 加载光标资源
hCursor = LoadCursor(pcs->hInstance, MAKEINTRESOURCE(IDC_CURSOR1));
2. SetCursor 设置光标样式
SetCursor(hCursor);
二、 获取鼠标在非窗体中的消息
1. SetCapture 设置截取鼠标事件的窗体(鼠标按下后)
SetCapture(hWnd);
2. ReleaseCapture 释放对鼠标事件截获(鼠标释放后)
ReleaseCapture();
三、 获取鼠标下的窗体
1. GetCurrentPos 获取鼠标位置
POINT ptMouse;
GetCursorPos(&ptMouse);
2. WindowFromPoint 根据位置获取
POINT ptMouse;
GetCursorPos(&ptMouse);
HWND hWndTmp = WindowFromPoint(ptMouse);
四、 获取窗体信息(窗体内容,窗体类名,窗体风格,大小以及进程和线程ID)
//==: Retrieve the Window information
TCHAR szWndInfo[512] = {0};
TCHAR szWndText[128] = {0};
TCHAR szClsName[64] = {0};
SendMessage(hWndTmp, WM_GETTEXT, (WPARAM)128, (LPARAM)szWndText);
GetClassName(hWndTmp, szClsName, 64);
DWORD dwStyle;
dwStyle = GetClassLongPtr(hWndTmp, GCL_STYLE);
RECT rtTmp;
GetWindowRect(hWndTmp, &rtTmp);
DWORD TID;
DWORD PID;
TID = GetWindowThreadProcessId(hWndTmp, &PID);
WINDOWINFO wndInfo;
wndInfo.cbSize = sizeof(wndInfo);
GetWindowInfo(hWndTmp, &wndInfo);
_stprintf(szWndInfo,
_T("\n´°Ìå:\t 0x%0X")
_T("\nÎı¾:\t %s")
_T("\nˈ̞:\t %s")
_T("\nÑùʽ:\t 0x%0X")
_T("\n¾ØÐÎ:\t (%d,%d)-(%d,%d) %dx%d")
_T("\nÏß³Ì:\t 0x%08X")
_T("\n½ø³Ì:\t 0x%08X"),
hWndTmp,
szWndText,
szClsName,
wndInfo.dwStyle,
rtTmp.left, rtTmp.top, rtTmp.right, rtTmp.bottom, (rtTmp.right - rtTmp.left), (rtTmp.bottom - rtTmp.top),
TID,
PID);
SetWindowText(GetDlgItem(hWnd, ID_LABINFO), szWndInfo);
TCHAR szWndInfo[512] = {0};
TCHAR szWndText[128] = {0};
TCHAR szClsName[64] = {0};
SendMessage(hWndTmp, WM_GETTEXT, (WPARAM)128, (LPARAM)szWndText);
GetClassName(hWndTmp, szClsName, 64);
DWORD dwStyle;
dwStyle = GetClassLongPtr(hWndTmp, GCL_STYLE);
RECT rtTmp;
GetWindowRect(hWndTmp, &rtTmp);
DWORD TID;
DWORD PID;
TID = GetWindowThreadProcessId(hWndTmp, &PID);
WINDOWINFO wndInfo;
wndInfo.cbSize = sizeof(wndInfo);
GetWindowInfo(hWndTmp, &wndInfo);
_stprintf(szWndInfo,
_T("\n´°Ìå:\t 0x%0X")
_T("\nÎı¾:\t %s")
_T("\nˈ̞:\t %s")
_T("\nÑùʽ:\t 0x%0X")
_T("\n¾ØÐÎ:\t (%d,%d)-(%d,%d) %dx%d")
_T("\nÏß³Ì:\t 0x%08X")
_T("\n½ø³Ì:\t 0x%08X"),
hWndTmp,
szWndText,
szClsName,
wndInfo.dwStyle,
rtTmp.left, rtTmp.top, rtTmp.right, rtTmp.bottom, (rtTmp.right - rtTmp.left), (rtTmp.bottom - rtTmp.top),
TID,
PID);
SetWindowText(GetDlgItem(hWnd, ID_LABINFO), szWndInfo);
以下用SPY++获取的窗体信息与例程读取的信息
图1 SPY++ 图2 例程