Drawing on another window (without flickering?)
I need to draw something on a window (that isn't mine - it's user-defined, if it matters).
I've already managed to draw on the window by getting the device context using GetDC and drawing normally, the problem is that I don't know when to draw to the window - the window is constantly redrawing itself (for example, a game). I've got a few ideas but so far they're no good or bad/no implementation.
- Using a timer. This was the worst idea, but I'll try anything right now. obviously, since the window is redrawn constantly it would flicker. I thought of forcing the window to redraw before my drawings, but still it's no good. can't really think of a good implementation with a timer...
- Hook WM_PAINT message. using SetWindowsHookEx WM_GETMESSAGE hook I can use an event that receive WM_PAINT message whenever the other window re开发者_JAVA技巧ceives it, but WM_PAINT is sent before drawing is done. I found on MSDN that instead of WH_GETMESSAGE Windows Hook you can use WndProcRetHook which is called after processing windows messages and recieves a structure containing the message information, but so far no luck - I can't get it to work...
- Hooking drawing functions. I've tried hooking the painting function (or functions) such as ReleaseDC and/or EndPaint which invalidate the window, so I can do my last-minute drawings. this time, I got it to work pretty good - but it still flickers a little and on some windows hooking ReleaseDC somehow prevents from the window to render - it doesn't even erase it's background. sometimes even the entire process crashes.
So I'm looking for a good method to be the last to draw on a window before it is displayed on the screen. I prefer avoiding hooks not being done with SetWindowsHookEx (such as: external DLL) but that would be fine too.
Thanks.
An alternative approach might be to avoid painting directly on the window, and instead create a transparent layered window that you keep positioned on top of the main one. That way, the drawing on your window doesn't interfere with the drawing on the main window, and vice-versa.
You may need to take certain steps to that mouse clicks will go through your window to the main window; eg. handle WM_NCHITTEST with HTTRANSPARENT and use the WS_EX_TRANSPARENT style.
Since, under Windows, windows are required to draw on demand, the only way to do this is to use SetWindowsHookEx()
to hook the WM_PAINT window for that window.
This is a non-trivial process that requires that your paint code reside in a DLL (so the DLL can be loaded into the target app's address space).
In general, this is not a very good idea unless you have a very good reason to do this.
精彩评论