Draw a rectangle with canvas moving with the mouse. Delphi/Windows SDK/API
Http://www.delphi2007.net/DelphiMultimedia/html/delphi_20061107153346171.html
I want to draw a rectangle from the start point to the end point on the form with the canvas moving with the mouse. I recorded the start point in mousedown, and then drew a rectangle from the start point to the position where the mouse is located in mousemove. However, I found that I drew a rectangle with multiple layers, now I only want to draw a rectangle from the starting point to the place where the mouse is located, and it changes as the mouse moves. What should I do? How can I erase unnecessary Rectangles?
Top
No one answered?
Set a timer, such as 100 ms
Draw a picture in ontimer to clear the screen before the rectangle
Unit unit1;
Interface
Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs;
Type
Tform1 = Class (tform)
Procedure formmousedown (Sender: tobject; button: tmousebutton;
Shift: tshiftstate; X, Y: integer );
Procedure formmousemove (Sender: tobject; shift: tshiftstate; X,
Y: integer );
Private
{Private Declarations}
Fopt,
Fcpt: tpoint;
Procedure drawrectangle (aopt, acpt: tpoint );
Public
{Public declarations}
End;
VaR
Form1: tform1;
Implementation
{$ R *. DFM}
Procedure tform1.drawrectangle (aopt, acpt: tpoint );
Begin
With canvas do
Begin
// Left vertical
MoveTo (aopt. X, aopt. y );
Lineto (aopt. X, acpt. y );
// Right vertical
MoveTo (acpt. X, aopt. y );
Lineto (acpt. X, acpt. y );
// Water
MoveTo (aopt. X, aopt. y );
Lineto (acpt. X, aopt. y );
MoveTo (aopt. X, acpt. y );
Lineto (acpt. X, acpt. y );
End;
End;
Procedure tform1.formmousedown (Sender: tobject; button: tmousebutton;
Shift: tshiftstate; X, Y: integer );
Begin
With canvas do
Begin
If shift = [ssleft] Then
Begin
Canvas. Pen. Mode: = pmnot;
Canvas. Pen. Color: = clred;
Fopt: = point (x, y );
Fcpt: = point (x, y );
MoveTo (x, y );
Lineto (x, y)
End;
End;
End;
Procedure tform1.formmousemove (Sender: tobject; shift: tshiftstate; X,
Y: integer );
Begin
With canvas do
Begin
If shift = [ssleft] Then
Begin
Drawrectangle (fopt, fcpt );
Fcpt: = point (x, y );
Drawrectangle (fopt, fcpt );
Exit;
MoveTo (fopt. X, fopt. y );
Lineto (fcpt. X, fcpt. y );
Fcpt: = point (x, y );
MoveTo (fopt. X, fopt. y );
Lineto (fcpt. X, fcpt. Y)
End;
End;
End;
End.
the vector graphics component tcad is recommended
http://www.codeidea.com/cn/