请教:GRID单元格光标跟随鼠标指针移动而能上下左右移动 (100分)

  • 主题发起人 主题发起人 kaithink
  • 开始时间 开始时间
K

kaithink

Unregistered / Unconfirmed
GUEST, unregistred user!
请教:<br>如何可在DBGRID或其它GRID里,当鼠标移动到哪个单元格上时HINT显示或在别处的EDIT里显示鼠标指针下的单元格内容。注意:该单元格并未获得焦点<br><br>同类的问题:如何可做到让DBGRID的单元格焦点随鼠标指针的移动而移动,也就是光标跟随鼠标移动。<br>
 
在onMouseMove事件里写<br>以下是在stringgrid中测试通过的例子。<br>procedure TForm1.StringGrid1MouseMove(Sender: TObject; Shift: TShiftState;<br>&nbsp; X, Y: Integer);<br>var<br>&nbsp; CellRect:TRect;<br>&nbsp; i,j:integer;<br>begin<br>&nbsp; for i:=0 to Stringgrid1.ColCount-1 do<br>&nbsp; begin<br>&nbsp; &nbsp; for j:=0 to stringgrid1.RowCount-1 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; CellRect:= stringgrid1.CellRect(j,i);<br>&nbsp; &nbsp; &nbsp; if (x&gt;CellRect.Left)and(x&lt;CellRect.Right)and<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(y&gt;CellRect.Top)and(y&lt;CellRect.Bottom) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;edit1.Text:=stringgrid1.Cells[j,i];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;
 
