Paint event example for C++ Builder

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

image\sqrblit.gif Paint event

image\sqrblit.gif Click event

image\sqrblit.gif Enabled property

image\sqrblit.gif Window property

1.

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

2.

Add these variables for MyRect and MyPoint to Unit1.h private section:

RECT MyRect;

3.

image\btncmd.gif Add a button to your form and name it as follows:

 

Name

Caption

 

Toggle

Toggle.

4.

Add an Edit box to the top of your form, and name it as follows:

 

Name

Text

 

Directions

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

5.

Code the Toggle button's Click procedure as the following, This code uses the Enabled property to toggle the LEAD RasterView control's ability to respond to click events.

void __fastcall TForm1::ToggleClick(TObject *Sender)
{
if (LEADRasterView1->Enabled == false)
{
 LEADRasterView1->Enabled= true;
 Directions->Text= "Click on the image for a splat";
 Toggle->Caption= "Disable";
}
 else
{
 LEADRasterView1->Enabled = false;
 Directions->Text= "Click does nothing";
 Toggle->Caption= "Enable";
 }
}

6.

Code the LEADRasterView1 Click procedure as the following. When the Enabled property is true, this code uses a Windows API call to generate a paint event.

void __fastcall TForm1::LEADRasterView1Click(TObject *Sender)
{
 HWND hCtl;
 RasterScaleModeConstants SavedMode;

 // Use pixels for API calls
 SavedMode = (RasterScaleModeConstants)LEADRasterView1->ScaleMode;
 LEADRasterView1->ScaleMode = 3; //Pixels
 // Use InvalidateRect to generate a paint message
 MyRect.left = 0;
 MyRect.top = 0;
 MyRect.bottom = LEADRasterView1->ScaleHeight;
 MyRect.right = LEADRasterView1->ScaleWidth;
 hCtl = (HWND)LEADRasterView1->Window_;
 InvalidateRect (hCtl, &MyRect, True);
 // Set the ScaleMode back to the default
 LEADRasterView1->ScaleMode = SavedMode;
}

7.

Code the LEADRasterView1 Paint procedure as the following. It uses Windows GDI functions to draw random lines on the control whenever there is a paint event.

void __fastcall TForm1::LEADRasterView1Paint(TObject *Sender, long hDC)
{
 HDC LeadClientDC;
int xStart;
int yStart;
int xDelta;
int yDelta;
int xEnd;
int yEnd;
short i;

srand((unsigned) time(NULL));

// Create a device context for the control that the GDI functions can access
LeadClientDC = (HDC)LEADRasterView1->GetClientDC ();
// Draw random lines on the control. This overlay does not affect the bitmap.

xStart = (int)((rand() * (int)LEADRasterView1->ScaleWidth / RAND_MAX) + 1);
yStart = (int)((rand() * (int)LEADRasterView1->ScaleHeight / RAND_MAX) + 1);
MoveToEx(LeadClientDC, xStart, yStart, NULL);
for (i = 1; i <=50; i ++)
{
 xDelta = (int)(81L * rand()/ RAND_MAX) - 40;
yDelta = (int)(81L * rand()/ RAND_MAX) - 40;
xEnd = xStart + xDelta;
yEnd = yStart + yDelta;
LineTo(LeadClientDC, xEnd, yEnd);
MoveToEx(LeadClientDC, xStart, yStart, NULL);
}
// Remove the lock from the device control
LEADRasterView1->ReleaseClientDC ();
}

8.

Run your program to test it.