如何使TMEMO的光标所在行显示高亮并能得到此被选行的行号?

jsmicrofox 2003-03-10 04:52:44
如何使TMEMO的光标所在行显示高亮并能得到此被选行的行号?

多谢!
...全文
292 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
frogshero 2003-03-10
  • 打赏
  • 举报
回复
用memo也可以《D5开发人员指南》里有,要子类化
xzgyb 2003-03-10
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TMainForm = class(TForm)
Memo1: TMemo;
Label1: TLabel;
procedure Memo1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
MainForm: TMainForm;

implementation

{$R *.dfm}

procedure TMainForm.Memo1Click(Sender: TObject);
var
CurLine: Integer;
LineCharIndex: Integer;
begin
LineCharIndex := SendMessage(Memo1.Handle, EM_LINEINDEX, -1, 0);
CurLine := SendMessage(Memo1.Handle, EM_LINEFROMCHAR, LineCharIndex, 0);
Memo1.SelStart := LineCharIndex;
Memo1.SelLength := SendMessage(Memo1.Handle, EM_LINELENGTH, LineCharIndex, 0);
Label1.Caption := IntToStr(CurLine);
end;

end.
jsmicrofox 2003-03-10
  • 打赏
  • 举报
回复
listbox怎么用?
leaf_zhy 2003-03-10
  • 打赏
  • 举报
回复
为什么不用ListBox
sysu 2003-03-10
  • 打赏
  • 举报
回复
取得行、列号,高亮的话要用TRichEdit。
var
row, col: Integer;
begin
row := SendMessage(memo1.Handle, EM_LINEFROMCHAR,
memo1.SelStart, 0);
col := memo1.SelStart -
SendMessage(memo1.Handle, EM_LINEINDEX, row, 0);
Inc(row); // 行号
Inc(col); // 列号
ShowMessage(Format('%d 行 %d列',[row, col]));
end;

用RichEdit设置高亮:
procedure TForm1.Button1Click(Sender: TObject);
var
row, i, cnt: Integer;
begin
// 取得当前行号
row := SendMessage(RichEdit1.Handle, EM_LINEFROMCHAR,
RichEdit1.SelStart, 0);
cnt := 0;
for i := 0 to row-1 do cnt := cnt + Length(RichEdit1.Lines[i])+2;
RichEdit1.SelStart := cnt;
RichEdit1.SelLength := Length(RichEdit1.Lines[row]);
RichEdit1.SelAttributes.Color := clRed; // 设为红色
end;

5,531

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