How can I draw on the desktop?

  • Thread starter Thread starter Don
  • Start date Start date
D

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
 
* "Don said:
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.
 
Is there any way I can get an object that allows me to draw to the
Windows
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
 
Don said:
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
 
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
 
* "Armin Zingler said:
Don said:
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...

:-)
 
All I have to say is Longhorn.

Hopefully.

Herfried K. Wagner said:
* "Armin Zingler said:
Don said:
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...

:-)
 
Back
Top