Delphi 使用StringGrid选择性地显示提示
在StringGrid组件子体中,我想根据单元格的值更改弹出提示消息。我的编码:Delphi 使用StringGrid选择性地显示提示,delphi,tstringgrid,Delphi,Tstringgrid,在StringGrid组件子体中,我想根据单元格的值更改弹出提示消息。我的编码: procedure TForm.GridMouseEnterCell(Sender: TObject; ACol, ARow: Integer); var k: integer; begin k := strtointdef(Grid.Cells[13, ARow],-1); Grid.ShowHint := (ACol = 12) and (k >= 0); if Grid.ShowHint
procedure TForm.GridMouseEnterCell(Sender: TObject; ACol, ARow: Integer);
var k: integer;
begin
k := strtointdef(Grid.Cells[13, ARow],-1);
Grid.ShowHint := (ACol = 12) and (k >= 0);
if Grid.ShowHint then
Grid.Hint := MyLIst.Items[k];
end;
当我将鼠标从另一列移到第12列时,这很好,但是如果停留在第12列并移动到另一行(使用不同的k值),弹出提示不会改变。只有当我第一次将鼠标移到另一列,然后返回到第12列时,它才会显示正确/新的提示。有人有解决方案吗?您确定
OnMouseEnterCell()
事件工作正常吗?当您停留在列中并移动到另一行时,是否会调用它?因为这是一个后代的事件,而不是TStringGrid的事件,所以我们对它没有洞察力
另外,尝试放置Application.ActivateHint(Mouse.CursorPos)代码>在函数末尾。它将强制提示重新显示:
procedure TForm.GridMouseEnterCell(Sender: TObject; ACol, ARow: Integer);
var
k: integer;
begin
k := StrToIntDef(Grid.Cells[13, ARow], -1);
Grid.ShowHint := (ACol = 12) and (k >= 0);
if Grid.ShowHint then
begin
Grid.Hint := MyList.Items[k];
Application.ActivateHint(Mouse.CursorPos);
end;
end;
在运行时修改提示最干净的方法是拦截CM\u HINTSHOW
消息。这样做意味着你不需要找出所有可能导致你的提示改变的不同事件。相反,您只需等待提示即将显示,然后使用控件的当前状态来确定要显示的内容
下面是使用插入器类的示例:
type
TStringGrid = class(Grids.TStringGrid)
protected
procedure CMHintShow(var Message: TCMHintShow); message CM_HINTSHOW;
end;
procedure TStringGrid.CMHintShow(var Message: TCMHintShow);
var
HintStr: string;
begin
inherited;
// customise Message.HintInfo to influence how the hint is processed
k := StrToIntDef(Cells[13, Row], -1);
if (Col=12) and (k>=0) then
HintStr := MyList.Items[k]
else
HintStr := '';
Message.HintInfo.HintStr := HintStr;
end;
如果您想让它更有用,您可以派生一个子类TStringGrid
,并添加OnShowHint
事件,允许以较少耦合的方式指定此类定制。您可以尝试Grid.ShowHint:=(ACol=12)和(k>=0)和(Grid.Hint MyLIst.Items[k])
重新触发提示的显示。一种不涉及拦截器(并且可以在设计时设置)的“更干净”方法是使用TApplication(Events).OnShowHint
事件。但概念是相同的,因为该事件提供了对相同THintInfo
记录的访问。