• 0

C# Drawing On Desktop


Question

Hi,

Got a little problem that I hope someone can help me with.

I am drawing a rectangle directly onto the desktop... and that works fine.

However I would like to restore the desktop to its original state (i.e. without rectangle) when an event occurs, for example the mouse button is released.

No matter what I do the rectangle stays put.

Anyone got any ideas????

Thanks in anticipation.

Jon

Link to comment
Share on other sites

18 answers to this question

Recommended Posts

  • 0

Duh! Dunno why I didn't post the code...

Here it is :

using System;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Runtime.InteropServices;

namespace DrawToScreenTestApp

{

#region UnmanagedMethods

internal class UnmanagedMethods

{

[DllImport("user32")]

internal static extern IntPtr GetDC(IntPtr hwnd);

[DllImport("User32.dll")]

internal static extern void ReleaseDC(IntPtr dc);

}

#endregion

#region DrawToScreen

public class DrawToScreen

{

public void PaintRectangleToScreen()

{

IntPtr deskDC = UnmanagedMethods.GetDC(IntPtr.Zero);

Graphics g = Graphics.FromHdc(deskDC);

g.FillRectangle(new SolidBrush(Color.FromArgb(100, Color.CadetBlue)), 0,0, 300, 300);

g.EndContainer(cont);

g.Dispose();

UnmanagedMethods.ReleaseDC(deskDC);

}

}

#endregion

}

Link to comment
Share on other sites

  • 0

All you should have to do is invalidate the desktop rectangle. Import InvalidateRect from GDI, and use the ClipRectangle from the desktop's graphics object as your rectangle. If that doesn't work, I'd be surprised.

Link to comment
Share on other sites

  • 0

Hey Guys,

Thanks for all your replies.

I have tried them all and the only one that I could get to work was weenur's suggestion.

However, it has the horrible side effect of making the whole screen flick. Not good when moving the rectangle across the screen.

So i'm gonna have to do a little more investigation :)

Thanks again

Jon

Link to comment
Share on other sites

  • 0

Is there a way to draw on desktop and be able to remove the drawing at runntime without a flicker effect? I tried weenur's suggestion but it causes the screen to flicker when the InvalidateRect is called. Any idea how to stop the flickering?

Link to comment
Share on other sites

  • 0

You are in no control of the flickering. You tell the desktop (which desktop btw: the desktop as in GetDesktopWindow() or as in the listview with all the icons ? ) to repaint, and that desktop itself is responsible for painting. If this is done flickerfree or not is up to the windows that will need repainting.

By the way, flickering occurs when you draw the same pixel twice onto the screen. This can be solved by being carefull what you are drawing or using a backbuffer. Normally, when dealing with flickering WM_ERASEBACKGROUND is not dealt with, all painting is done in WM_PAINT.

Link to comment
Share on other sites

  • 0

Thank you very much for your answer AndreasV :) So how can other programs paint over the screen without flickering? Can I draw on desktop without telling the desktop to repaint?

Link to comment
Share on other sites

  • 0

There is something you can do to reduce flicker greatly. Use double buffering to increase redraw speed. This renders the graphics to an in-memory bitmap, then copies the finalized bitmap to whereever you want.

Here is an MSDN article on it and here are results from Google.

Hope this helps you, good luck! :)

Link to comment
Share on other sites

  • 0

It might be easier to just draw to a transparent form whose parent is the desktop. This does what programs like Samurize do when you "Pin to desktop." Here's the code for setting the window's parent to the desktop.

[DllImport("user32.dll", CharSet=CharSet.Auto)]
  public static extern IntPtr FindWindow(
  	[MarshalAs(UnmanagedType.LPTStr)] string lpClassName,
  	[MarshalAs(UnmanagedType.LPTStr)] string lpWindowName);
[DllImport("user32.dll")]
  public static extern IntPtr SetParent(
  	IntPtr hWndChild,      // handle to window
  	IntPtr hWndNewParent   // new parent window
  	);


IntPtr hwndf = this.Handle;
IntPtr hwndParent = FindWindow("ProgMan", null);
SetParent(hwndf,hwndParent);
this.TopMost = false;

And this is an excellent article for making the window transparent.

Then, you could just set this.Visible = false; to make the window disappear.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.