Einzelnen Beitrag anzeigen

EmWieMichael

Registriert seit: 28. Mär 2012
103 Beiträge
 
#1

TComboBox unter Windows 10

  Alt 9. Apr 2021, 11:25
Bekanntermaßen wird die ComboBox mit der Eigenschaft 'DropDownList' unter Windows 10 bei Enabled=True grau dargestellt. Ein Verhalten, das weder ich, noch meine Anwender hinnehmen wollen.
Auf 'Stackoverflow' bin ich auf folgende Lösung des Problems gestoßen. Leider zeigen sich meine ComboBoxen (Delphi 10.2) davon völlig unberührt.

Falls also jemand einen Tipp hat, wie man den ComboBoxen einen frischeren Anstrich verpassen kann, wäre ich sehr dankbar.

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

  TWinControlClass= class(TWinControl);

implementation

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);
end.
  Mit Zitat antworten Zitat