如何更改禁用的 TComboBox 的字体颜色?
How to change Font color for disabled TComboBox?
我有一个 TComboBox
和 Style:= csOwnerDrawVariable;
,我想显示禁用的 Font
黑色而不是 'gray'。
这是我从这个来源得到的:
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;
但我想要内部 Edit
控件的字体颜色为黑色。
如果我在 WndProc
处更改 Font.Color:= clBlack
或其他什么都不会发生。
Google 搜索给了我一些关于将 TEdit
更改为只读的提示,但这对我没有帮助。
更新
这是我从@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;
使用OnDrawItem
事件。
在设计时没有对组件进行特殊设置 - 全部在代码中执行。只需放入窗体 ComboBox1 和 Button1 并将事件分配给它们。
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;
我有一个 TComboBox
和 Style:= csOwnerDrawVariable;
,我想显示禁用的 Font
黑色而不是 'gray'。
这是我从这个来源得到的:
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;
但我想要内部 Edit
控件的字体颜色为黑色。
如果我在 WndProc
处更改 Font.Color:= clBlack
或其他什么都不会发生。
Google 搜索给了我一些关于将 TEdit
更改为只读的提示,但这对我没有帮助。
更新
这是我从@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;
使用OnDrawItem
事件。
在设计时没有对组件进行特殊设置 - 全部在代码中执行。只需放入窗体 ComboBox1 和 Button1 并将事件分配给它们。
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;