Mega Code Archive

 
Categories / Delphi / VCL
 

How to make forms client size independent of windows settings

The possibility of changing the size of various Windows elements can be a headache to a developer. For instance we have developed an application which looks very nice on our machine, but when the awaited day of the first public presentation comes, we cannot recognize our thoroughly designed forms: some controls are visible only partially, some are overlapped by scrollbars which appeared out of a sudden on the edges of the forms. The reason is simple: on our development machine we have classical Windows settings while on the computer used for the presentation the desktop style is set to Windows XP, with wide caption bars which 'eat up' forms' client space where we have placed our controls. Below I describe two ways of avoiding such situation: 1. Place a bevel on your form. 2. Set the bevel's Align property to alClient. 3. Set the form's AutoSize property to True and Auto Scroll to false. 4. If you want the form to be sizeable, write a handler for the form's OnShow event, which will set AutoSize back to false when only the form appears on the screen. The other way involves using a non-visible component, which I named ClientGuard. Its task is to stream the form's client dimensions and read them back from a stream when the form is created. Using this component is extremely easy: you just put it on the form. There are no properties to be set or events to be handled. It is however advisable to set the form's AutoScroll property to false. The code of the component is quite simple as well: unit ClntGrd; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; type TClientGuard = class(TComponent) private procedure ReadClientWidth(Reader: TReader); procedure WriteClientWidth(Writer: TWriter); procedure ReadClientHeight(Reader: TReader); procedure WriteClientHeight(Writer: TWriter); { Private declarations } protected { Protected declarations } procedure DefineProperties(Filer: TFiler); override; public { Public declarations } published { Published declarations } end; procedure register; implementation procedure TClientGuard.ReadClientWidth(Reader: TReader); var w: integer; begin w:=Reader.ReadInteger; if Assigned(Owner) and (Owner is TCustomForm) then (Owner as TCustomForm).ClientWidth:=w; end; procedure TClientGuard.WriteClientWidth(Writer: TWriter); begin Writer.WriteInteger((Owner as TCustomForm).ClientWidth); end; procedure TClientGuard.ReadClientHeight(Reader: TReader); var h: integer; begin h:=Reader.ReadInteger; if Assigned(Owner) and (Owner is TCustomForm) then (Owner as TCustomForm).ClientHeight:=h; end; procedure TClientGuard.WriteClientHeight(Writer: TWriter); begin Writer.WriteInteger((Owner as TCustomForm).ClientHeight); end; procedure TClientGuard.DefineProperties(Filer: TFiler); function DoWrite: boolean; begin Result:=Assigned(Owner) and (Owner is TCustomForm); end; begin inherited; Filer.DefineProperty('ClientWidth', ReadClientWidth, WriteClientWidth, DoWrite); Filer.DefineProperty('ClientHeight', ReadClientHeight, WriteClientHeight, DoWrite); end; procedure register; begin RegisterComponents('MyCompnts', [TClientGuard]); end; end.