Messing with the creation process of a form object, or how to change the default style of a window when it gets created to suit your particular needs. Transparent forms, no caption forms, realy StayOnTop forms, ...
Let there be a Delphi Form
When Delphi creates a control that derives from TWinControl (base class for all windowed controls), such as an EditBox or Button or Form, the Create method for that object calls the CreteWindowEx API function to crete the actual window. However, before calling the CreteWindowEx, the CreateParams method is called. CreateParams is a virtual method of the TForm class.
This means you can override it which, in turn, means you can change the default style of a window when it is created to suit your particular needs. In this method, a data structure of type TCreateParams gets filled with information that will be used as the parameters in the CreteWindowEx call. Inheriting CreateParams is a convenient way to to affect a number of different (display, parentage) things about a form, as you'll see in this article.
For example, a custom component writer will typically override the CreateParams method when the window to be created needs to be created using additional Windows style settings.
TCreateParams
The TCreateParams structure is defined as:
type
TCreateParams = record
Caption: PChar; {the window text or caption}
Style: DWORD; {style flag}
ExStyle: DWORD; {extended style flag}
X, Y: Integer; {Left and Top property}
Width, Height: Integer; {Width and Height property}
WndParent: HWND; {Parent window}
Param: Pointer {additional data}
WindowClass: TWndClass; {window calss information}
WinClassName: array[0..63] of Char; {class name}
end;
|
For a complete owerview of the structure and its fields please see the help files provided with Delphi.
As stated above, when Delphi creates a form the CreateParams record gets filled. Most of the fields in this structure are filled with corresponding form properties you can set at design time. For example, the Caption, Width and Height fields hold the values of the Caption, Width and Height properties that we can set by using the Object Inspector. Other form properties are also used in the creation process. For example, if biHelp is ON in BorderIcons property then ExStyle has the WS_EX_CONTEXTHELP bit set ON - which includes a question mark in the title bar of the window.
However, there are some special cases when you can not rely on the default creation process - suppose you want to have a transparent form or something weird like that.
Overriding the CreateParams procedure
When we want to use additional parameters in the creation of the CreateParams structure we need to override the call to CreateParams procedure. (It really is confusing that both the procedure and the record type have the same name.)
In the private part of the form declaration place the next line:
...
private
procedure CreateParams
(var Params: TCreateParams); override;
...
|
In the implementation section place the function code:
procedure TForm.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
{code to alter the CreateParams values}
end;
|
The first line in the above procedure, inherited CreateParams(Params), should always be included. It is used to call the CreateParams for the TForm class and fill in the default set of parameters.
I'll now show you some of the situations when we need to override the CreateParams procedure in order to solve some problem:
Next page > Examples of extended Form functionality > Page 1, 2