Mirror

How to draw a frame around a TImage (Views: 39)


Problem/Question/Abstract:

How to draw a frame around a TImage

Answer:

unit paintframe;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Image1: TImage;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormPaint(Sender: TObject);
  private
    { Private Declarations}
  public
    procedure drawtheframe(Fwidth: Integer; colr: TColor);
    procedure Hideframe;
    procedure Showframe;
  end;

var
  Form1: TForm1;

const
  Fwidth = 2;
  There: boolean = false;

implementation

{$R *.DFM}

procedure TForm1.drawtheframe(Fwidth: Integer; colr: TColor);
var
  z: Integer;
begin
  z := ord(not (odd(Fwidth)));
  canvas.brush.style := bsClear;
  canvas.pen.width := Fwidth;
  canvas.pen.color := colr;
  Fwidth := width1 - (Fwidth div 2);
  canvas.rectangle(image1.left - Fwidth, image1.top - Fwidth, image1.left + image1.width
    + Fwidth + z, image1.top + image1.height + Fwidth + z);
end;

procedure TForm1.Showframe;
begin
  drawtheframe(Fwidth, clBlue);
  There := true;
end;

procedure TForm1.Hideframe;
begin
  There := false;
  drawtheframe(Fwidth, color);
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  if There then
    Showframe;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Showframe;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Hideframe;
end;

end.

<< Back to main page