2015-09-01 120 views
3

我有一個TComboBoxStyle:= csOwnerDrawVariable;,我想顯示禁用的Font顏色爲黑色,而不是「灰色」。如何更改禁用TComboBox(Delphi)的字體顏色?

這就是我得到這個來源:

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; 

enter image description here

但我想在黑內Edit控制的字體顏色。

如果我更改Font.Color:= clBlackWndProc或別的什麼都沒有發生。

谷歌搜索給了我一些關於將TEdit更改爲只讀的tipps,但這對我還沒有幫助。

更新

這裏現在是@Abelisto得到TIPP後,我的短期解決方案。

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; 
+0

你在哪裏改變Font.Color?在WndProc或其他地方? – GuidoG

+0

@GuidoG耶也嘗試過WndProc,但不能改變編輯控件的字體。 – punker76

+0

不知道它是否有效,但在wndprod中,畫筆用於背景,筆用於前景。嘗試改變筆。顏色 – GuidoG

回答

5

使用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; 
+0

這在德爾福2010年效果很好 –