如何使启用VCL样式的TComboBox的背景颜色 [英] How to color the background of a TComboBox with VCL styles enabled

查看:211
本文介绍了如何使启用VCL样式的TComboBox的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用类似于本文中描述的方式启用VCL样式的TComboBox的背景颜色。 http://theroadtodelphi.wordpress.com/2012/02/06/changing-the-color-of-edit-controls-with-vcl-styles-enabled/rel =nofollow> http://theroadtodelphi.wordpress .com / 2012/02/06 / change-the-color-of-edit-controls-with-vcl-styles-enabled /

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

推荐答案

根据您的Delphi版本,您必须

Depending of your Delphi version you must

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

For Delphi XE2 you must write a 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);



Delphi XE3 / XE4



只需删除 seClient 的值来自 StyleElements propertty

Delphi XE3/XE4

Simply remove the seClient value from the StyleElements propertty

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

这篇关于如何使启用VCL样式的TComboBox的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