4

当网格失去焦点到另一个非模态表单时,Delphi XE2 中是否有办法在 StringGrid 中保留 InPlaceEditor 的突出显示?

我当前的 StringGrid 选项是:

在此处输入图像描述

如果没有,我曾希望在失去焦点后使用下面的代码来保留当前单元格的高亮显示,但是当单元格不再是当前单元格时,我会遇到一些问题。

我是否需要在下面的代码中添加“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}

编辑:下面的屏幕截图阐明了它今天的表现。我希望当前单元格在失去焦点时比底部屏幕截图更清晰

在此处输入图像描述

4

1 回答 1

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;

以及聚焦和不聚焦网格状态的结果:

在此处输入图像描述

于 2012-08-12T18:41:09.117 回答