Wednesday, September 07, 2011

A resizable dialog box

I can't claim that this was my idea, but it can be useful in certain circumstances. Normally a dialog box should be of fixed size; the controls are normally at fixed locations and so enlarging the box doesn't really change anything. For an extreme example, try maximising a dialog box. Thus the dialog box style doesn't allow resizing.

But every now and then, it can be useful to have a resizable dialog box: let's say that the only controls within the dialog box are a rich edit control (possibly connected to a database) and a pair of buttons. Using the anchor property, the rich edit can have all four anchors set, so its size will grow as the dialog box grows. The button on the left has its bottom and left anchors set whereas the button on the right has its bottom and right anchors set.


Here's the code
type
  TSizeDemo = class(TForm)
    Memo1: TMemo;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    PaintBox1: TPaintBox;
    procedure PaintBox1Paint(Sender: TObject);
  private
  protected    procedure CreateParams(var Params: TCreateParams); override;
    procedure CreateWnd; override;
  end;
implementation

{$R *.dfm}

procedure TSizeDemo.CreateParams(var Params: TCreateParams);
begin
 inherited CreateParams (Params);
 Params.Style:= WS_CAPTION or WS_SIZEBOX or WS_SYSMENU;
 Params.ExStyle:= WS_EX_DLGMODALFRAME or WS_EX_WINDOWEDGE;
end;

procedure TSizeDemo.CreateWnd;
begin
 inherited CreateWnd;
 SendMessage (Self.Handle, WM_SETICON, 1, 0);
end;

procedure TSizeDemo.PaintBox1Paint(Sender: TObject);
begin
 With PaintBox1 do
  DrawFrameControl (Canvas.Handle,
                   Rect (Width - 12, Height - 12, Width, Height),
                   DFC_SCROLL,
                   DFCS_SCROLLSIZEGRIP );
end;

end.
The paintbox is necessary in order to display the sizing handle in the bottom right hand corner. Here is the link to the original article.

No comments: