Reputation: 14611
I have a TComboBox
with Style:= csOwnerDrawVariable;
and I want to show the disabled Font
color in black and not in 'gray'.
This is what I get with this source:
procedure TCustomComboBox.WndProc(var Message: TMessage);
begin
case Message.Msg of
CN_CTLCOLORMSGBOX .. CN_CTLCOLORSTATIC, //48434..48440
WM_CTLCOLORMSGBOX .. WM_CTLCOLORSTATIC:
begin
Color:= GetBackgroundColor; // get's the current background state
Brush.Color:= Color;
end;
end;
inherited;
end;
But I want the Font color of the inner Edit
control in black.
If I change Font.Color:= clBlack
at the WndProc
or something else nothing happens.
A Google search give me some tips about changing a TEdit
as read only, but this doesn't help me yet.
Update
Here is now my short solution after getting the tip from @Abelisto.
TCustomComboBox = class (TComboBox)
protected
procedure DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState); override;
end;
procedure TCustomComboBox.DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState);
begin
if odComboBoxEdit in State then begin // If we are drawing item in the edit part of the Combo
if not Enabled then
Canvas.Font.Color:= clBlack; // Disabled font colors
Canvas.Brush.Color:= GetBackgroundColor; // Get the right background color: normal, mandatory or disabled
end;
inherited DrawItem(Index, Rect, State);
end;
Upvotes: 4
Views: 3754
Reputation: 15614
Use OnDrawItem
event.
There is no special settings for the components at design time - all performed in code. Just put on the form ComboBox1 and Button1 and assign the events to them.
procedure TForm3.Button1Click(Sender: TObject);
begin
ComboBox1.Enabled := not ComboBox1.Enabled; // Change Enabled state
end;
procedure TForm3.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
txt: string;
begin
if Index > -1 then
txt := ComboBox1.Items[Index]
else
txt := '';
if odComboBoxEdit in State then // If we are drawing item in the edit part of the Combo
if ComboBox1.Enabled then
begin // Enabled colors
ComboBox1.Canvas.Font.Color := clRed; // Foreground
ComboBox1.Canvas.Brush.Color := clWindow; // Background
end
else
begin // Disabled colors
ComboBox1.Canvas.Font.Color := clYellow;
ComboBox1.Canvas.Brush.Color := clGray;
end;
ComboBox1.Canvas.TextRect(Rect, Rect.Left, Rect.Top, txt); // Draw item. It may be more complex
end;
procedure TForm3.FormCreate(Sender: TObject);
begin
with ComboBox1 do // Setup combo props
begin
Items.Add('111');
Items.Add('222');
Items.Add('333');
ItemIndex := 1;
Style := csOwnerDrawVariable;
end;
end;
Upvotes: 6