在delphi memo第一行怎么显示指针定位的那一行
1个回答
展开全部
在窗体form1中放置 edit1 和 memo1 两个控件。目的是让edit1中总是显示memo1中当前行的内容。让memo1的 click、dblclick、enter 以及 keyup 事件 的代码都和 change 的事件代码相同(见上图)。
单元的代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Edit1: TEdit;
procedure Memo1Change(Sender: TObject);
procedure Memo1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Memo1Change(Sender: TObject);
var
posy,posx,linelen:integer;
begin
posy:=sendmessage(memo1.Handle,EM_LINEFROMCHAR,memo1.SelStart,0);
posx:=sendmessage(memo1.Handle,EM_LINEFROMCHAR,posy,0);
linelen:=sendmessage(memo1.Handle,EM_LINEFROMCHAR,posx,0);
edit1.Text:=memo1.Lines.Strings[posy];
end;
procedure TForm1.Memo1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
posy,posx,linelen:integer;
begin
posy:=sendmessage(memo1.Handle,EM_LINEFROMCHAR,memo1.SelStart,0);
posx:=sendmessage(memo1.Handle,EM_LINEFROMCHAR,posy,0);
linelen:=sendmessage(memo1.Handle,EM_LINEFROMCHAR,posx,0);
edit1.Text:=memo1.Lines.Strings[posy];
end;
end.
进一步优化的单元代码为:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Edit1: TEdit;
procedure Memo1Change(Sender: TObject);
procedure Memo1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Memo1Change(Sender: TObject);
begin
edit1.Text:=memo1.Lines.Strings[memo1.CaretPos.Y];
end;
procedure TForm1.Memo1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
edit1.Text:=memo1.Lines.Strings[memo1.CaretPos.Y];
end;
end.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询