如何获得光标位置(不是鼠标位置),在线等待 (嫌分少可以加,说一声就行) (4分)

  • 主题发起人 主题发起人 delp
  • 开始时间 开始时间
Edit1.SelStart:=length(Edit1.Text) div 2;<br>SelStart就是光标的位置。
 
FT<br>哪里的光标?
 
抄来的,可能对你有用;<br>Delphi中Memo组件的光标定位 <br><br>罗起 <br>  作为一个与VisualBasic相类似,但功能更强大的可<br>视化程序开发工具,Delphi同样提供了大量的简单便捷的<br>控制组件,通过这些组件,程序设计者可以设计出规范美<br>观的界面。并且规范化使用者的输入内容。其中,Delphi<br>提供的编辑组件中的Memo组件的功能是非常丰富的。它可<br>以完成对文字的编辑、修改、文件的调入和存储文件等功<br>能,但是与一般的字处理软件相比,它并未提供编辑时对<br>光标位置、所在行及所在列的信息,这不能说不是一个缺<br>憾。其实,通过很简单的几行程序就可以实现这一要求:<br><br>  先在Form上放置三个Label组件,并在程序代码的前<br>面声明下面这三个变量为整型数,即:<br><br>  VarLpos,Cpos,Linelength:integer;<br><br>  其中Lpos是行的值(按照Memo的规定,第一行为0)<br>,Cpos是字符的位置,LineLength是当前行的字符总个数<br>。<br><br>  然后,再将下面的七行程序代码分别加入Memo1的OnM<br>ouseDown和OnKeyDown的事件代码处即可。<br><br>  Lops:ΚSendMessage(memol.Handle,EM—LINEFR<br>OM?CHAR,Memol.SelStart,0);<br><br>  Cpos:ΚSendMessage(meno1.Handle,EM—LINEIN<br>DEX,Lpos,0);<br><br>  LineLength:ΚSendMessage(memol.handle,EM—<br>LINELENGTH,Cpos,0);<br><br>  Cpos:ΚMemol.SelStart-CPos;<br><br>  Labell.caption:Κinttostr(lpos);<br><br>  Label2.caption:Κinttostr(cpos);<br><br>  Label3.caption:Κinttostr(linelength);<br><br>  这样,执行程序后,随着在Memo组件中的编辑操作,<br>Label1、Label2和Label3就将显示出相对应的当前行值、<br>字符位置与当前行的字符总个数了
 
兄弟们,就是 输入 时候的光标呀 , Edit Memo DBGrid DBEdit <br>包括大富翁的回帖框不都有光标吗? 我只是象做一个 光标跟随的程序<br>
 
GetCaretPos(lpPoint);<br>
 
to hfghfghfg,<br>正在测试中,请等等....
 
