Paint event example for Access 2.0

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

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

2. image\btnmodul.gif In the Database window, select the Module tab, and click the New button.

3. Add the following type definitions and API declarations:

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 file, and save it as the Declarations 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 default value 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 Me![LEAD1].Object.Enabled = False Then
  Me![LEAD1].Object.Enabled = True
  Directions.Value = "Click on the image for a splat"
  Toggle.Caption = "Disable"
Else
  Me![LEAD1].Object.Enabled = False
  Directions.Value = "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 = Me![LEAD1].Object.ScaleMode
Me![LEAD1].Object.ScaleMode = 3 'Pixels

' Use InvalidateRect to generate a paint message
MyWindow = Me![LEAD1].Object.hWnd
MyRect.Left = 0
MyRect.Top = 0
MyRect.bottom = Me![LEAD1].Object.ScaleHeight
MyRect.right = Me![LEAD1].Object.ScaleWidth
InvalidateRect MyWindow, MyRect, True

' Set the ScaleMode back to the default
Me![LEAD1].Object.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.

' Create a device context for the control that the GDI functions can access
LeadClientDC = Me![LEAD1].Object.GetClientDC

' Draw random lines on the control. This is an overlay that does not affect the bitmap.
xStart = Rnd * Me![LEAD1].Object.ScaleWidth
yStart = Rnd * Me![LEAD1].Object.ScaleHeight
MyPoint.x = 0
MyPoint.y = 0
MyReturn = MoveToEx(LeadClientDC, xStart, yStart, MyPoint)
For i = 1 To 50
    xDelta = (Rnd * 80) - 40  ' -40 to 40
    yDelta = (Rnd * 80) - 40  ' -40 to 40
    xEnd = xStart + xDelta
    yEnd = yStart + yDelta
    MyReturn = LineTo(LeadClientDC, xEnd, yEnd)
    MyReturn = MoveToEx(LeadClientDC, xStart, yStart, MyPoint)
Next i

' Remove the lock from the device control
Me![LEAD1].Object.ReleaseClientDC

10. Run your program to test it.