Recent

Author Topic: [SOLVED] Draw vertical line  (Read 6254 times)

tudi_x

  • Hero Member
  • *****
  • Posts: 532
[SOLVED] Draw vertical line
« on: December 11, 2017, 11:31:54 am »
hi All,
i need to draw some vertical lines on a form / panel (between a tab control and list box). i am currently using the below to draw an horizontal line.
please advise if i could use BGRA to draw also a vertical line or what other options i would have (cross platform).

thank you

Code: Pascal  [Select][+][-]
  1. uses BGRAShape;
  2. constructor THLine.Create(AOwner: TWinControl; AlignLeft: TControl; ALength: word; AColor: TColor = clBlack);
  3. begin
  4.   inherited Create(AOwner);
  5.  
  6.   self.UseRatioXY := True;
  7.   self.RatioXY := 1000;
  8.   self.ShapeType := stRegularPolygon;
  9.   self.BorderColor := AColor;
  10.   self.BorderWidth := 3;
  11.   self.Width := ALength;
  12.   self.Parent := AOwner;
  13. end;
« Last Edit: December 11, 2017, 04:46:37 pm by tudi_x »
Lazarus 2.0.2 64b on Debian LXDE 10

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Draw vertical line (TBGRAShape)
« Reply #1 on: December 11, 2017, 12:41:39 pm »
guess i would be using TShape:

Code: Pascal  [Select][+][-]
  1. type
  2.   {vertical line}
  3.   TVLine = class(TShape)
  4.   public
  5.     constructor Create(AOwner: TWinControl; ALength: word; AColor: TColor = clBlack); reintroduce;
  6.   end;
  7.  
  8. constructor TVLine.Create(AOwner: TWinControl; ALength: word; AColor: TColor = clBlack);
  9. begin
  10.   inherited Create(AOwner);
  11.  
  12.   self.Brush.Color:= AColor;
  13.   self.Width:= 3;
  14.   self.Height:= ALength;    
  15.   self.Parent := AOwner;
  16. end;
« Last Edit: December 11, 2017, 01:44:21 pm by tudi_x »
Lazarus 2.0.2 64b on Debian LXDE 10

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [SOLVED] Draw vertical line (TBGRAShape)
« Reply #2 on: December 11, 2017, 02:15:12 pm »
i need to draw some vertical lines on a form / panel

Does direct drawing on the form/panel's Canvas using Canvas.Line() not work for you?

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: [SOLVED] Draw vertical line (TBGRAShape)
« Reply #3 on: December 11, 2017, 02:27:33 pm »
i do not know how to draw on TPanel as this component does not expose the Canvas.Line().
« Last Edit: December 11, 2017, 02:29:18 pm by tudi_x »
Lazarus 2.0.2 64b on Debian LXDE 10

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: [SOLVED] Draw vertical line (TBGRAShape)
« Reply #4 on: December 11, 2017, 03:08:27 pm »
Huh ?

TPanel -> TCustomPanel -> TCustomControl . Canvas = TCanvas -> TFPCustomCanvas . Line.

Are you perhaps missing sources to FPC (or not configured correctly inside Lazarus where these are located) ? In which case Lazarus code editor is probably not able to show you methods from FPC classes.

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: [SOLVED] Draw vertical line (TBGRAShape)
« Reply #5 on: December 11, 2017, 03:24:27 pm »
i am using Lazarus 1.8 64b on Win7 as downloaded in the Lazarus archive (did not download the FPC archive).
my TCustomPanel looks like the below in 'c:\lazarus18\lcl\extctrls.pp'.
could you please point me in the right direction? do i need to create a new panel class based on the FPC code?


Code: Pascal  [Select][+][-]
  1. TCustomPanel = class(TCustomControl)
  2.   private
  3.     FBevelColor : TColor;
  4.     FBevelInner, FBevelOuter : TPanelBevel;
  5.     FBevelWidth : TBevelWidth;
  6.     FAlignment : TAlignment;
  7.     FFullRepaint: Boolean;
  8.     FWordWrap: Boolean;
  9.     procedure PaintBevel(var ARect: TRect; ABevel: TPanelBevel);
  10.     procedure SetAlignment(const Value : TAlignment);
  11.     procedure SetBevelColor(AValue: TColor);
  12.     procedure SetBevelInner(const Value: TPanelBevel);
  13.     procedure SetBevelOuter(const Value: TPanelBevel);
  14.     procedure SetBevelWidth(const Value: TBevelWidth);
  15.     procedure SetWordwrap(const Value: Boolean);
  16.   protected
  17.     class procedure WSRegisterClass; override;
  18.     procedure AdjustClientRect(var aRect: TRect); override;
  19.     class function GetControlClassDefaultSize: TSize; override;
  20.     procedure CMParentColorChanged(var Message: TLMessage); message CM_PARENTCOLORCHANGED;
  21.     function GetDefaultDockCaption: String; override;
  22.     procedure Loaded; override;
  23.     procedure RealSetText(const Value: TCaption); override;
  24.     procedure Paint; override;
  25.     procedure UpdateParentColorChange;
  26.     property WordWrap: Boolean read FWordwrap write SetWordwrap default false;
  27.   public
  28.     constructor Create(TheOwner: TComponent); override;
  29.     property Align default alNone;
  30.     property Alignment: TAlignment read FAlignment write SetAlignment default taCenter;
  31.     property BevelColor: TColor read FBevelColor write SetBevelColor default clDefault;
  32.     property BevelInner: TPanelBevel read FBevelInner write SetBevelInner default bvNone;
  33.     property BevelOuter: TPanelBevel read FBevelOuter write SetBevelOuter default bvRaised;
  34.     property BevelWidth: TBevelWidth read FBevelWidth write SetBevelWidth default 1;
  35.     property Color default {$ifdef UseCLDefault}clDefault{$else}clBtnFace{$endif};
  36.     property FullRepaint: Boolean read FFullRepaint write FFullRepaint default True; // exists only for Delphi compatibility, has no effect in LCL
  37.     property ParentColor default true;
  38.     property TabStop default False;
  39.   end;  
