How to redraw screen in VB.Net?

A

Andre Nogueira

Hi guys

I am developing a program which draws stuff on the screen (outside of the
form).
For this I use the GetDC function of user32.dll to get a handle to the
desktop, and then use GDI+ to draw. So far so good.
The problem is that before drawing, I have to erase what I had previously
drawn. I tried using the InvalidateRect API function, without luck.
I get this error when I run the program:

A call to PInvoke function 'WindowsApplication1.Form1::InvalidateRect' has
unbalanced the stack. This is likely because the managed PInvoke signature
does not match the unmanaged target signature. Check that the calling
convention and parameters of the PInvoke signature match the target
unmanaged signature.

This is what I have in my code:

Private Declare Function InvalidateRect Lib "user32" (ByVal hWnd As
System.IntPtr, ByVal lpRect As RECT, ByVal bErase As Boolean) As Boolean

Private Structure RECT

Dim Left As Long

Dim Top As Long

Dim Right As Long

Dim Bottom As Long

End Structure



And then in my function:



Dim ii As New RECT

ii.Bottom = 1280

ii.Top = 0

ii.Left = 100

ii.Right = 700

Dim DesktopHandle As System.IntPtr = GetDC(System.IntPtr.Zero)

InvalidateRect(DesktopHandle, ii, True)



Any ideas? I've tried to google it, but to no avail. I'm new in the Windows
API world, so... It's kinda hard for me to get anywhere.

Thank you for any help you might give me!

Andre Nogueira
 
P

Patrice

Long is Integer in VB.NET.

Also I believe this is also available in GDI+. I would use managed class
whenever using COM interop is not required... Similarly the System.Drawing
namespace should allow to retrieve the desktop without having to use interop
(IMO, not tested).
 
A

Andre Nogueira

Hi,

I tried to change "Long" to "Integer" in the RECT struct, but the same error
occurs...

I did try to find managed replacements for these two functions, but couldn't
find any... I'm using VB.Net 2005.

Any other ideas?

Thanks,

Andre
 
P

Patrice

Noticed also that that the RECT structure should be passed byref. Try
http://www.pinvoke.net for all the signatures you need...

System.Drawing.Graphics is basically the counterpart of a DeviceContext. IMO
you should be able to get access to the desktop using its FromHwnd. method.
I remember also to have seen an Invalidate method but this is one is likely
a bit harder to use for your intended usage (if I remember this is either
usable throught the painteventargs, likely available as a member of the
sender control).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top