delphi chart画曲线,手绘模式实现

zhaoyuan1216 2013-08-29 02:44:56
在chart上,用户点击chart的某一点,描点,最后连接点成曲线
1.获取鼠标点击的坐标
2.描点
3.连接成曲线
实现手绘生成曲线
...全文
422 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
simonhehe 2013-08-30
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TeEngine, Series, ExtCtrls, TeeProcs, Chart, Generics.Collections;

type
  TForm1 = class(TForm)
    cht1: TChart;
    fstlnsrsSeries1: TFastLineSeries;
    procedure cht1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure cht1AfterDraw(Sender: TObject);
  private
    { Private declarations }
    FLst : TList<TPoint>;   //存储坐标点
    procedure PrintLine(const nStep: Integer);    //划线   nStep:划线步长
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  FLst.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FLst := TList<TPoint>.Create;
end;

procedure TForm1.PrintLine(const nStep : Integer);
var
  i : integer;
  pPoint : TPoint;
begin
  if nStep < 2 then
    Exit;//raise Exception.Create('错误的步长值!');

  if FLst.Count < nStep then
    exit;

  cht1.Canvas.Pen.Color := clRed;
  i := FLst.Count - nStep;
  while i < FLst.Count - 1 do
  begin
    pPoint := FLst.Items[i];
    cht1.Canvas.MoveTo(pPoint.X, pPoint.Y);
    inc(i);

    pPoint := FLst.Items[i];
    cht1.Canvas.LineTo(pPoint.X, pPoint.Y);
  end;
end;

procedure TForm1.cht1AfterDraw(Sender: TObject);
begin
  PrintLine(FLst.Count);
end;

procedure TForm1.cht1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  pPoint : TPoint;
begin
  //按住Ctrl点击鼠标左键, 记录坐标点
  if (ssCtrl in shift) and (Button = mbLeft) then
  begin
    pPoint.X := x;
    pPoint.Y := y;
    FLst.Add(pPoint);

    PrintLine(2);
  end;
end;

end.

828

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