Reputation: 3187
I am writing an app such that user can draw annotation (circle, line, rectangle, etc) on the desktop directly.
I am using GetDC from User32.dll and successfully draw a rectangle on the desktop :
namespace Annotation
{
public partial class BaseForm : Form
{
[DllImport("User32.dll")]
static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("User32.dll")]
static extern void ReleaseDC(IntPtr dc);
public BaseForm()
{
InitializeComponent();
BackColor = Color.Lime;
TransparencyKey = Color.Lime;
}
private void button1_Click(object sender, EventArgs e)
{
IntPtr desktop = GetDC(IntPtr.Zero);
using (Graphics g = Graphics.FromHdc(desktop))
{
g.FillRectangle(Brushes.Red, 0, 0, 100, 100);
}
}
}
}
When I click the button, a rectangle will be drawn on the desktop. But when I move over the mouse over the rectangle, the desktop will be redrawn and my rectangle will disappear.
Is there a way such that I can make the rectangle stay there forever (until I clear the drawing)?
Thanks.
Upvotes: 0
Views: 768
Reputation: 612993
Is there a way such that I can make the rectangle stay there forever (until I clear the drawing)?
Not if you insist on drawing directly on the desktop. The desktop window is owned by the system. When the system decides that it needs to be re-painted, for instance because another window passed over the desktop, it will re-paint the desktop. Since the system knows nothing of what you wish to appear, it cannot re-paint your annotations.
If you want persistent state to appear on the desktop, you must create a window in which to draw that state. Create a transparent click-through window, and paint what you will in that window.
Upvotes: 4