⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 teetools.pas

📁 第三方控件:PaintGrid.pas 网格型仪表控件源文件 Mymeter.pas 圆型仪表控件源文件 Project1是这两个控件的使用范例。 该
💻 PAS
📖 第 1 页 / 共 5 页
字号:
                             tmpStart.X,tmpEnd.Y,PixelsTolerance);
      if not result then
         result:=PointInLine( P,tmpEnd.X,tmpStart.Y,
                                tmpEnd.X,tmpEnd.Y,PixelsTolerance);
    end;
    end;
  end;
end;

{$IFNDEF CLR}
type
  TOwnedCollectionAccess=class(TOwnedCollection);
{$ENDIF}

function TDrawLine.GetParent: TDrawLineTool;
begin
  {$IFDEF CLR}
  result:=TDrawLineTool(Collection.Owner);
  {$ELSE}
  result:=TDrawLineTool(TOwnedCollectionAccess(Collection).GetOwner);
  {$ENDIF}
end;

function TDrawLine.GetX0: Double;
begin
  result:=StartPos.X;
end;

function TDrawLine.GetX1: Double;
begin
  result:=EndPos.X;
end;

function TDrawLine.GetY0: Double;
begin
  result:=StartPos.Y;
end;

function TDrawLine.GetY1: Double;
begin
  result:=EndPos.Y;
end;

Procedure TDrawLine.SetStyle(Value:TDrawLineStyle);
begin
  if FStyle<>Value then
  begin
    FStyle:=Value;
    Parent.Repaint;
  end;
end;

procedure TDrawLine.SetX0(const Value: Double);
begin
  Parent.SetDoubleProperty(StartPos.X,Value);
end;

procedure TDrawLine.SetX1(const Value: Double);
begin
  Parent.SetDoubleProperty(EndPos.X,Value);
end;

procedure TDrawLine.SetY0(const Value: Double);
begin
  Parent.SetDoubleProperty(StartPos.Y,Value);
end;

procedure TDrawLine.SetY1(const Value: Double);
begin
  Parent.SetDoubleProperty(EndPos.Y,Value);
end;

function TDrawLine.IsPenStored: Boolean;
begin
  result:=Assigned(FPen);
end;

function TDrawLine.GetPen:TChartPen;
begin
  if not Assigned(FPen) then
  begin
    // LCL / FPC bug, workaround

    FPen:=TChartPen.Create({$IFDEF LCL}nil{$ELSE}Parent.CanvasChanged{$ENDIF});
    
    FPen.Assign(Parent.Pen);
  end;

  result:=FPen;
end;

procedure TDrawLine.SetPen(const Value: TChartPen);
begin
  if Assigned(Value) then Pen.Assign(Value)
  else
  begin
    FreeAndNil(FPen);
    Parent.Repaint;
  end;
end;

{ TDrawLineTool }
Constructor TDrawLineTool.Create(AOwner: TComponent);
begin
  inherited;
  FButton:=mbLeft;
  FLines:=TDrawLines.Create(Self,TDrawLine);
  FEnableDraw:=True;
  FEnableSelect:=True;
  FSelectNew:=True;
  ISelected:=nil;
  IHandle:=chNone;
end;

Destructor TDrawLineTool.Destroy;
begin
  ISelected:=nil;
  FLines.Free;
  inherited;
end;

Procedure TDrawLineTool.DrawLine( Const StartPos,EndPos:TPoint;
                                  AStyle:TDrawLineStyle);
begin
  With ParentChart.Canvas do
  if ParentChart.View3D then
  begin
    Case AStyle of
          dlLine: begin
                    MoveTo3D(StartPos.X,StartPos.Y,0);
                    LineTo3D(EndPos.X,EndPos.Y,0);
                  end;
 dlHorizParallel: begin
                    HorizLine3D(StartPos.X,EndPos.X,StartPos.Y,0);
                    HorizLine3D(StartPos.X,EndPos.X,EndPos.Y,0);
                  end;
    else
      begin
        VertLine3D(StartPos.X,StartPos.Y,EndPos.Y,0);
        VertLine3D(EndPos.X,StartPos.Y,EndPos.Y,0);
      end
    end;
  end
  else
  Case AStyle of
          dlLine: Line(StartPos.X,StartPos.Y,EndPos.X,EndPos.Y);
 dlHorizParallel: begin
                    DoHorizLine(StartPos.X,EndPos.X,StartPos.Y);
                    DoHorizLine(StartPos.X,EndPos.X,EndPos.Y);
                  end;
  else
    begin
      DoVertLine(StartPos.X,StartPos.Y,EndPos.Y);
      DoVertLine(EndPos.X,StartPos.Y,EndPos.Y);
    end
  end;
