Paint event example for Delphi
This example uses some Windows API functions to demonstrate the following:
Paint event
Click event
Enabled property
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 the Main form’s private section: |
MyRect: TRECT;
MyPoint: TPOINT;
3. |
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. |
procedure TForm1.ToggleClick(Sender: TObject);
begin
if (LEADRasterView1.Enabled = False) Then
begin
LEADRasterView1.Enabled:= True;
Directions.Text:= 'Click on the image for a splat';
Toggle.Caption:= 'Disable';
end
else
begin
LEADRasterView1.Enabled := False;
Directions.Text := 'Click does nothing';
Toggle.Caption := 'Enable';
end;
end;
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. |
procedure TForm1.LEADRasterView1Click (Sender: TObject);
var
hCtl: HWND;
SavedMode: RasterScaleModeConstants;
begin
// Use pixels for API calls
SavedMode := LEADRasterView1.ScaleMode;
LEADRasterView1.ScaleMode := 3; //Pixels
// Use InvalidateRect to generate a paint message
MyRect.Left := 0;
MyRect.Top := 0;
MyRect.Bottom := Trunc(LEADRasterView1.ScaleHeight);
MyRect.Right := Trunc(LEADRasterView1.ScaleWidth);
hCtl := LEADRasterView1.Window _;
InvalidateRect (hCtl, @MyRect, True);
// Set the ScaleMode back to the default
LEADRasterView1.ScaleMode := SavedMode;
end;
8. |
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. |
procedure TForm1.LEADRasterView1Paint (Sender: TObject; hDC: Integer);
var
LeadClientDC: Longint;
xStart: longint;
yStart: longint;
xDelta: Longint;
yDelta: Longint;
xEnd: Longint;
yEnd: Longint;
nRet: Integer;
sRet: Smallint;
i: Longint;
begin
MyPoint.x := 0;
MyPoint.y := 0;
// Create a device context for the control that the GDI functions can access
LeadClientDC := LEADRasterView1.GetClientDC (nRet);
// Draw random lines on the control. This overlay does not affect the bitmap.
xStart := Longint(Trunc((LEADRasterView1.ScaleWidth)*Random+ 1));
yStart := Longint(Trunc((LEADRasterView1.ScaleHeight)*Random+ 1));
MoveToEx(LeadClientDC, xStart, yStart, @MyPoint);
for i := 1 to 50 do
begin
xDelta := Longint(Trunc((81 * Random) - 40));
yDelta := Longint(Trunc((81 * Random) - 40));
xEnd := xStart + xDelta;
yEnd := yStart + yDelta;
LineTo(LeadClientDC, xEnd, yEnd);
MoveToEx(LeadClientDC, xStart, yStart, @MyPoint);
end;
// Remove the lock from the device control
LEADRasterView1.ReleaseClientDC (sRet);
end;
9. |
Run your program to test it. |