Вопрос

I am trying to convert some Delphi code as we are re-writing a Delphi 6.0 (VCL) application in .Net. I am not sure and could not figure out the comparison between 2 Delphi Tpoints(x,y) with that of C# Point(x,y).

I am trying to draw a line between 2 points but since I have no idea how Delphi draws it, I am not able to set the C# coordinates for it.

The Delphi code is simple:

Canvas.MoveTo(x, y - 128);    
Canvas.LineTo(x, y);

I know about the C# coordinates though about 72 Points per inch and need to calculate the pixel density. But I am not sure about the Delphi PPI.

Any would be appreciated. Thanks.

Edit: If someone is wondering what TPoint I am talking when there is none in my code snippet, Canvas.MoveTo sets the PenPos property of the canvas which is of type TPoint.

Это было полезно?

Решение

I'm not sure what the exact question is that's being asked here. You have no Delphi TPoint in your code snippet; you simply have client rect logical coordinates.

The origin is at X = 0, Y = 0, which is the top left corner of the client area. Increasing X moves the position to the right, and increasing Y moves the position down. Logical units are pixels, so starting at the origin of 0, 0, a Canvas.MoveTo(10, 10) would set the new drawing position in from the left edge 10 pixels and down from the top 10 pixels, and a Canvas.LineTo(20, 20) from there would draw a line from the point at 10, 10 to 20, 20.

TCanvas.MoveTo and TCanvas.LineTo are simply wrappers around the underlying Windows GDI functions MoveToEx (with an always NULL third parameter) and LineTo.

As far as the C# equivalent, if you're referring to System.Drawing.Point, the units used are exactly the same (although I'm not sure where the origin is based by default). Given an origin of 0, 0, System.Drawing.Point(10, 10) should be the same position described above - 10 pixels from the left edge and 10 pixels down from the top edge.

A quick check confirms that the origin in a WinForms application is in fact the top left corner of the client area, using:

// Delphi code
procedure TForm3.FormPaint(Sender: TObject);
begin
  Canvas.Pen.Color := clRed;
  Canvas.MoveTo(0, 0);
  Canvas.LineTo(100, 100);
end;

// C# code
private void Form1_Paint(object sender, PaintEventArgs e)
{
    Pen newPen = new System.Drawing.Pen(Color.Red);
    e.Graphics.DrawLine(newPen, new Point(0, 0), new Point(100, 100));
}

This produces the following output:

Side by side output comparison

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top