J
Joergen Bech
Am trying to paint a simple rectangle directly on the screen DC
using the code below, but nothing happens?!?
Weird. Anyone who can tell me what I am doing wrong? This
ought to be straightforward, but ...
TIA,
Joergen Bech
---
'Start new project, add a button to a form, then paste the following
'code:
Private Declare Function GetDesktopWindow Lib "user32" () As Int32
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Int32)
As Int32
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As
Int32, ByVal hdc As Int32) As Int32
Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As
Int32, ByVal hObject As Int32) As Int32
Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As
Int32, ByVal nWidth As Int32, ByVal crColor As Int32) As Int32
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject
As Int32) As Int32
Private Declare Function Rectangle Lib "gdi32" (ByVal hDC As
Int32, ByVal X1 As Int32, ByVal Y1 As Int32, ByVal X2 As Int32, ByVal
Y2 As Int32) As Int32
Private Declare Function CreateBrushIndirect Lib "gdi32" (ByRef
lpLogBrush As LOGBRUSH) As Int32
Private Structure LOGBRUSH
Dim lbColor As Int32
Dim lbHatch As Int32
Dim lbStyle As Int32
End Structure
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Test()
End Sub
Private Sub Test()
Dim newBrush As Int32
Dim newPen As Int32
Dim oldBrush As Int32
Dim oldPen As Int32
Dim dl As Int32
Dim lb As LOGBRUSH
' Get the desktop size in pixels.
Dim desktop_win As Int32 = GetDesktopWindow()
Dim desktop_dc As Int32 = GetDC(desktop_win)
Debug.WriteLine(desktop_win)
Debug.WriteLine(desktop_dc)
' Do something with the desktop device context
lb.lbStyle = 0 'BS_SOLID
lb.lbColor = 0
lb.lbHatch = 0
newBrush = CreateBrushIndirect(lb)
newPen = CreatePen(0, 10, 0) 'Style=PS_SOLID
oldBrush = SelectObject(desktop_dc, newBrush)
oldPen = SelectObject(desktop_dc, newPen)
Rectangle(desktop_dc, 0, 0, 100, 200)
dl = SelectObject(desktop_dc, oldPen)
dl = SelectObject(desktop_dc, oldBrush)
DeleteObject(newPen)
DeleteObject(newBrush)
' Release the bitmap's and desktop's DCs.
ReleaseDC(desktop_win, desktop_dc)
End Sub
using the code below, but nothing happens?!?
Weird. Anyone who can tell me what I am doing wrong? This
ought to be straightforward, but ...

TIA,
Joergen Bech
---
'Start new project, add a button to a form, then paste the following
'code:
Private Declare Function GetDesktopWindow Lib "user32" () As Int32
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Int32)
As Int32
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As
Int32, ByVal hdc As Int32) As Int32
Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As
Int32, ByVal hObject As Int32) As Int32
Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As
Int32, ByVal nWidth As Int32, ByVal crColor As Int32) As Int32
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject
As Int32) As Int32
Private Declare Function Rectangle Lib "gdi32" (ByVal hDC As
Int32, ByVal X1 As Int32, ByVal Y1 As Int32, ByVal X2 As Int32, ByVal
Y2 As Int32) As Int32
Private Declare Function CreateBrushIndirect Lib "gdi32" (ByRef
lpLogBrush As LOGBRUSH) As Int32
Private Structure LOGBRUSH
Dim lbColor As Int32
Dim lbHatch As Int32
Dim lbStyle As Int32
End Structure
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Test()
End Sub
Private Sub Test()
Dim newBrush As Int32
Dim newPen As Int32
Dim oldBrush As Int32
Dim oldPen As Int32
Dim dl As Int32
Dim lb As LOGBRUSH
' Get the desktop size in pixels.
Dim desktop_win As Int32 = GetDesktopWindow()
Dim desktop_dc As Int32 = GetDC(desktop_win)
Debug.WriteLine(desktop_win)
Debug.WriteLine(desktop_dc)
' Do something with the desktop device context
lb.lbStyle = 0 'BS_SOLID
lb.lbColor = 0
lb.lbHatch = 0
newBrush = CreateBrushIndirect(lb)
newPen = CreatePen(0, 10, 0) 'Style=PS_SOLID
oldBrush = SelectObject(desktop_dc, newBrush)
oldPen = SelectObject(desktop_dc, newPen)
Rectangle(desktop_dc, 0, 0, 100, 200)
dl = SelectObject(desktop_dc, oldPen)
dl = SelectObject(desktop_dc, oldBrush)
DeleteObject(newPen)
DeleteObject(newBrush)
' Release the bitmap's and desktop's DCs.
ReleaseDC(desktop_win, desktop_dc)
End Sub