如何在另一个应用程序之上绘制图形/文本 [英] How to draw graphics/text on top of another application
问题描述
我想增强应用程序,但没有可用的 3:e 方 API.所以基本上这个想法是在应用程序窗口的顶部绘制图形/文本.
I want to enhance an application, but there is no 3:e party API available. So basically the idea is to draw graphics/text on top of the applications windows.
z 顺序、剪切和将鼠标单击定向到我的应用程序或其他应用程序时存在问题.
There are problems with z order, clipping, and directing mouse clicks either to my application or the other application.
这样做的优雅方式是什么?
What is an elegant way of doing this?
示例图片在这里.这是一个交易应用程序,我的应用程序希望在其中向交易应用程序的窗口添加额外信息.[URL=http://img104.imageshack.us/my.php?image=windowontop.png][/URL]
Example image here. It is a trading application where my application wants to add extra information into the trading application's windows. [URL=http://img104.imageshack.us/my.php?image=windowontop.png][/URL]
推荐答案
没有好的方法可以做到这一点,但一种可能对您有用的方法是使用 SetWindowsHookEx(...) 添加有问题的应用程序一个 GetMsgProc,它根据 WM_PAINT 消息绘制您的叠加层.基本思想是在应用程序完成自己的绘制后立即绘制图形.
There are no nice ways to do this, but one approach that may work for you is to hook the application in question using SetWindowsHookEx(...) to add a GetMsgProc, which draws your overlay in response to WM_PAINT messages. The basic idea is that you're drawing YOUR graphics right after the application finishes its own drawing.
在您的主应用中:
....
HMODULE hDllInstance = LoadLibrary("myFavoriteDll");
HOOKPROC pOverlayHook = (HOOKPROC)GetProcAddress(hDllInstance, "OverlayHook");
SetWindowsHookEx(WH_GETMESSAGE, pOverlayHook, hDllInstance, threadId);
在某个 DLL 中关闭:
Off in a DLL somewhere:
LRESULT CALLBACK OverlayHook(int code, WPARAM wParam, LPARAM lParam)
{
//Try and be the LAST responder to WM_PAINT messages;
//Of course, if some other application tries this all bets are off
LRESULT retCode = CallNextHookEx(NULL, 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;
//Ignore everything that isn't a paint request
if(message->message != WM_PAINT) return retCode;
PAINTSTRUCT psPaint;
BeginPaint(message->hwnd, &psPaint);
//Draw your overlay here
...
EndPaint(message->hwnd, &psPaint);
return retCode;
}
这都是 win32,所以你的 C# 代码将是 p/invoke 繁重,相应地相当丑陋.您的 DLL 也必须是非托管的(如果您打算注入到您自己以外的进程中),这将是一个更糟糕的解决方案.
This is all win32 so your C# code will be p/invoke heavy and correspondingly quite ugly. Your DLL must be unmanaged as well (if you intend to inject into a process other than your own), making this an even nastier solution.
当您渲染到窗口本身时,这将解决您的 z 顺序和裁剪问题.但是,如果您的目标应用程序在响应 WM_PAINT 的 WinProc 之外进行任何绘图,事情就会分崩离析;这并非完全罕见.
This would solve your issue with z-order and clipping issues, as you're rendering into the window itself. However, if the application you're targeting does any drawing outside of the WinProc responding to WM_PAINT things fall apart; this is not an entirely uncommon occurence.
这篇关于如何在另一个应用程序之上绘制图形/文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!