失去焦点时保持InPlaceEditor高亮显示

时间:2012-08-12 16:21:48

标签: delphi

当网格失去焦点到另一个非模态形式时,Delphi XE2中是否有一种方法可以在StringGrid中保留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

1 个答案:

答案 0 :(得分:6)

如果要保持启用goAlwaysShowEditor选项并仅突出显示始终显示的编辑器,则需要访问InplaceEditor属性。这需要对字符串网格类进行子类化并更改inplace编辑器的颜色,默认情况下为TCustomMaskEdit控件类。
在此代码中显示如何更改inplace编辑器的颜色,具体取决于当字符串网格是否聚焦时:

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

相关问题