end;

procedure TDrawLineTool.ChartEvent(AEvent: TChartToolEvent);
var t : Integer;
begin
  inherited;

  if (AEvent=cteAfterDraw) and (Lines.Count>0) then
  begin
    with ParentChart do
    begin
      Canvas.BackMode:=cbmTransparent;
      ClipDrawingRegion;
    end;

    for t:=0 to Lines.Count-1 do
    With Lines[t] do
    begin
      if Assigned(FPen) then
         ParentChart.Canvas.AssignVisiblePen(FPen)
      else
         ParentChart.Canvas.AssignVisiblePen(Self.Pen);

      DrawLine(AxisPoint(StartPos),AxisPoint(EndPos),Style);
    end;

    if Assigned(ISelected) then
       ISelected.DrawHandles;

    ParentChart.Canvas.UnClipRectangle;
  end;
end;

Procedure TDrawLineTool.ClipDrawingRegion;
var R : TRect;
begin
  if Assigned(Series) then
  with Series do
     R:=TeeRect(GetHorizAxis.IStartPos,GetVertAxis.IStartPos,
                GetHorizAxis.IEndPos,GetVertAxis.IEndPos)
  else
     R:=ParentChart.ChartRect;

  with ParentChart do
  if CanClip then
     Canvas.ClipCube(R,0,Width3D);
end;

Function TDrawLineTool.InternalClicked(X,Y:Integer; AHandle:TDrawLineHandle):TDrawLine;
begin
  result:=InternalClicked(X,Y,AHandle,ClickTolerance);
end;

Function TDrawLineTool.InternalClicked(X,Y:Integer; AHandle:TDrawLineHandle; ClickTolerance:Integer):TDrawLine;
var t : Integer;
begin
  result:=nil;

  // convert from 3D to 2D...
  ParentChart.Canvas.Calculate2DPosition(X,Y,0);

  // first, find if clicked the selected line...
  if Assigned(ISelected) and ISelected.Clicked(X,Y,AHandle,ClickTolerance) then
  begin
    result:=ISelected;
    exit;
  end;

  // find which clicked line...
  for t:=0 to Lines.Count-1 do
      if Lines[t].Clicked(X,Y,AHandle,ClickTolerance) then
      begin
        result:=Lines[t];
        break;
      end;
end;

Function TDrawLineTool.Clicked(X,Y:Integer):TDrawLine;
begin
  result:=Clicked(X,Y,ClickTolerance);
end;

Function TDrawLineTool.Clicked(X,Y:Integer; ClickTolerance:Integer):TDrawLine;
begin
  // try first with whole line...
  result:=InternalClicked(X,Y,chNone,ClickTolerance);

  // try then at start and end line points...
  if not Assigned(result) then
  begin
     result:=InternalClicked(X,Y,chStart,ClickTolerance);

    if not Assigned(result) then
       result:=InternalClicked(X,Y,chEnd,ClickTolerance);
  end;
end;

Procedure TDrawLineTool.ChartMouseEvent( AEvent: TChartMouseEvent;
                                       AButton:TMouseButton;
                                       AShift: TShiftState; X, Y: Integer);

  { returns True if the mouse is over selected line ending points or over
    any non-selected line.
    If True, the Chart cursor is changed.
  }
  Function CheckCursor:Boolean;
  begin
    if Assigned(ISelected) and
       ((InternalClicked(X,Y,chStart)=ISelected) or
        (InternalClicked(X,Y,chEnd)=ISelected)) then { ending points }
    begin
      ParentChart.Cursor:=crCross;
      result:=True;
    end
    else
    if Assigned(Clicked(X,Y)) then { over a line }
    begin
      ParentChart.Cursor:=crHandPoint;  // set cursor
      result:=True;
    end
    else
      result:=False;
  end;

  procedure SetSelection(const ALine:TDrawLine); // 7.05
  begin
    FromPoint:=AxisPoint(ALine.StartPos);
    ToPoint:=AxisPoint(ALine.EndPos);
    IPoint:=TeePoint(X,Y);
  end;

