PaintPalette example for C++ 4.0 and later

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.

// Disable automatic repainting of the image.
m_Lead1.SetAutoRepaint(FALSE);
m_Lead2.SetAutoRepaint(FALSE);
// Turn off scroll bars to make sure we use the full client area.
m_Lead1.SetAutoScroll(FALSE);
m_Lead2.SetAutoScroll(FALSE);
// Clear the bitmaps from memory
m_Lead1.SetBitmap(0);
m_Lead2.SetBitmap(0);
BeginWaitCursor();
// Load the bitmaps. These path names may be different on your system.
m_Lead1.Load("c:\\lead\\images\\image1.cmp", 0, 0, 1);
m_Lead2.Load("c:\\lead\\images\\image2.cmp", 0, 0, 1);
// Change the bitmaps to 8 BPS with optimized palettes
m_Lead1.ColorRes(8, CRP_OPTIMIZEDPALETTE, CRD_FLOYDSTEINDITHERING, 0);
m_Lead2.ColorRes(8, CRP_OPTIMIZEDPALETTE, CRD_FLOYDSTEINDITHERING, 0);
// Make the controls visible so that we can size and position them.
// They will not really appear until we load an image and paint it.
m_Lead1.ShowWindow(SW_NORMAL);
m_Lead2.ShowWindow(SW_NORMAL);
// Set the variables used for preserving the aspect ratio.
float HeightFactor1 = m_Lead1.GetBitmapHeight();
float WidthFactor1 = m_Lead1.GetBitmapWidth();
float HeightFactor2 = m_Lead2.GetBitmapHeight();
float WidthFactor2 = m_Lead2.GetBitmapWidth();
CRect rcWindow;
GetWindowRect(rcWindow);
float ScaleWidth = (float) rcWindow.Width();
float ScaleHeight = (float) rcWindow.Height();
float HeightAllowed = ScaleHeight * 0.8f;
float WidthAllowed = ScaleWidth * 0.45f;
// 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.
float Left;
float Width;
float Height;
float Top;
CRect rcControl;
if ((WidthAllowed * HeightFactor1) / WidthFactor1 < HeightAllowed)
{
  Left =  (ScaleWidth / 4) - (WidthAllowed / 2);
  Width = WidthAllowed;
  Height = (Width * HeightFactor1) / WidthFactor1;
  Top = (ScaleHeight - Height) / 2;
}
else
{
  Top = (ScaleHeight - HeightAllowed) / 2;
  Height = HeightAllowed;
  Width = (Height * WidthFactor1) / HeightFactor1;
  Left = (ScaleWidth / 4) - (Width / 2);
}
rcControl.SetRect(0, 0, (int) Width, (int) Height);
rcControl.OffsetRect((int) Left, (int) Top);
m_Lead1.MoveWindow(rcControl);
if ((WidthAllowed * HeightFactor2) / WidthFactor2 < HeightAllowed)
{
  Left =  (ScaleWidth * 3/4) - (WidthAllowed / 2);
  Width = WidthAllowed;
  Height = (Width * HeightFactor2) / WidthFactor2;
  Top = (ScaleHeight - Height) / 2;
}
else
{
  Top = (ScaleHeight - HeightAllowed) / 2;
  Height = HeightAllowed;
  Width = (Height * WidthFactor2) / HeightFactor2;
  Left = (ScaleWidth * 3 / 4) - (Width / 2);
}
rcControl.SetRect(0, 0, (int) Width, (int) Height);
rcControl.OffsetRect((int) Left, (int) Top);
m_Lead2.MoveWindow(rcControl);
// Set the image display sizes to match the LEAD controls
m_Lead1.SetDstRect(0.0f, 0.0f, m_Lead1.GetScaleWidth(), m_Lead1.GetScaleHeight());
m_Lead1.SetDstClipRect(0.0f, 0.0f, m_Lead1.GetScaleWidth(), m_Lead1.GetScaleHeight());
m_Lead2.SetDstRect(0.0f, 0.0f, m_Lead2.GetScaleWidth(), m_Lead2.GetScaleHeight());
m_Lead2.SetDstClipRect(0.0f, 0.0f, m_Lead2.GetScaleWidth(), m_Lead2.GetScaleHeight());
// Display the images
m_Lead1.SetPaintPalette(PAINTPALETTE_AUTO);
m_Lead2.SetPaintPalette(PAINTPALETTE_AUTO);
m_Lead1.ForceRepaint();
m_Lead2.ForceRepaint();
EndWaitCursor();