469,304 Members | 1,800 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

Draw directly to screen

Is it possible to draw directly to the screen using the .net libraries? I
thought that it might be possible with GDI+ but I can't find out how to do it
anywhere. A part of me suspects I may have to look into using the Windows API.

Any ideas,

Darrell

Nov 16 '05 #1
6 39160
Within your own window you can draw using the Graphics object passed in the
OnPaint or Paint event arguments.

Dou you mean directly on the desktop? Yes you can by obtaining the desktop
handle and wrapping it in a GDI+ Graphics object using Graphics.FromHdc.

I generally use interop for this and import the GetDc and ReleaseDc methods.
You must remember to correctly release DC's when you do this.

It's not neat or pretty though...

[DllImport("User32.dll")]

public static extern IntPtr GetDC(IntPtr hwnd);

[DllImport("User32.dll")]

public static extern void ReleaseDC(IntPtr dc);

protected override void OnPaint(PaintEventArgs e)

{

SolidBrush b=new SolidBrush(Color.Red);
IntPtr desktopDC=GetDC(IntPtr.Zero);

Graphics g = Graphics.FromHdc(desktopDC);

g.FillEllipse(b,0,0,1024,768);

g.Dispose();

ReleaseDC(desktopDC);

}
--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"redneon" <re*****@discussions.microsoft.com> wrote in message
news:05**********************************@microsof t.com...
Is it possible to draw directly to the screen using the .net libraries? I
thought that it might be possible with GDI+ but I can't find out how to do
it
anywhere. A part of me suspects I may have to look into using the Windows
API.

Any ideas,

Darrell

Nov 16 '05 #2
You cannot draw somewhere but within your own created windows.
That means you have to create a window before you can draw something.
If you want to make appear something on the users desktop you can always
make a borderless shaped/transparent window but you cannot draw without a
window neither with .net nor with the windows API.

"redneon" <re*****@discussions.microsoft.com> schrieb im Newsbeitrag
news:05**********************************@microsof t.com...
Is it possible to draw directly to the screen using the .net libraries? I
thought that it might be possible with GDI+ but I can't find out how to do it anywhere. A part of me suspects I may have to look into using the Windows API.
Any ideas,

Darrell

Nov 16 '05 #3
This is great. Thanks. There's just another problem with it though. It only
draws it once. Is there any way to get it to constantly draw it? I'm
wondering if there's an event message I could capture in public override
WndProc for a screen refresh and stick it in there. I tried putting it in a
recursive while loop in it's own thread but obviously this is a bad idea and,
while it worked, it ended up just recursively drawing, jerking and generally
looking a mess.

Darrell

"Bob Powell [MVP]" wrote:
Within your own window you can draw using the Graphics object passed in the
OnPaint or Paint event arguments.

Dou you mean directly on the desktop? Yes you can by obtaining the desktop
handle and wrapping it in a GDI+ Graphics object using Graphics.FromHdc.

I generally use interop for this and import the GetDc and ReleaseDc methods.
You must remember to correctly release DC's when you do this.

It's not neat or pretty though...

[DllImport("User32.dll")]

public static extern IntPtr GetDC(IntPtr hwnd);

[DllImport("User32.dll")]

public static extern void ReleaseDC(IntPtr dc);

protected override void OnPaint(PaintEventArgs e)

{

SolidBrush b=new SolidBrush(Color.Red);
IntPtr desktopDC=GetDC(IntPtr.Zero);

Graphics g = Graphics.FromHdc(desktopDC);

g.FillEllipse(b,0,0,1024,768);

g.Dispose();

ReleaseDC(desktopDC);

}
--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"redneon" <re*****@discussions.microsoft.com> wrote in message
news:05**********************************@microsof t.com...
Is it possible to draw directly to the screen using the .net libraries? I
thought that it might be possible with GDI+ but I can't find out how to do
it
anywhere. A part of me suspects I may have to look into using the Windows
API.

Any ideas,

Darrell


Nov 16 '05 #4
What about this funny idea: Generate a composition between the users current
desktop wallpaper and the stuff you want to draw.
That means you have to draw into this wallpaperbitmap in memory, write it to
a new file and set it as wallpaper.
The question is how often the things you are drawing should be refreshed.
May I ask what exactly you are drawing?

"redneon" <re*****@discussions.microsoft.com> schrieb im Newsbeitrag
news:F1**********************************@microsof t.com...
This is great. Thanks. There's just another problem with it though. It only draws it once. Is there any way to get it to constantly draw it? I'm
wondering if there's an event message I could capture in public override
WndProc for a screen refresh and stick it in there. I tried putting it in a recursive while loop in it's own thread but obviously this is a bad idea and, while it worked, it ended up just recursively drawing, jerking and generally looking a mess.

Darrell

"Bob Powell [MVP]" wrote:
Within your own window you can draw using the Graphics object passed in the OnPaint or Paint event arguments.

Dou you mean directly on the desktop? Yes you can by obtaining the desktop handle and wrapping it in a GDI+ Graphics object using Graphics.FromHdc.

I generally use interop for this and import the GetDc and ReleaseDc methods. You must remember to correctly release DC's when you do this.

It's not neat or pretty though...

[DllImport("User32.dll")]

public static extern IntPtr GetDC(IntPtr hwnd);

[DllImport("User32.dll")]

public static extern void ReleaseDC(IntPtr dc);

protected override void OnPaint(PaintEventArgs e)

{

SolidBrush b=new SolidBrush(Color.Red);
IntPtr desktopDC=GetDC(IntPtr.Zero);

Graphics g = Graphics.FromHdc(desktopDC);

g.FillEllipse(b,0,0,1024,768);

g.Dispose();

ReleaseDC(desktopDC);

}
--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"redneon" <re*****@discussions.microsoft.com> wrote in message
news:05**********************************@microsof t.com...
Is it possible to draw directly to the screen using the .net libraries? I thought that it might be possible with GDI+ but I can't find out how to do it
anywhere. A part of me suspects I may have to look into using the Windows API.

Any ideas,

Darrell


Nov 16 '05 #5
> What about this funny idea: Generate a composition between the users current
desktop wallpaper and the stuff you want to draw.


There are about a million problems with that. Firstly, we're not all running
Cray supercomputers so the refresh rate will kill people. Secondly, I want to
draw on top of Windows too and setting the wallpaper would draw behind them.

Nov 16 '05 #6
Thats why I asked you what exactly you are trying to achieve. There is
always the option to make a topmost small invible/translucent window and
draw you stuff in there.

"redneon" <re*****@discussions.microsoft.com> schrieb im Newsbeitrag
news:5F**********************************@microsof t.com...
What about this funny idea: Generate a composition between the users current desktop wallpaper and the stuff you want to draw.
There are about a million problems with that. Firstly, we're not all

running Cray supercomputers so the refresh rate will kill people. Secondly, I want to draw on top of Windows too and setting the wallpaper would draw behind them.

Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by ˴ɽ | last post: by
1 post views Thread by redneon | last post: by
2 posts views Thread by Hai Ly Hoang | last post: by
8 posts views Thread by Oberfuhrer | last post: by
2 posts views Thread by myjpa1 | last post: by
reply views Thread by active | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.