【问题标题】:How to color the background of a TComboBox with VCL styles enabled如何在启用 VCL 样式的情况下为 TComboBox 的背景着色
【发布时间】:2013-05-14 08:55:14
【问题描述】:

我正在尝试为启用了 VCL 样式的 TComboBox 的背景着色,就像它在本文中描述的方式一样,但它不起作用。

http://theroadtodelphi.wordpress.com/2012/02/06/changing-the-color-of-edit-controls-with-vcl-styles-enabled/

【问题讨论】:

  • 好的。有什么问题?
  • 他在您链接的文章中解释了如何解决它?

标签: delphi vcl-styles


【解决方案1】:

根据你的 Delphi 版本,你必须

德尔福 XE2

对于 Delphi XE2,你必须编写一个 Style Hook

uses
  Vcl.Styles,
  Vcl.Themes;

type
  TComboBoxStyleHookExt= class(TComboBoxStyleHook)
    procedure UpdateColors;
  strict protected
    procedure WndProc(var Message: TMessage); override;
  public
    constructor Create(AControl: TWinControl); override;
  end;

  TWinControlClass= class(TWinControl);


{ TComboBoxStyleHookExt }

constructor TComboBoxStyleHookExt.Create(AControl: TWinControl);
begin
  inherited;
  UpdateColors;
end;

procedure TComboBoxStyleHookExt.UpdateColors;
const
  ColorStates: array[Boolean] of TStyleColor = (scComboBoxDisabled, scComboBox);
  FontColorStates: array[Boolean] of TStyleFont = (sfComboBoxItemDisabled, sfComboBoxItemNormal);
var
  LStyle: TCustomStyleServices;
begin
 if Control.Enabled then }//use the control colors
 begin
  Brush.Color := TWinControlClass(Control).Color;
  FontColor   := TWinControlClass(Control).Font.Color;
 end
 else
 begin //if not enabled. use the current style colors
  LStyle := StyleServices;
  Brush.Color := LStyle.GetStyleColor(ColorStates[Control.Enabled]);
  FontColor := LStyle.GetStyleFontColor(FontColorStates[Control.Enabled]);
 end;
end;

procedure TComboBoxStyleHookExt.WndProc(var Message: TMessage);
begin
  case Message.Msg of
    WM_CTLCOLORMSGBOX..WM_CTLCOLORSTATIC,
    CN_CTLCOLORMSGBOX..CN_CTLCOLORSTATIC:
      begin
        UpdateColors;
        SetTextColor(Message.WParam, ColorToRGB(FontColor));
        SetBkColor(Message.WParam, ColorToRGB(Brush.Color));
        Message.Result := LRESULT(Brush.Handle);
        Handled := True;
      end;
    CM_ENABLEDCHANGED:
      begin
        UpdateColors;
        Handled := False;
      end
  else
    inherited WndProc(Message);
  end;
end;

initialization
  TStyleManager.Engine.RegisterStyleHook(TComboBox, TComboBoxStyleHookExt);

德尔福 XE3/XE4

只需从 StyleElements 属性中删除 seClient

  ComboBox1.StyleElements:=[seFont, seBorder];
  ComboBox2.StyleElements:=[seFont, seBorder];
  ComboBox3.StyleElements:=[seFont, seBorder];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 2013-11-07
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 2014-04-18
    相关资源
    最近更新 更多