2
2
2572017-10-04 03:10:52
Delphi
257, 2017-10-04 03:10:52

Why is the line not drawn from the right?

I work in Delphi 7.
I create labels, draw lines between them, but for some reason a line is not drawn to the right of the left label.

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.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kisaa, 2017-10-04
@kisaa

Would you attach a screenshot or something :) I
will assume that the line is drawn to the Label's border, which is not visible, but is. And you probably need to pull it closer to the text (number "1").
Try playing around with the Width property (maybe also AutoSize).

M
Mercury13, 2017-10-04
@Mercury13

UPD2. Remember already, drawing on the form should be done only in OnPaint. Minimize-expand the window - the line will disappear The code will be something like this.

TForm1 = class(TForm)
   ...
  private
    MyLabel,MyLabel2:TLabel;  // тут они занулятся автоматически, да и фэншуйнее
    ...
  end;

procedure TForm1.Button1Click(Sender: TObject);
begin
...
MyLabel2.Caption:='3';
Invalidate;   // Запросить перерисовку
end;

procedure TForm1.FormPaint(Sender: TObject);  // Событие OnPaint
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 there are several options for calculating your callouts.
1. Draw a number into an off-screen buffer and calculate the dimensions of the actual shaded part.
2. Get the font metric, use it to estimate the size of a typical "capital" digit. On the right, remove the pixel of the inter-letter space, plus make some correction for the “narrow” one.
UPD3. What was the matter? And that TGraphicControl (which includes TLabel) is a Delphi abstraction that has no analogue in Windows. It simply takes the TWinControl it's on, in the case of Transparent, draws the background underneath itself via WM_PAINT, and then draws itself. In this case, the line drawn NOT through OnPaint is erased.
UPD4. Full order, drawing through OnPaint - that's all that was needed. TGraphicControl is smart and does without unnecessary redraws.
And finally, the “good qualities” of your code.
MyLabel := TLabel.Create(Form1);
MyLabel.Parent := Form1;
It's better to use Self rather than Form1.
Form1: TForm1;
   p,c,z,j:integer;
If these variables were needed, it would be better to add them to Form1. If…
function rez(p:integer): integer;
   begin
   c:=p+20;

   end;
We here, obviously, calculate the dimensions of our inscriptions. Can't this be done without side effects?
UPD. This is just a layout and will glitch at high DPIs. Please, if you want this for the general public, complicate the calculations.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question