Recent

Author Topic: How to add a border to a Label  (Read 713 times)

seany

  • Jr. Member
  • **
  • Posts: 99
How to add a border to a Label
« on: February 11, 2023, 11:23:39 am »
Hi all

I posted a lot about menu renders using Linux/QT5

I got sidetracked, and decided to make my own Menu Class. I posted my first steps here: https://forum.lazarus.freepascal.org/index.php/topic,62257.0.html

I wonder, how can I add a border to labels programatically? If I create a tlabel on form designer, and look with object inspector, I can't find an item that seems to relate to a border. So, some advice will be appreciated. Tahnks.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1405
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How to add a border to a Label
« Reply #1 on: February 11, 2023, 12:09:35 pm »
Use TStaticText and choose a BorderStyle.
Use a TPanel, put a TLabel in, remove TPanel caption, set TPanel AutoSize = True.

Does one of above fit your needs?
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1405
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How to add a border to a Label
« Reply #2 on: February 11, 2023, 12:22:27 pm »
And if it really just needs to be one single TLabel and your desire is that big to just draw a border around, you can override the Forms OnPaint with something like this:
Code: Pascal  [Select][+][-]
  1. Label2.Canvas.Rectangle(0, 0, Label2.Width, Label2.Height);
In attachment you see all three variations on Windows with native widgetset.

Label1 is the Panel version
Label2 is the OnPaint version
and static is static :D
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1405
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How to add a border to a Label
« Reply #3 on: February 11, 2023, 12:42:08 pm »
TShape would also be a possibility.
Place one on form and set for it this OnPaint handler:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Shape1Paint(Sender: TObject);
  2. var
  3.   lbl: string;
  4. begin
  5.   lbl := 'TShape';
  6.   Shape1.Canvas.Font.Color := clBlack;
  7.   Shape1.Canvas.Pen.Color := clDefault;
  8.   Shape1.Canvas.TextOut(3, 1, lbl);
  9.   Shape1.Width := Shape1.Canvas.TextWidth(lbl) + 6;
  10.   Shape1.Height := Shape1.Canvas.TextHeight(lbl) + 3;
  11. end;
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

lainz

  • Hero Member
  • *****
  • Posts: 4151
    • https://lainz.github.io/
Re: How to add a border to a Label
« Reply #4 on: February 11, 2023, 04:24:22 pm »
Hi, you can use BGRAControls TBCLabel.

It supports border, and also gradient background.

seany

  • Jr. Member
  • **
  • Posts: 99
Re: How to add a border to a Label
« Reply #5 on: February 12, 2023, 11:46:37 am »
Hi
That's a great option... I would like to find a tutorial on BGRA controls please, and can it support border radius? thank you

jamie

  • Hero Member
  • *****
  • Posts: 5362
Re: How to add a border to a Label
« Reply #6 on: February 12, 2023, 03:30:42 pm »
just drop a TPanel on there and use that as the label, it has caption and boarder control.
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018