var tmpLine  : TDrawLine;
    tmpAllow : Boolean;
begin
  Case AEvent of
    cmeDown: if AButton=FButton then
             begin
               if FEnableSelect then tmpLine:=Clicked(X,Y)
                                else tmpLine:=nil;

               if Assigned(tmpLine) then { clicked line, do select }
               begin
                 SetSelection(tmpLine);
                 IHandle:=chSeries;

                 if tmpLine<>ISelected then
                 begin
                   tmpAllow:=True;
                   if Assigned(FOnSelecting) then
                      FOnSelecting(Self,tmpLine,tmpAllow); { 5.03 }

                   if tmpAllow then
                   begin
                     { change selected line }
                     if Assigned(ISelected) then
                     begin
                       ISelected:=tmpLine;
                       Repaint;
                     end
                     else
                     begin
                       ISelected:=tmpLine;
                       ISelected.DrawHandles;
                     end;

                     { call event }
                     if Assigned(FOnSelect) then FOnSelect(Self);
                   end;
                 end
                 else
                 begin
                   { check if clicked on ending points }
                   if Assigned(InternalClicked(X,Y,chStart)) then
                      IHandle:=chStart
                   else
                   if Assigned(InternalClicked(X,Y,chEnd)) then
                      IHandle:=chEnd;
                 end;

                 ParentChart.CancelMouse:=True;
               end
               else
               begin
                 { un-select }
                 ISelected:=nil;

                 if EnableDraw then
                 begin
                   IDrawing:=True;
                   FromPoint:=TeePoint(X,Y);
                   ToPoint:=FromPoint;
                   RedrawLine(ISelected);
                   ParentChart.CancelMouse:=True;
                 end;
               end;
             end;
    cmeMove: if IDrawing or (IHandle<>chNone) then // drawing or dragging
             begin
               RedrawLine(ISelected); // hide previous line

               if IDrawing then ToPoint:=TeePoint(X,Y)
               else
               begin
                 if IHandle=chStart then FromPoint:=TeePoint(X,Y)
                 else
                 if IHandle=chEnd then ToPoint:=TeePoint(X,Y)
                 else
                 if IHandle=chSeries then
                 begin
                   Inc(FromPoint.X,X-IPoint.X);
                   Inc(FromPoint.Y,Y-IPoint.Y);
                   Inc(ToPoint.X,X-IPoint.X);
                   Inc(ToPoint.Y,Y-IPoint.Y);
                   IPoint:=TeePoint(X,Y);
                 end;
               end;

               RedrawLine(ISelected);  // show line at new position
               ParentChart.CancelMouse:=True;

               IMoved:=True;

               // call event
               if Assigned(FOnDragLine) then FOnDragLine(Self);
             end
             else
             if FEnableSelect then { change the cursor if mouse is over lines }
                ParentChart.CancelMouse:=CheckCursor;

      cmeUp: if AButton=FButton then
             begin
               // if dragging...
               if (IHandle<>chNone) and Assigned(ISelected) then // 7.02
               begin
                 if (IHandle=chStart) or (IHandle=chSeries) then
                    ISelected.StartPos:=ScreenPoint(FromPoint);

                 if (IHandle=chEnd) or (IHandle=chSeries) then
                    ISelected.EndPos:=ScreenPoint(ToPoint);

                 IHandle:=chNone;

                 // stop dragging, repaint
                 Repaint;

                 // call event
                 if IMoved then // TVC52010342
                 begin
                   IMoved:=False;

                   if Assigned(FOnDraggedLine) then
                      FOnDraggedLine(Self); { 5.01 }
                 end;
               end
               else
               if IDrawing then  // if drawing a new line...
               begin
                 if (FromPoint.X<>ToPoint.X) or (FromPoint.Y<>ToPoint.Y) then
                 begin
                   // create the new drawn line...
                   tmpLine:=TeeDrawLineClass.Create(Lines);
           

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -