2015-10-12 78 views
2

我想更改爲我的TEdits Texthint灰色。德爾福XE7:TEdit TextHint顏色

我媒體鏈接發現這個https://stackoverflow.com/a/31550017/1862576並試圖通過SendMessage函數改變顏色像這樣

procedure TEdit.DoSetTextHint(const Value: string); 
var 
    Font: TFont; 
begin 
    if CheckWin32Version(5, 1) and StyleServices.Enabled and HandleAllocated then 
    begin 
    Font := TFont.Create; 
    try 
     Font.Assign(self.Font); 
     Font.Color := clGreen; 
     Font.Size := 20; 

     SendTextMessage(Handle, EM_SETCUEBANNER, WPARAM(1), Value); 
     SendMessage(Handle, WM_SETFONT, Integer(Font.Handle), Integer(True)); 
    finally 
//  Font.Free; 
    end; 
    end;  
end; 

它改變了字體的大小,但不是顏色。 感謝您的幫助。

+0

的問題是,你的代碼要更改Ç用於TEdit文本而不是HintText。現在我甚至不確定VCL是否暴露了TextHint顏色,或者它是否可以硬編碼。 – SilverWarior

+0

底層Win32 API沒有提示橫幅顏色功能,因此無法將自定義顏色應用於提示橫幅文本。 –

回答

3

提示橫幅是嵌入到底層Win32 EDIT控件中的一項功能,TEdit包裝。它不是由VCL管理的。沒有公開的Win32 API來管理提示橫幅文本的顏色。如果您需要自定義着色,則必須直接停止使用本地提示橫幅功能,並通過直接處理其WM_ERASEBKGND和/或WM_PAINT消息來手動自定義繪製編輯控件(請參閱How do i custom draw of TEdit control text?)。否則,您將不得不查找支持自定義着色的第三方Edit控件。或者使用TRichEdit而不是TEdit,這樣您可以根據需要設置文本顏色。

1

defenation

Type 
    HitColor = class helper for tEdit 
     private 
     procedure SetTextHintColor(const Value: TColor); 
     function GetTextHintColor: TColor; 
     procedure fixWndProc(var Message: TMessage); 
    published 
     property TextHintColor : TColor read GetTextHintColor write SetTextHintColor; 
    end; 

實施

procedure HitColor.fixWndProc(var Message: TMessage); 
var 
    dc : HDC ; 
    r : TRect ; 
    OldFont: HFONT; 
    OldTextColor: TColorRef; 
    Handled : boolean; 
begin 
    Handled := false; 
    if (Message.Msg = WM_PAINT) and (Text = '') and not Focused then 
        begin 

        self.WndProc(Message); 
        self.Perform(EM_GETRECT, 0, LPARAM(@R)); 
        dc := GetDC(handle); 
        try 
         OldFont := SelectObject(dc, Font.Handle); 
         OldTextColor := SetTextColor(DC, ColorToRGB(GetTextHintColor)); 

         FillRect(dc,r,0); 
         DrawText(DC, PChar(TextHint), Length(TextHint), R, DT_LEFT or DT_VCENTER or DT_SINGLELINE or DT_NOPREFIX); 
        finally 
         SetTextColor(DC, OldTextColor); 
         SelectObject(DC, OldFont); 
         ReleaseDC(handle,dc); 
        end; 
        Handled := true; 
       end; 




    if not Handled then WndProc(Message); 

end; 

function HitColor.GetTextHintColor: TColor; 
begin 
    result := tag; 
end; 

procedure HitColor.SetTextHintColor(const Value: TColor); 
begin 
    tag := Value; 
    WindowProc := fixWndProc ; 
end; 

使用:

edit1.TextHintColor := clred;