Connect with Expertise | Find Experts, Get Answers, Share Insights

Draw directly to screen

redneon
 
Posts: n/a
#1: Nov 16 '05
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


Bob Powell [MVP]
 
Posts: n/a
#2: Nov 16 '05

re: Draw directly to screen


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" <redneon@discussions.microsoft.com> wrote in message
news:05A27CF9-3496-4CF0-BE6D-CA9E812AB486@microsoft.com...[color=blue]
> 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
>[/color]


cody
 
Posts: n/a
#3: Nov 16 '05

re: Draw directly to screen


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" <redneon@discussions.microsoft.com> schrieb im Newsbeitrag
news:05A27CF9-3496-4CF0-BE6D-CA9E812AB486@microsoft.com...[color=blue]
> 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[/color]
it[color=blue]
> anywhere. A part of me suspects I may have to look into using the Windows[/color]
API.[color=blue]
>
> Any ideas,
>
> Darrell
>[/color]


redneon
 
Posts: n/a
#4: Nov 16 '05

re: Draw directly to screen


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:
[color=blue]
> 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" <redneon@discussions.microsoft.com> wrote in message
> news:05A27CF9-3496-4CF0-BE6D-CA9E812AB486@microsoft.com...[color=green]
> > 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
> >[/color]
>
>
>[/color]
cody
 
Posts: n/a
#5: Nov 16 '05

re: Draw directly to screen


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" <redneon@discussions.microsoft.com> schrieb im Newsbeitrag
news:F11663C1-5B11-4464-B485-ED56851B2C76@microsoft.com...[color=blue]
> This is great. Thanks. There's just another problem with it though. It[/color]
only[color=blue]
> 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[/color]
a[color=blue]
> recursive while loop in it's own thread but obviously this is a bad idea[/color]
and,[color=blue]
> while it worked, it ended up just recursively drawing, jerking and[/color]
generally[color=blue]
> looking a mess.
>
> Darrell
>
> "Bob Powell [MVP]" wrote:
>[color=green]
> > Within your own window you can draw using the Graphics object passed in[/color][/color]
the[color=blue][color=green]
> > OnPaint or Paint event arguments.
> >
> > Dou you mean directly on the desktop? Yes you can by obtaining the[/color][/color]
desktop[color=blue][color=green]
> > handle and wrapping it in a GDI+ Graphics object using Graphics.FromHdc.
> >
> > I generally use interop for this and import the GetDc and ReleaseDc[/color][/color]
methods.[color=blue][color=green]
> > 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" <redneon@discussions.microsoft.com> wrote in message
> > news:05A27CF9-3496-4CF0-BE6D-CA9E812AB486@microsoft.com...[color=darkred]
> > > Is it possible to draw directly to the screen using the .net[/color][/color][/color]
libraries? I[color=blue][color=green][color=darkred]
> > > thought that it might be possible with GDI+ but I can't find out how[/color][/color][/color]
to do[color=blue][color=green][color=darkred]
> > > it
> > > anywhere. A part of me suspects I may have to look into using the[/color][/color][/color]
Windows[color=blue][color=green][color=darkred]
> > > API.
> > >
> > > Any ideas,
> > >
> > > Darrell
> > >[/color]
> >
> >
> >[/color][/color]


redneon
 
Posts: n/a
#6: Nov 16 '05

re: Draw directly to screen


> What about this funny idea: Generate a composition between the users current[color=blue]
> desktop wallpaper and the stuff you want to draw.[/color]

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.

cody
 
Posts: n/a
#7: Nov 16 '05

re: Draw directly to screen


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" <redneon@discussions.microsoft.com> schrieb im Newsbeitrag
news:5FE4A440-93EE-404D-AA3C-F929581E0F7C@microsoft.com...[color=blue][color=green]
> > What about this funny idea: Generate a composition between the users[/color][/color]
current[color=blue][color=green]
> > desktop wallpaper and the stuff you want to draw.[/color]
>
> There are about a million problems with that. Firstly, we're not all[/color]
running[color=blue]
> Cray supercomputers so the refresh rate will kill people. Secondly, I want[/color]
to[color=blue]
> draw on top of Windows too and setting the wallpaper would draw behind[/color]
them.[color=blue]
>[/color]


Closed Thread

Tags
"screen c#"