博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
delphi 画 带箭头的线
阅读量:5068 次
发布时间:2019-06-12

本文共 5580 字,大约阅读时间需要 18 分钟。

unit   Unit1;interfaceuses    Windows,   Messages,   SysUtils,   Variants,   Classes,   Graphics,   Controls,   Forms,    Dialogs;const    Penwidth   =   1;//画笔的粗细    Len   =   20;//箭头线的长度    {说明:这两个常量应该一起变化,具体值由效果来定。    当Penwidth很小时,显示的效果不是太好}type    TForm1   =   class(TForm)        procedure   FormMouseUp(Sender:   TObject;   Button:   TMouseButton;            Shift:   TShiftState;   X,   Y:   Integer);        procedure   FormMouseDown(Sender:   TObject;   Button:   TMouseButton;            Shift:   TShiftState;   X,   Y:   Integer);        procedure   FormMouseMove(Sender:   TObject;   Shift:   TShiftState;   X,            Y:   Integer);        procedure   FormShow(Sender:   TObject);        procedure   FormCreate(Sender:   TObject);    private        {   Private   declarations   }    public        {   Public   declarations   }    end;var    Form1:   TForm1;    xs,   ys:   integer;//画线开始处的坐标    xt,   yt:   integer;//记录鼠标前一时刻的坐标    xl,   yl:   integer;//记录第一条箭头线的端点坐标    xr,   yr:   integer;//记录第二条箭头线的端点坐标    B:   boolean;//判断是否已经开始画线implementation{$R   *.dfm}procedure   TForm1.FormMouseUp(Sender:   TObject;   Button:   TMouseButton;    Shift:   TShiftState;   X,   Y:   Integer);begin    {画线结尾时,将线重新填充一遍,以免有部分空白}    if   not   ((x   =   xs)   and   (y   =   ys))   then    begin        Form1.Canvas.Pen.Mode   :=   pmCopy;        Form1.Canvas.Pen.Color   :=   clRed;        Form1.Canvas.Pen.Width   :=   PenWidth;        Form1.Canvas.MoveTo(xs,   ys);        Form1.Canvas.LineTo(x,   y);        Form1.Canvas.MoveTo(x,   y);        Form1.Canvas.LineTo(xl,   yl);        Form1.Canvas.MoveTo(x,   y);        Form1.Canvas.LineTo(xr,   yr);    end;    B   :=   False;end;procedure   TForm1.FormMouseDown(Sender:   TObject;   Button:   TMouseButton;    Shift:   TShiftState;   X,   Y:   Integer);begin    xs   :=   x;    ys   :=   y;    xt   :=   x;    yt   :=   y;    xl   :=   -1;    yl   :=   -1;    xr   :=   -1;    yr   :=   -1;    B   :=   True;end;procedure   TForm1.FormMouseMove(Sender:   TObject;   Shift:   TShiftState;   X,    Y:   Integer);begin    if   B   then    begin        Form1.Canvas.Pen.Mode   :=   pmNotXor;        Form1.Canvas.Pen.Color   :=   clRed;        Form1.Canvas.Pen.Width   :=   PenWidth;        //绘旧线        Form1.Canvas.MoveTo(xs,   ys);        Form1.Canvas.LineTo(xt,   yt);        //绘新线        Form1.Canvas.MoveTo(xs,   ys);        Form1.Canvas.LineTo(x,   y);        if   xl   <>   -1   then        begin            Form1.Canvas.MoveTo(xt,   yt);            Form1.Canvas.LineTo(xl,   yl);            Form1.Canvas.MoveTo(xt,   yt);            Form1.Canvas.LineTo(xr,   yr);            Form1.Canvas.MoveTo(xl,   yl);            Form1.Canvas.LineTo(xr,   yr);        end;        //记录下原坐标        xt   :=   x;        yt   :=   y;        if   x   >   xs   then        begin            xl   :=   trunc(x   -   Len   *   Cos(ArcTan((y   -   ys)   /   (x   -   xs))   -   Pi   /   6));            yl   :=   trunc(y   -   Len   *   Sin(ArcTan((y   -   ys)   /   (x   -   xs))   -   Pi   /   6));            xr   :=   trunc(x   -   Len   *   Cos(ArcTan((y   -   ys)   /   (x   -   xs))   +   Pi   /   6));            yr   :=   trunc(y   -   Len   *   Sin(ArcTan((y   -   ys)   /   (x   -   xs))   +   Pi   /   6));        end        else            if   x   <   xs   then            begin                xl   :=   trunc(x   +   Len   *   Cos(ArcTan((y   -   ys)   /   (x   -   xs))   -   Pi   /   6));                yl   :=   trunc(y   +   Len   *   Sin(ArcTan((y   -   ys)   /   (x   -   xs))   -   Pi   /   6));                xr   :=   trunc(x   +   Len   *   Cos(ArcTan((y   -   ys)   /   (x   -   xs))   +   Pi   /   6));                yr   :=   trunc(y   +   Len   *   Sin(ArcTan((y   -   ys)   /   (x   -   xs))   +   Pi   /   6));            end            else                if   y   <   ys   then                begin                    xl   :=   trunc(x   -   Len   *   Sin(Pi   /   6));                    yl   :=   trunc(y   +   Len   *   Cos(Pi   /   6));                    xr   :=   trunc(x   +   Len   *   Sin(Pi   /   6));                    yr   :=   trunc(y   +   Len   *   Cos(Pi   /   6));                end                else                    if   y   >   ys   then                    begin                        xl   :=   trunc(x   -   Len   *   Sin(Pi   /   6));                        yl   :=   trunc(y   -   Len   *   Cos(Pi   /   6));                        xr   :=   trunc(x   +   Len   *   Sin(Pi   /   6));                        yr   :=   trunc(y   -   Len   *   Cos(Pi   /   6));                    end                    else                    begin                        xl   :=   -1;                        yl   :=   -1;                        xr   :=   -1;                        yr   :=   -1;                    end;        if   xl   <>   -1   then        begin            Form1.Canvas.MoveTo(x,   y);            Form1.Canvas.LineTo(xl,   yl);            Form1.Canvas.MoveTo(x,   y);            Form1.Canvas.LineTo(xr,   yr);            Form1.Canvas.MoveTo(xl,   yl);            Form1.Canvas.LineTo(xr,   yr);        end;    end;end;procedure   TForm1.FormShow(Sender:   TObject);begin    Form1.Color   :=   clWhite;    Form1.Caption   :=   '画带箭头的直线 ';    Form1.WindowState   :=   wsMaximized;    B   :=   False;    xt   :=   -1;    yt   :=   -1;    xl   :=   -1;    yl   :=   -1;    xr   :=   -1;    yr   :=   -1;end;procedure   TForm1.FormCreate(Sender:   TObject);begin    Form1.BorderIcons   :=   [biSystemMenu];end;end.

 

