以下是Dll代码,想用来获取正在移动的窗口,无论是 SetWindowsHookExW 还是 SetWinEventHook ,最后获取到的窗口总是调用其中两个 Start() 方法的窗口,获取不到其他正在移动的窗口。
恳求各位大佬指点一二
[C++] 纯文本查看 复制代码 // dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"
static HINSTANCE DllHandle = nullptr;
static HHOOK HookHandle = nullptr;
static HWND HostHandle = nullptr;
static HWINEVENTHOOK EventHookHandle = nullptr;
LPCWPSTRUCT CWPStruct = nullptr;
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
DllHandle = hModule;
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
void DoEvent(HWND hWnd)
{
if (IsWindow(hWnd))
{
if ((GetWindowLongPtrW(hWnd, GWL_STYLE) & WS_CHILD) != WS_CHILD)
{
int length = GetWindowTextLengthW(hWnd) + 1;
WCHAR title[MAX_PATH];
GetWindowTextW(hWnd, title, length);
SendMessageW(HostHandle, WM_USER + 9000, (WPARAM)length, (LPARAM)title);
}
}
}
void Wineventproc(HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD idEventThread, DWORD dwmsEventTime)
{
if (hWinEventHook == EventHookHandle)
{
if (event == EVENT_OBJECT_LOCATIONCHANGE)
{
DoEvent(hwnd);
}
}
}
extern "C"
{
__declspec(dllexport) LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode >= HC_ACTION)
{
if (wParam != 0)
{
CWPStruct = (LPCWPSTRUCT)lParam;
if (CWPStruct->message == WM_MOVING)
{
DoEvent(CWPStruct->hwnd);
}
}
}
return CallNextHookEx(HookHandle, nCode, wParam, lParam);
}
__declspec(dllexport) void Start(HWND hHost)
{
HostHandle = hHost;
HookHandle = SetWindowsHookExW(WH_CALLWNDPROC, CallWndProc, DllHandle, 0);
}
__declspec(dllexport) void Stop()
{
UnhookWindowsHookEx(HookHandle);
}
__declspec(dllexport) void Start2(HWND hHost)
{
HostHandle = hHost;
EventHookHandle = SetWinEventHook(EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_LOCATIONCHANGE, DllHandle, Wineventproc, 0, 0, WINEVENT_INCONTEXT | WINEVENT_SKIPOWNPROCESS);
}
__declspec(dllexport) void Stop2()
{
UnhookWinEvent(EventHookHandle);
}
} |