Friday, 19 October 2012

How to put a single line border around a form

Recently I wanted to put a single line border around a form without having a title bar. The way I solved the problem was to set the border style to bsNone and put the following in the OnPaint event of the form.


var
    YFrame: Integer;
    Rect  : TRect;
begin
    inherited;
    if not(fsModal in FormState) then
    begin
        YFrame      := 1;
        Rect.Left   := 0;
        Rect.Top    := 0;
        Rect.Right  := Width;
        Rect.Bottom := Height;

        Canvas.Handle      := GetWindowDC(Handle);
        Canvas.Brush.Color := clBlack;
        Canvas.Brush.Style := bsSolid;
        Canvas.Rectangle(Rect.Left, Rect.Top, Rect.Right, YFrame);
        Canvas.Rectangle(Rect.Left, Rect.Top, YFrame, Rect.Bottom);
        Canvas.Rectangle(Rect.Right - YFrame, Rect.Top, Rect.Right, Rect.Bottom);
        Canvas.Rectangle(Rect.Left, Rect.Bottom - YFrame, Rect.Right, Rect.Bottom);
    end;
end;


1 comment:

  1. To get this working correctly you may need to remove the "if not(fsModal in FormState) then" part of the code.

    ReplyDelete