我知道,可以为组件禁用自定义样式,但是如何仅对一个组件类启用样式?例如,保留整个窗体及其上的所有组件的外观不变,只对TButton设置外观。喜欢这张图片。

最佳答案

大多数VCL控件在内部使用StyleServices全局函数来获取绘制控件的方法。因此,如果您不使用Vcl样式,则StyleServices将实例返回到Windows API函数以绘制主题控件(UxTheme API)。因为没有办法只将单个类控件(至少是您自己绘制该控件)的外观(应用Vcl样式)换肤。

因此,唯一的选择是应用Vcl样式,然后对除所要查找的一种类型之外的所有控件禁用。

你可以用这样的东西

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;


使用Vcl样式检查此表单



现在调用上述方法之后

DisableVclStyles(Self,'TButton');




注意:使用StyleElements属性启用o禁用vcl样式不适用于某些组件,例如(TStringGrid,TBitBtn,TSpeedButton等)

07-27 13:14
查看更多