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:
1 unit ClntGrd;
2 3 interface4 5 uses6 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
7 8 type9 TClientGuard = class(TComponent)
10 private11 procedure ReadClientWidth(Reader: TReader);
12 procedure WriteClientWidth(Writer: TWriter);
13 procedure ReadClientHeight(Reader: TReader);
14 procedure WriteClientHeight(Writer: TWriter);
15 { Private declarations }16 protected17 { Protected declarations }18 procedure DefineProperties(Filer: TFiler); override;
19 public20 { Public declarations }21 published22 { Published declarations }23 end;
24 25 procedureregister;
26 27 implementation28 29 30 procedure TClientGuard.ReadClientWidth(Reader: TReader);
31 var32 w: integer;
33 begin34 w:=Reader.ReadInteger;
35 if Assigned(Owner) and (Owner is TCustomForm) then36 (Owner as TCustomForm).ClientWidth:=w;
37 end;
38 39 40 procedure TClientGuard.WriteClientWidth(Writer: TWriter);
41 begin42 Writer.WriteInteger((Owner as TCustomForm).ClientWidth);
43 end;
44 45 46 procedure TClientGuard.ReadClientHeight(Reader: TReader);
47 var48 h: integer;
49 begin50 h:=Reader.ReadInteger;
51 if Assigned(Owner) and (Owner is TCustomForm) then52 (Owner as TCustomForm).ClientHeight:=h;
53 end;
54 55 56 procedure TClientGuard.WriteClientHeight(Writer: TWriter);
57 begin58 Writer.WriteInteger((Owner as TCustomForm).ClientHeight);
59 end;
60 61 62 procedure TClientGuard.DefineProperties(Filer: TFiler);
63 function DoWrite: boolean;
64 begin65 Result:=Assigned(Owner) and (Owner is TCustomForm);
66 end;
67 begin68 inherited;
69 Filer.DefineProperty('ClientWidth', ReadClientWidth, WriteClientWidth, DoWrite);
70 Filer.DefineProperty('ClientHeight', ReadClientHeight, WriteClientHeight,
71 DoWrite);
72 end;
73 74 procedureregister;
75 begin76 RegisterComponents('MyCompnts', [TClientGuard]);
77 end;
78 79 80 end.