Draw a rectangle with canvas moving with the mouse

Source: Internet
Author: User
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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.