On Tue, 04 Jan 2005 16:45:15 +0100, Mattias Sjögren
<ma************ ********@mvps.o rg> wrote:
Joergen,
Am trying to paint a simple rectangle directly on the screen DC
using the code below, but nothing happens?!?
I believe you want to use GetDC(0) rather than
GetDC(GetDeskt opWindow()).
Yep. That was it. I got the GetDesktopWindo w() code from
a screen grabber program. Worked great for the screen grabber,
but not for me.
And if you care about 64-bit compatibility, you really should use
IntPtr rather than Int32 for all handle parameters.
OK. Will do. Thanks. I was wondering about that. Seems like
Integer/IntPtr/Int32 are used randomly depending on what
sample code one is looking at. I just picked one.
I converted all handles to IntPtr, but this meant that I could not
use GetDC(0) as the compiler complained about not being able
to convert an integer to intptr.
I used GetDC(Nothing) instead. Check the revised code/definitions
below. Is this correct? Seems to work now.
/Joergen Bech
----
Private Declare Function GetDesktopWindo w Lib "user32" () As
IntPtr
Private Declare Function GetDC Lib "user32" (ByVal hwnd As IntPtr)
As IntPtr
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As
IntPtr, ByVal hdc As IntPtr) As IntPtr
Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As
IntPtr, ByVal hObject As IntPtr) As IntPtr
Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As
Int32, ByVal nWidth As Int32, ByVal crColor As Int32) As IntPtr
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject
As IntPtr) As Int32
Private Declare Function Rectangle Lib "gdi32" (ByVal hDC As
IntPtr, ByVal X1 As Int32, ByVal Y1 As Int32, ByVal X2 As Int32, ByVal
Y2 As Int32) As Int32
Private Declare Function CreateBrushIndi rect Lib "gdi32" (ByRef
lpLogBrush As LOGBRUSH) As IntPtr
Private Structure LOGBRUSH
Dim lbColor As Int32
Dim lbHatch As Int32
Dim lbStyle As Int32
End Structure
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click
Test()
End Sub
Private Sub Test()
Dim newBrush As IntPtr
Dim newPen As IntPtr
Dim oldBrush As IntPtr
Dim oldPen As IntPtr
Dim dl As IntPtr
Dim lb As LOGBRUSH
' Get the desktop size in pixels.
Dim desktop_dc As IntPtr = GetDC(Nothing)
' Do something with the desktop device context
lb.lbStyle = 0 'BS_SOLID
lb.lbColor = 0
lb.lbHatch = 0
newBrush = CreateBrushIndi rect(lb)
newPen = CreatePen(0, 10, 0) 'Style=PS_SOLID
oldBrush = SelectObject(de sktop_dc, newBrush)
oldPen = SelectObject(de sktop_dc, newPen)
Rectangle(deskt op_dc, 0, 0, 100, 200)
dl = SelectObject(de sktop_dc, oldPen)
dl = SelectObject(de sktop_dc, oldBrush)
DeleteObject(ne wPen)
DeleteObject(ne wBrush)
' Release the bitmap's and desktop's DCs.
ReleaseDC(Nothi ng, desktop_dc)
End Sub