Why the line is drawn right?

Work in Delphi 7 .
Create labels , draw lines between them , but for some reason the right from the left label to the end of the drawn line .
unit Unit1;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls;

type
 TForm1 = class(TForm)
 Button2: TButton;
 Label1: TLabel;
 Label2: TLabel;

 procedure Button2Click(Sender: TObject);
 procedure FormCreate(Sender: TObject);

private
 { Private declarations }
public
 { Public declarations }
end;

var
 Form1: TForm1;
p,c,z,j:integer;
 function rez(p:integer): integer;
 function toside(j:integer): integer;
 var MyLabel,MyLabel2:TLabel;

implementation

{$R *.dfm}



procedure TForm1.Button2Click(Sender: TObject);


begin
rez(p);
p:=c;
toside(j);
j:=z;


MyLabel := TLabel.Create(Form1);
MyLabel.Parent := Form1;
MyLabel.Left:=300 ;
MyLabel.Top:=Mylabel.Top+c;
MyLabel.Caption:='1';


MyLabel2 := TLabel.Create(Form1);
MyLabel2.Parent := Form1;
MyLabel2.Left:=MyLabel.Left+z ;
MyLabel2.Top:=Mylabel.Top;
MyLabel2.Caption:='3';

 with Form1.Canvas do
begin
Pen.Color:=clRed;
 MoveTo(MyLabel.left,MyLabel.top+(MyLabel.Font.Size+MyLabel.Font.Size div 2) div 2);
 LineTo(MyLabel2.left,MyLabel2.top+(MyLabel2.Font.Size+MyLabel2.Font.Size div 2) div 2);
end;
end;




function rez(p:integer): integer;
begin
c:=p+20;

end;


 function toside(j:integer): integer;
begin
z:=j+38;

end;



procedure TForm1.FormCreate(Sender: TObject);
begin
j:=11;
end;






end.
June 14th 19 at 20:05
2 answers
June 14th 19 at 20:07
A screenshot would have made, or something :)
I suppose that line is drawn to the border Label'and which is not visible, but there. And you probably need to tighten it closer to the text (the number "1").
Try to play with the Width property (possibly AutoSize).
June 14th 19 at 20:09
UPD2. Remember too, drawing on the form should be done only in OnPaint. Fold-expand the window — the line will disappear, the Code will be like this.
TForm1 = class(TForm)
...
private
 MyLabel,MyLabel2:TLabel; // here they sauleda automatically, and Pensiunea
...
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
...
MyLabel2.Caption:='3';
Invalidate; // Request a redraw
end;

procedure TForm1.FormPaint(Sender: TObject); // OnPaint Event
begin
 if MyLabel <> nil then
 with Canvas do begin
Pen.Color:=clRed;
 MoveTo(MyLabel.left,MyLabel.top+(MyLabel.Font.Size+MyLabel.Font.Size div 2) div 2);
 LineTo(MyLabel2.left,MyLabel2.top+(MyLabel2.Font.Size+MyLabel2.Font.Size div 2) div 2);
end;
end;

And to calculate your leaders few options.
1. To draw the figure into an offscreen buffer and calculate the real size of the shaded part.
2. Get the font metrics, to estimate the size of a typical "capital" numbers. Right to remove the pixel character space, plus make some amendment to "narrow" one.

UPD3. What was the matter? And that TGraphicControl (which applies to TLabel) is an abstraction of Delphi, which has no equivalent in Windows. He just takes the TWinControl, which is, in the case of Transparent paints the background under a WM_PAINT through, and then draws herself. The line drawn through OnPaint, erased.

UPD4. Full order drawing using OnPaint is all that is needed. TGraphicControl smart and without unnecessary redraws.

And finally, "good quality" of your code.
MyLabel := TLabel.Create(Form1);
MyLabel.Parent := Form1;
It is better to use Self, not Form1.

Form1: TForm1;
 p,c,z,j:integer;
If these variables were needed, it would be better to post them in Form1. If...

function rez(p:integer): integer;
begin
c:=p+20;

 end;
We are here, obviously, we expect the size of our labels. And it is impossible to do it without side effects?

UPD. It's only a model and will fail at a high DPI. Please, if you want such a wide audience — complicated calculations.

Find more questions by tags Delphi