PixelData example for C++ 4.0 and later

This example, using the MouseDown event, gets the value of the current pixel and sets all values in the line to that value. The example shows how to convert mouse coordinates to bitmap pixel coordinates, regardless of how the image is zoomed or scrolled.

void CTestDlg::OnMouseDownLeadctrl1 (short Button, short Shift, long x, long y) 
{
   COleVariant xycolor;
   float xfraction;
   float yfraction;
   float xPixel;
   float yPixel;
   // In MFC 4.0 you get pixels; so set the scale mode accordingly.
   m_Lead1.SetScaleMode(3);
   m_Lead1.SetAutoRepaint(FALSE);
   // Translate mouse coordinates to fractions of the destination width and height.
   xfraction = (x - m_Lead1.GetDstLeft()) / m_Lead1.GetDstWidth();
   yfraction = (y - m_Lead1.GetDstTop()) / m_Lead1.GetDstHeight();

   // Convert fractions to bitmap pixel coordinates.
   xPixel = (m_Lead1.GetSrcWidth() * xfraction) + m_Lead1.GetSrcLeft();
   yPixel = (m_Lead1.GetSrcHeight() * yfraction) + m_Lead1.GetSrcTop();
   // Make the whole line the same color as the pixel.
   xycolor = m_Lead1.GetPixelData(xPixel, yPixel);
   //disable repainting
   m_Lead1.SetAutoRepaint(FALSE);
   for(xPixel=0; xPixel<m_Lead1.GetBitmapWidth();xPixel++)
   {
     m_Lead1.SetPixelData(xPixel,yPixel,xycolor);
   }
   // re-enable repainting
   m_Lead1.SetAutoRepaint(TRUE);
}