2010-08-05 63 views

回答

2

Tofig,您可以使用MouseCoord過程獲取當前行和關口,但表現出pos的值[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; 
+0

你並不需要得到MouseCoord。 OnMouseMove事件已經擁有它們。 – Linas 2010-08-05 06:42:11

+0

@Linas,X和Y值返回一個點(X,Y),您需要使用函數'MouseCoord'將這些值轉換爲col和row索引,請查看此鏈接http://docwiki.embarcadero.com/VCL /en/Grids.TCustomGrid.MouseCoord – RRUZ 2010-08-05 06:47:04

+0

謝謝,這工作。我有一種感覺,應該有一些更簡單的方法。我的意思是DbGrid應該在某處存儲這些值,以便繪製它們。我們不能直接以某種方式訪問​​它們嗎? – 2010-08-05 07:31:15

相關問題