PaintPalette example for Access 95 and 97

This example sets the paint palette to the palette of each Lead control. It then loads two images and dithers them to 8 bits per pixel with optimized palettes, and displays them side by side. In 8-bit display mode, you can see the palette problems that result when you do not use a fixed palette.

' Declare local variables
Dim HeightFactor1, WidthFactor1, HeightFactor2, WidthFactor2, HeightAllowed, WidthAllowed 
Dim ImageHeight1, ImageWidth1, ImageHeight2, ImageWidth2 
Dim ImageLeft2, ImageTop2, ImageLeft1, ImageTop1

' Make the LEAD controls invisible
Me![Lead1].Visible = False
Me![Lead2].Visible = False

' Clear the bitmaps from memory
Lead1.Bitmap = 0
Lead2.Bitmap = 0

' Turn off the foreground palette
Lead1.ForePalette = False
Lead2.ForePalette = False

'Clear the paint palette
Lead1.PaintPalette = PAINTPALETTE_AUTO
Lead2.PaintPalette = PAINTPALETTE_AUTO

' Disable automatic repainting of the image.
Lead1.AutoRepaint = False
Lead2.AutoRepaint = False

' Turn off scroll bars to make sure we use the full client area.
Lead1.AutoScroll = False
Lead2.AutoScroll = False

DoCmd.Hourglass True

' Load the bitmaps. These hard-coded path names may be different on your system.
Lead1.Load "c:\lead\images\image1.cmp", 0, 0, 1
Lead2.Load "c:\lead\images\image2.cmp", 0, 0, 1

' Change the bitmaps to 8 BPS with optimized palettes
Lead1.ColorRes 8, CRP_OPTIMIZEDPALETTE, CRD_FLOYDSTEINDITHERING, 0
Lead2.ColorRes 8, CRP_OPTIMIZEDPALETTE, CRD_FLOYDSTEINDITHERING, 0

' Make the controls visible so that we can size and position them.
Me![Lead1].Visible = True
Me![Lead2].Visible = True

'Set the variables used for preserving the aspect ratio.
HeightFactor1 = Lead1.BitmapHeight
WidthFactor1 = Lead1.BitmapWidth
HeightFactor2 = Lead2.BitmapHeight
WidthFactor2 = Lead2.BitmapWidth
HeightAllowed = Me.WindowHeight * 0.8
WidthAllowed = Me.WindowWidth * 0.45

'Center each LEAD control on half of the form, preserving the aspect ratio.
'Check to see if using the maximum width will make the image too tall.
'Set the dimensions based on the result.
If (WidthAllowed * HeightFactor1) / WidthFactor1 < HeightAllowed Then
  Me![Lead1].Left = (Me.WindowWidth / 4) - (WidthAllowed / 2)
  Me![Lead1].Width = WidthAllowed
  Me![Lead1].Height = (Me![Lead1].Width * HeightFactor1) / WidthFactor1
  Me![Lead1].Top = (Me.WindowHeight - Me![Lead1].Height) / 2
Else
  Me![Lead1].Top = (Me.WindowHeight - HeightAllowed) / 2
  Me![Lead1].Height = HeightAllowed
  Me![Lead1].Width = (Me![Lead1].Height * WidthFactor1) / HeightFactor1
  Me![Lead1].Left = (Me.WindowWidth / 4) - (Me![Lead1].Width / 2)
End If

If (WidthAllowed * HeightFactor2) / WidthFactor2 < HeightAllowed Then
  Me![Lead2].Left = (Me.WindowWidth * 3 / 4) - (WidthAllowed / 2)
  Me![Lead2].Width = WidthAllowed
  Me![Lead2].Height = (Me![Lead2].Width * HeightFactor2) / WidthFactor2
  Me![Lead2].Top = (Me.WindowHeight - Me![Lead2].Height) / 2
Else
  Me![Lead2].Top = (Me.WindowHeight - HeightAllowed) / 2
  Me![Lead2].Height = HeightAllowed
  Me![Lead2].Width = (Me![Lead2].Height * WidthFactor2) / HeightFactor2
  Me![Lead2].Left = (Me.WindowWidth * 3 / 4) - (Me![Lead2].Width / 2)
End If

'Set the image display sizes to match the LEAD controls
ImageHeight1 = Lead1.ScaleHeight
ImageWidth1 = Lead1.ScaleWidth
Lead1.SetDstRect 0, 0, ImageWidth1, ImageHeight1
Lead1.SetDstClipRect 0, 0, ImageWidth1, ImageHeight1

'Set the image display sizes to match the LEAD controls
ImageHeight2 = Lead2.ScaleHeight
ImageWidth2 = Lead2.ScaleWidth
Lead2.SetDstRect ImageLeft2, ImageTop2, ImageWidth2, ImageHeight2
Lead2.SetDstClipRect ImageLeft2, ImageTop2, ImageWidth2, ImageHeight2

'Display the images
Lead1.ForceRepaint
Lead2.ForceRepaint
DoCmd.Hourglass False

' Show differences
MsgBox "Currently using Access palette. Click to paint Lead1 with forepalette"
Lead1.ForePalette = True
Lead1.ForceRepaint
MsgBox "Click to paint Lead2 with forepalette"
Lead2.ForePalette = True
Lead2.ForceRepaint