468,490 Members | 2,606 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

How can I draw on the desktop?

Don
Is there any way I can get an object that allows me to draw to the Windows
desktop using managed code? I've managed to figure out how to do it with
unmanaged code using API functions, but I was just wondering if there was a
more "proper" way to do it in VB.NET

- Don
Nov 20 '05 #1
7 7886
* "Don" <un*****@oblivion.com> scripsit:
Is there any way I can get an object that allows me to draw to the Windows
desktop using managed code? I've managed to figure out how to do it with
unmanaged code using API functions, but I was just wondering if there was a
more "proper" way to do it in VB.NET


AFAIS that's not supported by the .NET Framework.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #2
Don
> > Is there any way I can get an object that allows me to draw to the
Windows
desktop using managed code? I've managed to figure out how to do it with unmanaged code using API functions, but I was just wondering if there was a more "proper" way to do it in VB.NET


AFAIS that's not supported by the .NET Framework.


That would explain why System.Drawing.Graphics.FromHwnd() works when passed
a handle to a form but not when passed a handle to the desktop. :-(

- Don
Nov 20 '05 #3
"Don" <un*****@oblivion.com> schrieb
Is there any way I can get an object that allows me to draw to the
Windows desktop using managed code? I've managed to figure out how
to do it with unmanaged code using API functions, but I was just
wondering if there was a more "proper" way to do it in VB.NET

Private Declare Function GetDC Lib "user32.dll" _
(ByVal hwnd As IntPtr) As IntPtr
Private Declare Function ReleaseDC Lib "user32.dll" _
(ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As IntPtr

'...
Dim hdc As IntPtr

hdc = GetDC(IntPtr.Zero)

Try
Dim g As Graphics
g = Graphics.FromHdc(hdc)
Try
g.FillRectangle(Brushes.Red, 10, 10, 100, 100)
Finally
g.Dispose()
End Try
Finally
ReleaseDC(IntPtr.Zero, hdc)
End Try
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #4
Sounds like you found the only way. The only thing I'd suggest is writing a managed code wrapper class for the unmanaged code functions you're using. At least that way you could call managed code methods from your application. (The wrapper calls the unmanaged code functions within the methods you define, and handles the exceptions.) That's what I do when I have to work with the shell, BITS, and other Windows APIs

Good luck
Ben
Nov 20 '05 #5
* "Armin Zingler" <az*******@freenet.de> scripsit:
"Don" <un*****@oblivion.com> schrieb
Is there any way I can get an object that allows me to draw to the
Windows desktop using managed code? I've managed to figure out how [...]

Private Declare Function GetDC Lib "user32.dll" _
(ByVal hwnd As IntPtr) As IntPtr
Private Declare Function ReleaseDC Lib "user32.dll" _
(ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As IntPtr


That's not managed code...

:-)

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #6
All I have to say is Longhorn.

Hopefully.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:OI**************@TK2MSFTNGP10.phx.gbl...
* "Armin Zingler" <az*******@freenet.de> scripsit:
"Don" <un*****@oblivion.com> schrieb
Is there any way I can get an object that allows me to draw to the
Windows desktop using managed code? I've managed to figure out how
[...] Private Declare Function GetDC Lib "user32.dll" _
(ByVal hwnd As IntPtr) As IntPtr
Private Declare Function ReleaseDC Lib "user32.dll" _
(ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As IntPtr


That's not managed code...

:-)

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #7
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schrieb
Private Declare Function ReleaseDC Lib "user32.dll" _
(ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As IntPtr


That's not managed code...

:-)


Ahhh, sorry!! I didn't read conscientiously. :-/
--
Armin

Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Matt | last post: by
reply views Thread by Florida Draw | last post: by
5 posts views Thread by Matt | last post: by
3 posts views Thread by 八大山人 | last post: by
6 posts views Thread by redneon | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.