Forum > Beginners

Adding border to component

<< < (2/2)

molly:
Well, in case using standard LCL controls and a TForm like component to place them on then you could perhaps use something like this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;  // click event assigned to ControlClick    Edit1: TEdit; // click event assigned to ControlClick    procedure ControlClick(Sender: TObject);  private    { private declarations }  public    { public declarations }  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.ControlClick(Sender: TObject);const  FrameWidth = 2;Var  Rect : classes.TRect;begin  Self.Refresh;  With Sender as TControl do  begin    Rect.Top    := Top    - FrameWidth;    Rect.Left   := Left   - FrameWidth;    Rect.Bottom := (Top + Height)  + FrameWidth;    Rect.Right  := (Left + Width) + FrameWidth;    Canvas.Frame3D(Rect, clRed, clPurple, FrameWidth);    // or: Canvas.FrameRect(Rect);  end;end; end. afaik the above should work on every supported platform (e.g. i have ditched the Windows only solution)

In case you are writing your own custom controls, then things can become a completely different ballgame ;-)

howardpc:
The following code shows another way to highlight focused controls. You may be able to adapt it for your needs.
Drop a TButton and a TEdit on a new form, double-click to make an OnCreate handler and complete it as follows:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit mainBordered; {$mode objfpc}{$H+} interface uses  Classes, Forms, Controls, Graphics, StdCtrls, types; type   { TBorder }   TBorder = class(TGraphicControl)  private    FPenColor: TColor;    FBorderColor: TColor;  protected    procedure Paint; override;    procedure ControlEnter(Sender: TObject);    procedure ControlExit(Sender: TObject);  public    constructor {%H-}Create(AOwner: TForm;                       aWinControl: TWinControl;                       aBorderWidth: integer=3;                       aBorderColor: TColor=clBlue);  end;   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Edit1: TEdit;    procedure FormCreate(Sender: TObject);  end;   Procedure AddFocusBorder(aWinControl: TWinControl;                           aForm: TForm;                           aBorderWidth: integer=3;                           aColor: TColor=clBlue); var  Form1: TForm1; implementation procedure AddFocusBorder(aWinControl: TWinControl; aForm: TForm;  aBorderWidth: integer; aColor: TColor);begin  TBorder.Create(aForm, aWinControl, aBorderWidth, aColor);end; {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  AddFocusBorder(Button1, Self, 5, clMoneyGreen);  AddFocusBorder(Edit1, Self);end; { TBorder } constructor TBorder.Create(AOwner: TForm; aWinControl: TWinControl;  aBorderWidth: integer; aBorderColor: TColor);var  r: TRect;begin  Assert(aWinControl<>nil,'TBorder: non-existent control');  inherited Create(AOwner);  FBorderColor:=aBorderColor;  Canvas.Pen.Width:=aBorderWidth;  r:=aWinControl.BoundsRect;  if InflateRect(r, aBorderWidth, aBorderWidth) then    SetInitialBounds(r.Left, r.Top, r.Right-r.Left, r.Bottom-r.Top);  aWinControl.OnEnter:=@ControlEnter;  aWinControl.OnExit:=@ControlExit;  Parent:=AOwner;end; procedure TBorder.ControlEnter(Sender: TObject);begin  FPenColor:=FBorderColor;  Repaint;end; procedure TBorder.ControlExit(Sender: TObject);begin  FPenColor:=clBtnFace;  Repaint;end; procedure TBorder.Paint;begin  Canvas.Pen.Color:=FPenColor;  Canvas.Rectangle(ClientRect);end; end. 

Trenatos:
Thanks guys, you sparked an idea.

I ended up creating a tshape, and then just update the size, parent, location on click, super simple but works for now.

Navigation

[0] Message Index

[*] Previous page

Go to full version