用 TLine 从 BottomLeft 到 TopRight 画一条线

Draw a line from BottomLeft To TopRight with TLine

是否可以画一条从 Bottomleft 到 Topright 或者相反的对角线。

我只能画从左上角到右下角的线。

线型设置为 ltDiagonal。这条线总是从左上角画到右下角

如果设置负宽度(bottomright在TopLeft的左边),Tline不会被绘制,因为宽度设置为0。

Tline(Control).LineType:=TLineType.ltDiagonal;

您可以使用 RotationAngle 属性。将其设置为 90,线条将从 BottomLeft 绘制到 TopRight,并且大小尺寸将交换。

这是我创建的用于处理任意两点这种情况的效用函数。 TLine 必须设置为对角线。

  //------------------------------------------------------------------
  // DrawLineBetweenPoints
  // TLine component draws a line bound by a rectangular box. The default
  // line goes low to high when set as a diagonal, so we need to do the 
  // math to make it handle the other quadrant possibilities.
  //------------------------------------------------------------------
  procedure DrawLineBetweenPoints(L: TLine; p1, p2: TPointF);
  begin
    L.LineType := TLineType.Diagonal;
    L.RotationCenter.X := 0.0;
    L.RotationCenter.Y := 0.0;
    if (p2.X >= p1.X) then begin
      // Line goes left to right, what about vertical?
      if (p2.Y > p1.Y) then begin
        // Case #1 - Line goes high to low, so NORMAL DIAGONAL
        L.RotationAngle := 0;
        L.Position.X := p1.X;
        L.Width := p2.X - p1.X;
        L.Position.Y := p1.Y;
        L.Height := p2.Y - p1.Y;
      end else begin
        // Case #2 - Line goes low to high, so REVERSE DIAGONAL
        // X and Y are now upper left corner and width and height reversed
        L.RotationAngle := -90;
        L.Position.X := p1.X;
        L.Width := p1.Y - p2.Y;
        L.Position.Y := p1.Y;
        L.Height := p2.X - p1.X;
      end;
    end else begin
      // Line goes right to left
      if (p1.Y > p2.Y) then begin
        // Case #3 - Line goes high to low (but reversed) so NORMAL DIAGONAL
        L.RotationAngle := 0;
        L.Position.X := p2.X;
        L.Width := p1.X - p2.X;
        L.Position.Y := p2.Y;
        L.Height := p1.Y - p2.Y;
      end else begin
        // Case #4 - Line goes low to high, REVERSE DIAGONAL
        // X and Y are now upper left corner and width and height reversed
        L.RotationAngle := -90;
        L.Position.X := p2.X;
        L.Width := p2.Y - p1.Y;
        L.Position.Y := p2.Y;
        L.Height := p1.X - p2.X;
      end;
    end;
    if (L.Height < 0.01) then L.Height := 0.1;
    if (L.Width < 0.01) then L.Width := 0.1;
  end;