Paint event example for Visual Basic

This example uses some Windows API functions to demonstrate the following:

Paint event

Click event

Enabled property

hWnd property

1. Start with the project that you created in Loading and Displaying an Image.

2. On the Insert menu, select the Module option, and use the API Text Viewer to add the following from the appropriate (16- or 32-bit) Windows API file:

Data types: RECT and POINTAPI

Declarations: InvalidateRect, LineTo, and MoveToEx

3. Add global variables for MyRect and MyPoint as shown in the following 16-bit code:

Type RECT
    left As Integer
    top As Integer
    right As Integer
    bottom As Integer
End Type
Global MyRect As RECT

Type POINTAPI
    x As Integer
    y As Integer
End Type
Global MyPoint As POINTAPI

Declare Sub InvalidateRect Lib "User" (ByVal hWnd As Integer, lpRect As RECT, ByVal bErase As Integer)
Declare Function LineTo Lib "GDI" (ByVal hdc As Integer, ByVal x As Integer, ByVal y As Integer) As Integer
Declare Function MoveToEx Lib "GDI" (ByVal hdc As Integer, ByVal x As Integer, ByVal y As Integer, lpPoint As POINTAPI) As Integer

4. Close the code module.

5. Return to Form1, add a command button to the top of your main form, and in the properties box, change both its name and caption to Toggle.

6. Add a text box to the top of your form, and in the properties box, change its name to Directions, and change its Text property to the following:

Use the Toggle button to turn on or turn off Click-Splat

7. Add the following code to the Toggle button's Click procedure. This code uses the Enabled property to toggle the LEAD control's ability to respond to click events.

If (Lead1.Enabled = False) Then
  Lead1.Enabled = True
  Directions.Caption = "Click on the image for a splat"
  Toggle.Text = "Disable"
Else
  Lead1.Enabled = False
  Directions.Text = "Click does nothing"
  Toggle.Caption = "Enable"
End If

8. Add the following code to the LEAD1 Click procedure. When the Enabled property is true, this code uses a Windows API call to generate a paint event.

' Use pixels for API calls
SavedMode = Lead1.ScaleMode
Lead1.ScaleMode = 3 'Pixels

' Use InvalidateRect to generate a paint message
MyRect.left = 0
MyRect.top = 0
MyRect.bottom = Lead1.ScaleHeight
MyRect.right = Lead1.ScaleWidth

hCtl = Form1.Lead1.hWnd
InvalidateRect hCtl, MyRect, True

' Set the ScaleMode back to the default
Lead1.ScaleMode = SavedMode

9. Add the following code to the Lead1 Paint procedure. It uses Windows GDI functions to draw random lines on the control whenever there is a paint event.

MyPoint.x = 0
MyPoint.y = 0

' Create a device context for the control that the GDI functions can access
LeadClientDC = Lead1.GetClientDC

' Draw random lines on the control. This overlay does not affect the bitmap.
xStart = Int((Rnd(1) * Lead1.ScaleWidth) + 1)
yStart = Int((Rnd(1) * Lead1.ScaleHeight) + 1)
MyReturn = MoveToEx(LeadClientDC, xStart, yStart, MyPoint)
For i = 1 To 50
  xDelta = Int(81 * Rnd(1)) - 40
  yDelta = Int(81 * Rnd(1)) - 40
  xEnd = xStart + xDelta
  yEnd = yStart + yDelta
  MyReturn = LineTo(LeadClientDC, xEnd, yEnd)
  MyReturn = MoveToEx(LeadClientDC, xStart, yStart, MyPoint)
Next

' Remove the lock from the device control
Lead1.ReleaseClientDC

10. Run your program to test it.