0

我有 OnMouseMove 事件,在此期间我想找到某个单元格的值(不一定是鼠标下的那个)。基本上问题是:如何使用其 x 和 y 坐标访问单元格数据而不选择它、更改焦点等?

4

1 回答 1

2

Tofig,您可以使用该MouseCoord过程来获取当前行和列,但要显示 pos 的值,[Col,Row]您必须将DataLink.ActiveRecord属性设置为 Row 值并创建一个新的类后代以访问受保护的属性。

检查此代码

type
 THackGrid = class(TCustomDBGrid); //Create a new class to access the protected properties


procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
    Cell             : TGridCoord;
    Row,Col          : integer;
    OrigActiveRecord : integer;
begin
    inherited;
    Cell:=DBGrid1.MouseCoord(X,Y);
    Col:= Cell.X;
    Row:= Cell.Y;

  if dgTitles in DBGrid1.Options    then  Dec(Row); //if the titles are shown then adjust Row index (-1);
  if dgIndicator in DBGrid1.Options then  Dec(Col); //if the indicator is shown then adjust the Column index (-1);

    if THackGrid(DBGrid1).DataLink.Active and (Row>=0) and (Col>=0)  then
    begin
       OrigActiveRecord:=THackGrid(DBGrid1).DataLink.ActiveRecord; //save the original index
      try
       THackGrid(DBGrid1).DataLink.ActiveRecord:= Row;
       Label1.Caption:=DBGrid1.Columns[Col].Field.AsString; //show the current value in a tlabel
      finally
        THackGrid(DBGrid1).DataLink.ActiveRecord:= OrigActiveRecord; //restore the index
      end;
    end;
end;
于 2010-08-05T06:36:38.950 回答