Title: A new Checkbox
Question: Change checkbox control appearance, while keeping its original behaviour
Answer:
The unit below defines a new component based on TCheckBox; this new component behaves like a normal checkbox, but looks like a pushbutton:
{-begin code-}
unit CheckBoxX;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TCheckBoxX = class(TCheckBox)
private
{ Private declarations }
protected
procedure CreateParams(var Params: TCreateParams); override;
public
{ Public declarations }
published
{ Published declarations }
end;
procedure Register;
implementation
procedure TCheckBtn.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.Style := Params.Style or BS_PUSHLIKE;
end;
procedure Register;
begin
RegisterComponents('Odin Software', [TCheckBoxX]);
end;
end.
[-end code-}
As you can see, all we have to do is override the protected method CreateParams and add a new flag: BS_PUSHLIKE; BS_ stands for "button style". The createparams method is the place where we must set any custom parameter to the control class, prior to its creation. It's VCL encapsulation of WinAPI stuff.