Paint event example for Access 95 and 97

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. In the Database window, select the Modules tab, and click the New button.

3. Add the following public type definitions and declarations to support the GDI functions. In online help, you can select the block of code with the mouse, then press Ctrl-C to copy it. (Refer to the Access documentation for instructions on using API functions.)

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

4. Close the file, and save it as the Paint event module.

5. In the Database window, select the Forms tab, select Form1, and click the Design button.

6. image\btncmd.gif Select the Command Button control; then add a control to your form. (Cancel the Command Button Wizard when it appears.) Put the control at the top of the form to keep it away from the image.

7. In the Properties box, change the Command Button control's Name property to Toggle and change the Caption property to Toggle.

8. Select the Text Box control; then add a control to your form. (Delete the label portion of the text box.) Put the control at the top of the form to keep it away from the image.

9. In the Properties box, change the Text Box control's Name property to Directions and change its Default Value property to the following:

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

10. Click the Code Window icon on the toolbar.

image\btncode.gif For Access 95.

image\btncode2.gif For Access 97.

11. Select the Click procedure for the Toggle object, and add the following code. In online help, you can select the block of code with the mouse, then press Ctrl-C to copy it.

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

12. Select the Click procedure for the Lead1 object, and add the following code. In online help, you can select the block of code with the mouse, then press Ctrl-C to copy it.

' Declare local variables
Dim SavedMode, MyWindow

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

' Use InvalidateRect to generate a paint message
MyWindow = Lead1.hwnd
MyRect.Left = 0
MyRect.Top = 0
MyRect.Bottom = Lead1.ScaleHeight
MyRect.Right = Lead1.ScaleWidth
InvalidateRect MyWindow, MyRect, True

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

13. Select the Paint procedure for the Lead1 object, and add the following code. In online help, you can select the block of code with the mouse, then press Ctrl-C to copy it.

' Declare local variables
Dim xStart, yStart, xDelta, yDelta, xEnd, yEnd, MyReturn, i, LeadClientDC

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

' Draw random lines on the control. This is an overlay that does not affect the bitmap.
xStart = Rnd * Lead1.ScaleWidth
yStart = Rnd * Lead1.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
Lead1.ReleaseClientDC

14. image\btncmpl.gif Click the compile button on the toolbar; then close the code window.

15. Close the form designer, saving the changes.

16. With Form1 selected in the Database window, click the Open button to test the form.