Click here to Skip to main content
16,012,468 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have draw Rectangle on Desktop but now I have some Problems with ths.
It is desapearing when I refresh desktop or moving above some window. I need do have this Rectangle on Desktop until I Press some Button(for example Refresh Button).

What I have tried:

So, I am drawing Rectangle on Desktop with following code:

C#
Pen pen = new Pen(Color.Yellow, 2);
            IntPtr desktop = GetDC(IntPtr.Zero);
            using (Graphics g = Graphics.FromHdc(desktop))
            {
                g.DrawRectangle(pen, x, y, width, height);
            }
            ReleaseDC(IntPtr.Zero, desktop);
Posted
Updated 11-Jul-16 14:23pm
Comments
[no name] 10-Jul-16 8:20am    
You should create a transparent window without border, for which you set "Parent" to the desktop. This will receive then the WM_PAINT Messages where you can refresh the rectangle.
dave_bulac 10-Jul-16 8:30am    
what do you mean "for which you set "Parent" to the desktop" ?
[no name] 10-Jul-16 8:57am    
BillWoodruff 12-Jul-16 6:20am    
It would be interesting to know the purpose, the reason, why you want to do this, and knowing that might assist us to assist you.

1 solution

Drawing anything in Windows ia transitory, unless it is drawn onto the surface of a bitmap or similar that is redrawn when necessary. As a result, if you don't re-draw your rectangle when another app covers it up and then it becomes visible again it will disappear - there really isn't anything you can do about that.

And your app doesn't "own" the desktop, so there is no mechanism for your app to be notified that your rectangle needs re-drawing.
You could do it with a Timer - but that's a messy and nasty kludge - but other than that, you can copy the desktop, create a temporary wallpaper and draw on that so the system "maintains" your drawing for you.
This may help: c# - Change desktop wallpaper using code in .NET - Stack Overflow[^]
 
Share this answer
 
Comments
Philippe Mori 12-Jul-16 9:26am    
Obviously, don't change wall papers. What would happen if the system is restarted or multiple wallpapers are used (Windows 8) or virtual desktops (Windows 10)...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900