转载于:https://www.cnblogs.com/jxsoft/archive/2011/12/12/2284937.html

你可能感兴趣的文章
Spring MVC 入门(二)
查看>>
Java处理多人同时读写文件的文件锁处理
查看>>
设计模式IOS篇-第二章:委托模式
查看>>
beego——日志处理
查看>>
【连载】 FPGA Verilog HDL 系列实例--------十进制加减法计数器
查看>>
MySQL中MyISAM与InnoDB区别及选择
查看>>
DataGrid 上修改數據
查看>>
nginx php-fpm安装配置(转)
查看>>
重读The C programming Lanuage 笔记一:类型转换
查看>>
复杂类型的属性注入
查看>>
回家最好最快路线
查看>>
mysql面试题
查看>>
格式化输出数字和时间
查看>>
页面中公用的全选按钮,单选按钮组件的编写
查看>>
判断文本框输入的文字长度
查看>>
java笔记--用ThreadLocal管理线程,Callable<V>接口实现有返回值的线程
查看>>
Scaling Pinterest - From 0 To 10s Of Billions Of Page Views A Month In Two Years
查看>>
SelectSort 选择排序
查看>>
关于android 加载https网页的问题
查看>>
BZOJ 1047 HAOI2007 理想的正方形 单调队列
查看>>