0

OnMouseMove イベントがあり、その間に特定のセルの値を見つけたいと考えています (必ずしもマウスの下のセルではありません)。基本的に問題は次のとおりです。x 座標と y 座標を使用して、セル データを選択したり、フォーカスを変更したりせずにセル データにアクセスする方法は?

4

1 に答える 1

2

Tofigでは、プロシージャを使用しMouseCoordて現在の行と列を取得できますが、posの値を表示するには、プロパティをRow値に[Col,Row]設定し、保護されたプロパティにアクセスするための新しいクラスの子孫を作成する必要があります。DataLink.ActiveRecord

このコードを確認してください

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 に答える