Styling only one VCL component in Delphi
Asked Answered
L

1

7

I know, that it's possible to disable custom styling for components, but how can I enable styles for only one component class? For example leave the whole form and all components on it unskinned, and skin only TButton. Like on this image.

enter image description here

Landes answered 25/12, 2012 at 13:39 Comment(3)
if you need just styled button - try to find styled button - TButton descendant component wich looks not like usual TButton. Adding VCL Styles functionality to the project and then disabling it for a whole project, excluding TButton, is not good way to skin just one button in project.Fadge
It just looks strange to have styling funtionality in Delphi and to not be able to use it without applying to the whole application.Landes
@Landes I don't find it strange - it was designed for that purpose. As long as you're applying a theme, you're assumed to be skinning the entire application. Personally I find it strange to use these styles to skin only certain controls - given the mechanism in how it works. For your purpose, I would create my own custom button control, which is rather trivial.Essa
D
13

Most of the VCL controls internally uses the StyleServices global function to get the methods to draw the control. So if you are not using the Vcl Styles, the StyleServices return an instance to the windows API functions to draw themed controls (UxTheme API's). because that there is not way to skin (apply the Vcl Styles) to only a single class control (at least which you draw the control yourself).

So the only alternative is apply a Vcl Styles and then disable for all the controls except the one type which you are looking for.

You can use something like this

procedure DisableVclStyles(Control : TControl;const ClassToIgnore:string);
var
  i : Integer;
begin
  if Control=nil then
    Exit;

  if not Control.ClassNameIs(ClassToIgnore) then
   Control.StyleElements:=[];

  if Control is TWinControl then
    for i := 0 to TWinControl(Control).ControlCount-1 do
      DisableVclStyles(TWinControl(Control).Controls[i], ClassToIgnore);
end;

Check this form with a Vcl Style

enter image description here

And now after of call the above method

DisableVclStyles(Self,'TButton');

enter image description here

Note : using the StyleElements property to enable o disable the vcl styles doesn't work with some component like (TStringGrid, TBitBtn, TSpeedButton and so on)

Douma answered 25/12, 2012 at 17:49 Comment(3)
I'm trying this in XE2 and TControl doesn't have a property StyleElementsEssa
Nevermind I just found another answer of yours: #16539390Essa
what if my component gets created in a DataModule? when I put " DisableVclStyles(Self,'TButton'); " in the DataModule the IDE returns me this error: Incompatible types: 'TControl' and 'TDMRotinas'Linnette

© 2022 - 2024 — McMap. All rights reserved.