Examples Delphi

Title: Playing with style - Part 1
Question: I like the flat style buttons in the Delphi3000 web pages. How do I get that style?
Answer:
Searching the Windows documentation I found that Microsoft added a flat style to button derived class, but this style is not include in the Delphi ones: for some reason the buttons in the Delphi VCL are strictly 3D and, as far I can see, there is no property to 'flatten' the buttons (even the legacy Ctl3D property has no effect on TButtons).
The style in question is the BS_FLAT style. Changing the style of a button derived class (buttons, check boxes, radio buttons and group boxes) will change the graphical appearance of that control.
Try this:
procedure Flatten(theControl: TWinControl);
var
dwStyle: Longint;
begin
dwStyle := GetWindowLong(theControl.handle, GWL_STYLE) or BS_FLAT;
SetWindowLong(theControl.Handle, GWL_STYLE, dwStyle);
end;
The control you can pass to this procedure can be a Button, a CheckBox or a RadioButton. Passing a GroupBox will not give any result because, oddly enough, the window class underlying the implementation of a group box is not a button class. In this case the legacy property Ctld3D will work!
To sharpen things down you can derive a class form the TButton, TCheckBox or TRadioButton and add the BS_FLAT style upon creation.
This is the code I used to obtain a flat button:
unit FlatButton;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TFlatButton = class(TButton)
private
{ Private declarations }
protected
{ Protected declarations }
procedure CreateParams(var Params: TCreateParams); override;
public
{ Public declarations }
published
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Bigpepe', [TFlatButton]);
end;
{ TFlatButton }
procedure TFlatButton.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.Style := Params.Style or BS_FLAT;
end;
end.
If you want a flat checkbox or a flat radio button, simply change the base class (and the name of the derived one) to TCheckBox and TRadioButton respectively.
Enjoy!