to hfghfghfg<br><br>痛苦,在 DBGrid 里面很糟糕,<br>GetCaretPos(lpPoint); 是已当前控件为起始<br>但是在 DBGrid 里面,我怎么知道当前的 Select Col 的位置呢?<br><br>如果嫌分少可以加<br>
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, Grids, DBGrids, DB, ADODB, ExtCtrls;<br>type<br>&nbsp; TmyGrid = class(TCustomGrid);<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; DBGrid1: TDBGrid;<br><br>&nbsp; &nbsp; ADODataSet1: TADODataSet;<br>&nbsp; &nbsp; DataSource1: TDataSource;<br>&nbsp; &nbsp; ADODataSet1a: TStringField;<br>&nbsp; &nbsp; Panel1: TPanel;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure FormKeyPress(Sender: TObject; var Key: Char);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; ADODataSet1.CreateDataSet;<br>&nbsp; ADODataSet1.Open;<br>&nbsp; ADODataSet1.InsertRecord(['111111111']);<br>&nbsp; ADODataSet1.InsertRecord(['111111111']);<br>&nbsp; ADODataSet1.InsertRecord(['111111111']);<br>&nbsp; ADODataSet1.InsertRecord(['111111111']);<br>&nbsp; ADODataSet1.InsertRecord(['111111111']);<br>&nbsp; ADODataSet1.InsertRecord(['111111111']);<br>end;<br><br>procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);<br><br>var<br>&nbsp; lpPoint: TPoint;<br>begin<br>&nbsp; GetCaretPos(lpPoint);<br><br>&nbsp; Panel1.Parent := self.ActiveControl;<br>&nbsp; Panel1.Top := TmyGrid(DBGrid1).InplaceEditor.top +TmyGrid(DBGrid1).InplaceEditor.Height+ lpPoint.y;<br>&nbsp; Panel1.Left := lpPoint.X;<br>end;<br><br>end.<br><br><br>object Form1: TForm1<br>&nbsp; Left = 192<br>&nbsp; Top = 107<br>&nbsp; Width = 696<br>&nbsp; Height = 480<br>&nbsp; Caption = 'Form1'<br>&nbsp; Color = clBtnFace<br>&nbsp; Font.Charset = DEFAULT_CHARSET<br>&nbsp; Font.Color = clWindowText<br>&nbsp; Font.Height = -11<br>&nbsp; Font.Name = 'MS Sans Serif'<br>&nbsp; Font.Style = []<br>&nbsp; KeyPreview = True<br>&nbsp; OldCreateOrder = False<br>&nbsp; OnCreate = FormCreate<br>&nbsp; OnKeyPress = FormKeyPress<br>&nbsp; PixelsPerInch = 96<br>&nbsp; TextHeight = 13<br>&nbsp; object DBGrid1: TDBGrid<br>&nbsp; &nbsp; Left = 192<br>&nbsp; &nbsp; Top = 184<br>&nbsp; &nbsp; Width = 320<br>&nbsp; &nbsp; Height = 120<br>&nbsp; &nbsp; DataSource = DataSource1<br>&nbsp; &nbsp; TabOrder = 0<br>&nbsp; &nbsp; TitleFont.Charset = DEFAULT_CHARSET<br>&nbsp; &nbsp; TitleFont.Color = clWindowText<br>&nbsp; &nbsp; TitleFont.Height = -11<br>&nbsp; &nbsp; TitleFont.Name = 'MS Sans Serif'<br>&nbsp; &nbsp; TitleFont.Style = []<br>&nbsp; end<br>&nbsp; object Panel1: TPanel<br>&nbsp; &nbsp; Left = 384<br>&nbsp; &nbsp; Top = 328<br>&nbsp; &nbsp; Width = 185<br>&nbsp; &nbsp; Height = 41<br>&nbsp; &nbsp; Caption = 'Panel1'<br>&nbsp; &nbsp; Color = clGradientActiveCaption<br>&nbsp; &nbsp; TabOrder = 1<br>&nbsp; end<br>&nbsp; object ADODataSet1: TADODataSet<br>&nbsp; &nbsp; Parameters = &lt;&gt;<br>&nbsp; &nbsp; Left = 184<br>&nbsp; &nbsp; Top = 88<br>&nbsp; &nbsp; object ADODataSet1a: TStringField<br>&nbsp; &nbsp; &nbsp; FieldName = 'a'<br>&nbsp; &nbsp; &nbsp; Size = 23<br>&nbsp; &nbsp; end<br>&nbsp; end<br>&nbsp; object DataSource1: TDataSource<br>&nbsp; &nbsp; DataSet = ADODataSet1<br>&nbsp; &nbsp; Left = 264<br>&nbsp; &nbsp; Top = 96<br>&nbsp; end<br>end<br>
 
var<br>&nbsp;lpPoint: TPoint;<br>begin<br>&nbsp;GetCaretPos(lpPoint);<br><br>&nbsp;Panel1.Parent := self.ActiveControl;<br>&nbsp;Panel1.Top := TmyGrid(DBGrid1).InplaceEditor.top +TmyGrid(DBGrid1).InplaceEditor.Height+ lpPoint.y;<br>&nbsp;Panel1.Left := lpPoint.X;<br>
 
我也喜欢星际争霸
 
to hfghfghfg &nbsp;改进如下:<br><br>&nbsp; GetCaretPos(lpPoint);<br><br>&nbsp; Panel1.Parent := self.ActiveControl;<br>&nbsp; if TmyGrid(DBGrid1).InplaceEditor &lt;&gt; nil then<br>&nbsp; begin<br>&nbsp; &nbsp; Panel1.Top := TmyGrid(DBGrid1).InplaceEditor.top + lpPoint.y;<br>&nbsp; &nbsp; Panel1.Left := TmyGrid(DBGrid1).InplaceEditor.Left + lpPoint.X;<br>&nbsp; end;<br><br>兄谢谢
 
后退
顶部