2012-08-12 25 views
4

當網格失去焦點到另一個非模態窗體時,Delphi XE2中有沒有一種方法可以保留StringGrid中InPlaceEditor的高光?當失去焦點時保持InPlaceEditor高亮

我現在的StringGrid選項有:

enter image description here

如果沒有,我希望利用下面的代碼失去焦點後保存當前小區的一大亮點,但我有一些麻煩與它離開當它們不再是當前單元格時,單元格會突出顯示。

我是否需要在下面的代碼中添加一個「else」以將顏色更改回原始的非選定單元格?任何警告?

procedure TForm1.sgMultiDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);  
    begin 
    if (ACol = sgMulti.Col) and (ARow = sgMulti.Row) then 
    begin 
     sgMulti.Canvas.Brush.Color := clYellow; 
     sgMulti.Canvas.FillRect(Rect); 
     sgMulti.Canvas.TextRect(Rect, Rect.Left, Rect.Top, sgMulti.Cells[ACol, ARow]); 
     if gdFocused in State then 
     sgMulti.Canvas.DrawFocusRect(Rect); user 
    end; 
    end; { sgMultiDrawCell} 

編輯:下面的屏幕截圖闡明它是如何表現的今天。我想當前單元格,失去焦點的時候,比底部的屏幕捕捉更加清晰

enter image description here

+0

@TLama:我的目標是細胞在失去焦點時仍然以某種方式突出顯示。正如你所說,我沒有想到,這意味着將InPlaceEditor置於編輯模式。在我看來,使用InPlaceEditor不太可能或者更可取,因爲你已經這麼做了。也許我應該在沒有重點時自己突出顯示自己? (如果未聚焦時的突出顯示與編輯模式突出顯示不同,則可以。) – RobertFrank 2012-08-12 16:33:28

+1

您嘗試刪除'goAlwaysShowEditor'選項嗎? – kludg 2012-08-12 16:42:42

+1

@Serg和TLama:我認爲你是對的。刪除goAlwaysShowEditor是我想要的,特別是因爲goEditing已啓用。我想我前段時間可能會設置goAlwaysShowEditor以突出亮點。要突出顯示的邊界框或顏色有多難? Serg:請將您以前的評論發佈爲答案。感謝你們兩位。 – RobertFrank 2012-08-12 17:53:37

回答

6

如果你想保持啓用goAlwaysShowEditor選項,並強調只是始終顯示編輯器,你需要訪問InplaceEditor屬性。這需要子類化您的字符串網格類並更改就地編輯器的顏色,這是默認情況下TCustomMaskEdit控件類。
在這個代碼顯示,如何改變就地編輯的顏色,這取決於當字符串電網
集中與否:

type 
    TStringGrid = class(Grids.TStringGrid) 
    private 
    procedure CMEnter(var Message: TCMEnter); message CM_ENTER; 
    procedure CMExit(var Message: TCMExit); message CM_EXIT; 
    protected 
    function CreateEditor: TInplaceEdit; override; 
    end; 

implementation 

{ TStringGrid } 

procedure TStringGrid.CMEnter(var Message: TCMEnter); 
begin 
    inherited; 
    if Assigned(InplaceEditor) then 
    TMaskEdit(InplaceEditor).Color := $0000FFBF; 
end; 

procedure TStringGrid.CMExit(var Message: TCMExit); 
begin 
    inherited; 
    if Assigned(InplaceEditor) then 
    TMaskEdit(InplaceEditor).Color := $0000A6FF; 
end; 

function TStringGrid.CreateEditor: TInplaceEdit; 
begin 
    Result := inherited; 
    if Focused then 
    TMaskEdit(Result).Color := $0000FFBF 
    else 
    TMaskEdit(Result).Color := $0000A6FF; 
end; 

而且隨着聚焦而散電網狀態的結果:

enter image description here

+2

哇!完全是我正在嘗試做的。謝謝,@TLama !!! – RobertFrank 2012-08-12 20:08:31

+1

很高興幫助;-) – TLama 2012-08-12 20:19:55