Lazarus 2.0.2 64b on Debian LXDE 10

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: [SOLVED] Draw vertical line (TBGRAShape)
« Reply #6 on: December 11, 2017, 03:27:28 pm »
One class further down the road, my dear  :) (or up depending on how you look at things).

You need to look at TCustomControl for Canvas property.

lainz

  • Hero Member
  • *****
  • Posts: 4449
    • https://lainz.github.io/
Re: [SOLVED] Draw vertical line (TBGRAShape)
« Reply #7 on: December 11, 2017, 03:36:29 pm »
I found this topic not as precise as I need =)

Why in the topic you put TBGRAShape? You need an OnPaint event for TBGRAShape?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: [SOLVED] Draw vertical line (TBGRAShape)
« Reply #8 on: December 11, 2017, 03:39:45 pm »
Just for the record @tudi_x: i was talking about using plain canvas to draw your line. So in case you are still using bgra control you might want to change that. The path to canvas property or drawing a line might be different for bgra controls.

lainz

  • Hero Member
  • *****
  • Posts: 4449
    • https://lainz.github.io/
Re: [SOLVED] Draw vertical line (TBGRAShape)
« Reply #9 on: December 11, 2017, 03:40:57 pm »
i do not know how to draw on TPanel as this component does not expose the Canvas.Line().

Use the OnPaint event of TPanel, and with BGRA you can use TBGRAVirtualScreen OnRedraw. That in a regular panel...

If you want to make your own control, just inherit from TCustomPanel and override the Paint method. It if you want to reuse your drawing in many places..
« Last Edit: December 11, 2017, 03:45:18 pm by lainz »

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Draw vertical line
« Reply #10 on: December 11, 2017, 04:13:04 pm »
guys, i am a beginner. could you please advise what is wrong with using the TShape code i posted?
what are the benefits of creating a new panel component if i can use TShape?

i placed BGRA initially as currently i am using BGRA to draw horizontal line, i thought i could switch it 90 degrees to be vertical.

thank you
Lazarus 2.0.2 64b on Debian LXDE 10

lainz

  • Hero Member
  • *****
  • Posts: 4449
    • https://lainz.github.io/
Re: Draw vertical line
« Reply #11 on: December 11, 2017, 04:29:19 pm »
guys, i am a beginner. could you please advise what is wrong with using the TShape code i posted?
what are the benefits of creating a new panel component if i can use TShape?

i placed BGRA initially as currently i am using BGRA to draw horizontal line, i thought i could switch it 90 degrees to be vertical.

thank you

Ok. Well you're mixing things. TShape does not uses BGRAShape unit.

hi All,
i need to draw some vertical lines on a form / panel (between a tab control and list box). i am currently using the below to draw an horizontal line.
please advise if i could use BGRA to draw also a vertical line or what other options i would have (cross platform).

thank you

If you need to draw vertical lines on a form  / panel the best thing you can do is to use the event OnPaint.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormPaint(Sender: TObject);
  2. var
  3.   s: TForm;
  4. begin
  5.   s := TForm(Sender);
  6.   s.Canvas.Line(s.Width div 2, 0, s.Width div 2, s.Height); // x1, y1, x2, y2
  7. end;
  8.  
  9. procedure TForm1.Panel1Paint(Sender: TObject);
  10. var
  11.   p: TPanel;
  12. begin
  13.   p := TPanel(Sender);
  14.   p.Canvas.Line(p.Width div 2, 0, p.Width div 2, p.Height); // x1, y1, x2, y2
  15. end;  

If you want to rotate the line just change x1, y1, x2, y2 values.
« Last Edit: December 11, 2017, 04:34:38 pm by lainz »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Draw vertical line
« Reply #12 on: December 11, 2017, 04:37:59 pm »
guys, i am a beginner.
afaik it seems to be a mixup in the English language.

Quote
could you please advise what is wrong with using the TShape code i posted?
There is absolutely nothing wrong with using a TShape to draw/show a line. If it works for you then perfect  :D

Quote
what are the benefits of creating a new panel component if i can use TShape?
Depending on your requirements it offers more flexibility, such as drawing horizontal, cross, or vertical lines (in fact you can draw anything what your heart desires).

DO note that it is not required to create a new component. As Lainz already wrote you can use the OnPaint method of TPanel and draw your line (or whatever shape you wish).

However, when howardpc asked:
Does direct drawing on the form/panel's Canvas using Canvas.Line() not work for you?

You replied with:
i do not know how to draw on TPanel as this component does not expose the Canvas.Line().

And that is imho a misunderstanding from your side.

I showed you the 'path' to reach Canvas.Line method. Something like MyPanel1.Canvas.Line(1,1,200,200) should do the trick. Your claim that Canvas.Line is not exposed is therefor wrong. Nothing wrong with that, but that is what i tried to explain.  :)

However, it is better to use the OnPaint event to do your drawing, which makes the lien more permanent. That does not require you to create a new component. Just for you to add the OnPaint Event in the object inspector, and start drawing in code ;)
« Last Edit: December 11, 2017, 04:40:57 pm by molly »

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Draw vertical line
« Reply #13 on: December 11, 2017, 04:46:23 pm »
thank you very much for explaining it on my level.
now i think i got it.
Lazarus 2.0.2 64b on Debian LXDE 10

 

TinyPortal © 2005-2018