这个问题是这样的:<br>1、TStringGrid 可以<br>2、TDBGrid 在鼠标移动时想取得单元格的内容,就必需使焦点随鼠标指针的移动<br><br>实现方法:<br>基本上是在<br>procedure TForm1.StringGrid1MouseMove(Sender: TObject; Shift: TShiftState;<br>事件里,用<br>StringGrid1.MouseCoord(X, Y)<br>的方法来解决。<br>如需例子,请留个EMAIL联系
 
从TDBGrid继承一个新的控件,比如叫TDBGrid1;然后重载MouseMove过程;代码如下<br><br>
代码:
unit DBGrid1;<br><br>interface<br><br>uses<br>&nbsp; SysUtils, Classes, Controls, Grids, DBGrids;<br><br>type<br>&nbsp; TDBGrid1 = class(TDBGrid)<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; protected<br>&nbsp; &nbsp; { Protected declarations }<br>&nbsp; &nbsp; procedure MouseMove(Shift: TShiftState; X, Y: Integer); override; &nbsp; &nbsp;<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; published<br>&nbsp; &nbsp; { Published declarations }<br>&nbsp; end;<br><br>procedure Register;<br><br>implementation<br><br>procedure Register;<br>begin<br>&nbsp; RegisterComponents('Samples', [TDBGrid1]);<br>end;<br><br>{ TDBGrid1 }<br><br>procedure TDBGrid1.MouseMove(Shift: TShiftState; X, Y: Integer);<br>var<br>&nbsp; CellHit: TGridCoord;<br>begin<br>&nbsp; inherited MouseMove(Shift,X,Y);<br>&nbsp; if FGridState=gsNormal then<br>&nbsp; begin<br>&nbsp; &nbsp; CellHit := MouseCoord(X, Y);<br>&nbsp; &nbsp; if (CellHit.X &gt;= FixedCols) and (CellHit.Y &gt;= FixedRows) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if dgEditing in Options then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if (CellHit.X = Col) and (CellHit.Y = Row) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowEditor<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Col := CellHit.X;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Row := CellHit.Y;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowEditor;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; Click;<br>&nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Col := CellHit.X;<br>&nbsp; &nbsp; &nbsp; &nbsp; Row := CellHit.Y;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end<br>&nbsp; end;<br>end;<br><br>end.
<br><br>这个控件已经得到验证,没有问题,可以达到"单元格焦点随鼠标指针的移动而移动"的目的
 
好,等在下周一试过之后即行结贴<br>在此多谢各位了
 
to x900<br>试了阁下的方法<br>现象是:当鼠标移动,蓝色焦点亦移动,但DBGRID的数据指针还在原记录行,而且蓝色<br>焦点进入哪个单元格就会用数据指针所指记录的对应字段值替换蓝色焦点所在行的<br>所在单元格的值,离开时又恢复原状。<br><br>
 
to hnlygtjj<br>试了阁下的方法,是正确的,在结贴之前还请阁下继续关注此问题。<br>即是否存在比循环更具效率的途径?
 
改了一下,用FocusCell或MoveColRow方法可以做到正确移动蓝色焦点(数据指针仍不移动,不过这样也可以了,点击后再移动数据指针可能更合适)<br><br>问题是:此时鼠标点击后无任何效果,不象通常情况那样使所点击记录成为当前记录并使所点击单元格成为当前单元格。<br><br>请有兴趣的富翁关注!<br><br>代码更改列示:<br>.......<br>&nbsp; if FGridState=gsNormal then<br>&nbsp; begin<br>&nbsp; &nbsp; CellHit := MouseCoord(X, Y);<br>&nbsp; &nbsp; if (CellHit.X &gt;= FixedCols) and (CellHit.Y &gt;= FixedRows) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if dgEditing in Options then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if (CellHit.X = Col) and (CellHit.Y = Row) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FocusCell(CellHit.x,CellHit.Y,False);<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MoveColRow(CellHit.x,CellHit.Y,False,true);<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShowEditor;<br>&nbsp; &nbsp; &nbsp; &nbsp; end &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FocusCell(CellHit.x,CellHit.Y,False);<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MoveColRow(CellHit.x,CellHit.Y,False,true);<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Col := CellHit.X;<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Row := CellHit.Y;<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShowEditor;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>// &nbsp; &nbsp; &nbsp; &nbsp;Click;<br>&nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; FocusCell(CellHit.x,CellHit.Y,False);<br>// &nbsp; &nbsp; &nbsp; &nbsp;MoveColRow(CellHit.x,CellHit.Y,False,true);<br>// &nbsp; &nbsp; &nbsp; &nbsp;Col := CellHit.X;<br>// &nbsp; &nbsp; &nbsp; &nbsp;Row := CellHit.Y;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end<br>&nbsp; end;<br>
 
To:kaithink 下面的代码不存在你说的问题了<br><br>
代码:
procedure TDBGrid1.MouseMove(Shift: TShiftState; X, Y: Integer);<br>var<br>&nbsp; CellHit: TGridCoord;<br>begin<br>&nbsp; inherited MouseMove(Shift,X,Y);<br>&nbsp; if FGridState=gsNormal then<br>&nbsp; begin<br>&nbsp; &nbsp; CellHit := MouseCoord(X, Y);<br>&nbsp; &nbsp; if DataLink.Active then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if (CellHit.X &gt;= FixedCols) and (CellHit.Y &gt;= FixedRows) then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if dgEditing in Options then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (CellHit.X = Col) and (CellHit.Y = Row) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowEditor<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Col := CellHit.X;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Datalink.DataSet.MoveBy(CellHit.Y - Row);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Row := CellHit.Y;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HighlightCell(Col,Row,'',[]);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Click;<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Col := CellHit.X;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Datalink.DataSet.MoveBy(CellHit.Y - Row);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Row := CellHit.Y;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;
 
procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word;<br>&nbsp; Shift: TShiftState);<br>begin<br>if Key = 13 then with TDbgrid(ActiveControl) do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if Selectedindex&lt;(FieldCount-1)then<br>&nbsp; &nbsp; &nbsp; &nbsp; Selectedindex:=Selectedindex+1<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; dbgrid1.DataSource.DataSet.Next;<br>&nbsp; &nbsp; &nbsp; &nbsp; if dbgrid1.DataSource.DataSet.Eof then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dbgrid1.DataSource.DataSet.First ;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; Selectedindex:=0;<br>&nbsp; &nbsp; &nbsp; end ;<br>&nbsp; &nbsp; &nbsp; end;<br>end;<br><br>这个完全可以满足你的要求,收分!
 
To:Carson_zzd<br>老大,你处理的是键盘事件吧,好像与鼠标无关啊![:D]
 
To : x900<br>果然<br>阁下对VCL很熟悉啊<br><br>
 
后退
顶部