要求是在另一个应用程序窗口的一侧绘制我的信息。照顾 z 顺序等等挂钩 WH_GETMESSAGE 和绘制 WM_PAINT 似乎不错。
然而,一些 WM_PAINT 用于我关注的窗口区域,但其他 WM_PAINT 用于完全不同的东西,如上下文菜单或按钮。
示例记事本与在记事本屏幕中写入“Hello”的覆盖物挂钩。这工作正常。但是,当右键单击记事本时,上下文菜单会被 Hello 覆盖。基本上上下文菜单被破坏了。
有没有一种优雅的方法来确定 WM_PAINT 是上下文菜单?
LRESULT CALLBACK overlayHook(int code, WPARAM wParam, LPARAM lParam) {
//Try and be the LAST responder to WM_PAINT messages;
LRESULT retCode = CallNextHookEx(hhk, code, wParam, lParam);
//Per GetMsgProc documentation, don't do anything fancy
if(code < 0) {
return retCode;
}
//Assumes that target application only draws when WM_PAINT message is
//removed from input queue.
if(wParam == PM_NOREMOVE) {
return retCode;
}
MSG* message = (MSG*)lParam;
if(message->message != WM_PAINT) {
//Ignore everything that isn't a paint request
return retCode;
}
PAINTSTRUCT psPaint;
BeginPaint(message->hwnd, &psPaint);
HDC hdc = psPaint.hdc;
RECT r = psPaint.rcPaint;
TextOut(hdc, 10, 10, "Hello", 4);
EndPaint(message->hwnd, &psPaint);
return retCode;
}
仅测试绘图更新区域是不够的,因为上下文菜单可以在任何地方并包含我关注的区域。