Paint event example for Visual Basic

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

image\sqrblit.gif Paint event

image\sqrblit.gif Click event

image\sqrblit.gif Enabled property

image\sqrblit.gif Window 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 Windows API defines:

Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type
Private Type POINTAPI
        x As Long
        y As Long
End Type
Private Declare Function InvalidateRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT, ByVal bErase As Long) As Long
Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As POINTAPI) As Long
Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long

3.

Add global variables for MyRect and MyPoint as shown in the following code:

Private MyRect As RECT
Private MyPoint As POINTAPI

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 RasterView control's ability to respond to click events.

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

8.

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

Dim hCtl As Long
Dim SavedMode As RasterScaleModeConstants
' Use pixels for API calls
SavedMode = LEADRasterView1.ScaleMode
LEADRasterView1.ScaleMode = 3 'Pixels
' Use InvalidateRect to generate a paint message
MyRect.Left = 0
MyRect.Top = 0
MyRect.Bottom = LEADRasterView1.ScaleHeight
MyRect.Right = LEADRasterView1.ScaleWidth
hCtl = LEADRasterView1.Window
InvalidateRect hCtl, MyRect, True
' Set the ScaleMode back to the default
LEADRasterView1.ScaleMode = SavedMode

9.

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

Private Sub LEADRasterView1_Paint(ByVal hDC As Long)
Dim LeadClientDC As Long
    Dim xStart As Long
    Dim yStart As Long
    Dim MyReturn As Long
    Dim xDelta As Long
    Dim yDelta As Long
    Dim xEnd As Long
    Dim yEnd As Long
    Dim i As Long
    MyPoint.x = 0
    MyPoint.y = 0
    ' Create a device context for the control that the GDI functions can access
    LeadClientDC = LEADRasterView1.GetClientDC
    ' Draw random lines on the control. This overlay does not affect the bitmap.
    xStart = CLng((Rnd(1) * LEADRasterView1.ScaleWidth) + 1)
    yStart = CLng((Rnd(1) * LEADRasterView1.ScaleHeight) + 1)
    MyReturn = MoveToEx(LeadClientDC, xStart, yStart, MyPoint)
    For i = 1 To 50
      xDelta = CLng(81 * Rnd(1)) - 40
      yDelta = CLng(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
    LEADRasterView1.ReleaseClientDC
End Sub

10.

Run your program to test it.