I am trying to create a program that gets a window handle under your cursor, shows some data about it and draws a filled rectangle (with very low alpha) on top of the whole window. I use C # and winforms.
I succeeded, but the problem is that my draw method is in the BackgroundWorker loop, and it continues to create more and more rectangles (-> a rectangle with higher alpha) in the window or when moving the mouse to another window, the old one still exists .
I could not find a way to clear the drawn rectangle, because it is just โlocatedโ on the screen and is not attached to a graphic object or anything else.
I tried using some proprietary methods like
[DllImport("User32.dll")] public static extern Int64 SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll")] public static extern bool InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase); [DllImport("user32.dll")] public static extern bool UpdateWindow(IntPtr hWnd); [DllImport("user32.dll")] public static extern bool RedrawWindow(IntPtr hWnd, IntPtr lprcUpdate, IntPtr hrgnUpdate, RedrawWindowFlags flags);
but none of the above actions work correctly. Some of them work, but as messages get in the queue, the redrawing does not take place immediately or very slowly and smoothes out (flickering, etc.).
So the question is, how would I โdeleteโ the rectangle that I drew using Graphics.FromHwnd (handleOfWindowUnderCursor)? I actually think that it doesnโt matter that it is painted in another window, because I had the same problem earlier when we tried to get rid of the drawings on my own form too (it never worked, it fixed!).
Alternatively, any suggestions on how I could draw and remove a rectangle in the window under the cursor without using the methods that I am now?