Delphi操作RichEdit总结

uses RichEdit;1. 设置RichEdit的行间距,代码如下Delphi/Pascal12345678910111213141516171819202122procedure TForm1.Button3Click(Sender: TObject);var pf: PARAFORMAT2

uses RichEdit;

1. 设置RichEdit的行间距,代码如下


Delphi/Pascal

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

procedure TForm1.Button3Click(Sender: TObject);

var

  pf: PARAFORMAT2;

begin

  FillChar(pf, sizeof(paraformat2), #0);

  pf.cbSize := SizeOf(paraformat2);

  pf.dwMask := PFM_LINESPACING  ;   //需要设置上 PFM_LINESPACING 标志,bLineSpacingRule和dyLineSpacing才可能有效

 

//  pf.bLineSpacingRule := 0;   //单倍行距,dyLineSpacing的值将被忽略

//  pf.bLineSpacingRule := 1;   //1.5倍行距,dyLineSpacing的值将被忽略

//  pf.bLineSpacingRule := 2;   //两倍行距,dyLineSpacing的值将被忽略

//  pf.bLineSpacingRule := 3;   //用dyLineSpacing以缇为单位指定行间距,当此值小于单倍行距时,效果为单倍行距

//  pf.bLineSpacingRule := 5;   //用dyLineSpacing/20指定行间距

 

  pf.bLineSpacingRule := 4;   //用dyLineSpacing以缇为单位指定行间距

  pf.dyLineSpacing := RichEdit1.Font.Size * 20 + 20 * 4; //这是笔者大概计算的,可以根据字体大小调节的,行间最小距离,大字体时可能出现上下行重叠,可以设置为300或者自己计算

 

  RichEdit1.SelectAll;   //只对选择的文本有效,***重要***

  SendMessage(RichEdit1.Handle, EM_SETPARAFORMAT, 0, LPARAM(@pf));

  RichEdit1.SelStart := 0;

  RichEdit1.SelLength := 0;

end;


2. 为RichEdit设置行号,代码如下:


Delphi/Pascal

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

procedure TForm1.Button4Click(Sender: TObject);

const

  PFNS_PAREN = $000;  //e.g. 1)

  PFNS_PARENS = $100;  //e.g. (1)

  PFNS_PERIOD = $200;  //e.g. 1.

  PFNS_PLAIN = $300;

  PFNS_NONUMBER = $400;

 

const

  PFN_NONE     = $00000000;  //无

  PFN_BULLET   = $00000001;  //黑色实心圆点

  PFN_ARABIC   = $00000002;  //0,1,2

  PFN_LCLETTER = $00000003;  //a,b,c

  PFN_UCLETTER = $00000004;  //A,B,C

  PFN_LCROMAN  = $00000005;  //i,ii,iii

  PFN_UCROMAN  = $00000006;  //I,II,III

 

  var

  pf: PARAFORMAT2;

begin

  FillChar(pf, sizeof(paraformat2), #0);

  pf.cbSize := SizeOf(paraformat2);

  //PFM_NUMBERING: wNumbering 值有效

  //PFM_NUMBERINGSTYLE: wNumberingStyle值有效

  //PFM_NUMBERINGSTART: wNumberingStart值有效

  //PFM_STARTINDENT: dxStartIndent值有效

  pf.dwMask := PFM_NUMBERING or PFM_NUMBERINGSTYLE or PFM_NUMBERINGSTART or PFM_STARTINDENT;//or PFM_OFFSET;

 

  pf.wNumberingStyle := PFNS_PERIOD;  //设置行号的样式,可以为“)”,“.”,“()”

  pf.wNumberingStart := 1;            //设置行号起始值

  pf.wNumbering := PFN_ARABIC;        //设置行号的格式,可以为阿拉伯数字或者英文字母等格式

  pf.dxStartIndent := 60;             //设置行首缩进值

 

  RichEdit1.SelectAll;

  SendMessage(RichEdit1.Handle, EM_SETPARAFORMAT, 0, LPARAM(@pf));

  RichEdit1.SelStart := 0;

  RichEdit1.SelLength := 0;

end;


3. 根据鼠标位置定位光标的方法。

当RichEdit为可用状态时,是不用代码控制此操作的,TRichEdit本身就可以定位光标。但是当RichEdit开始不可用时,则此方法就可能用到了。本例中RichEdit1开始时是不可用的,当在ApplicationEvents1中接收到RichEdit1被双击时则设置RichEdit1的Enable := true;并且定位光标。

Delphi/Pascal

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

unit Unit1;

 

interface

 

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, ComCtrls, RichEdit, AppEvnts;

 

type

  TForm1 = class(TForm)

    RichEdit1: TRichEdit;

    ApplicationEvents1: TApplicationEvents;

    procedure EnableRichEdit;

    procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);

  private

    { Private declarations }

  public

    { Public declarations }

  end;

 

var

  Form1: TForm1;

  FLastMousePos: TPoint;

 

implementation

 

{$R *.dfm}

 

procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;

  var Handled: Boolean);

var

  mPos: TPoint;

begin

  if msg.message = WM_LBUTTONDBLCLK then begin

    if msg.hwnd = RichEdit1.Parent.Handle then begin

      GetCursorPos(FLastMousePos);

      EnableRichEdit;

    end;

  end ;

end;

 

procedure TForm1.EnableRichEdit;

var

  cPos: TPoint;

  retCode: integer;

  mR, mC: Word;

begin

  RichEdit1.Enabled := True;

  RichEdit1.SetFocus ;

  RichEdit1.SelStart := 0;

 

  if (FLastMousePos.X = 0) and (FLastMousePos.Y = 0) then

    GetCursorPos(FLastMousePos);

  FLastMousePos := RichEdit1.ScreenToClient(FLastMousePos);

  retCode := SendMessage(RichEdit1.Handle, EM_CHARFROMPOS, 0, LPARAM(@FLastMousePos));

  mR := HiWord(retCode);

  mC := Loword(retCode);

  cPos.X := mC ;

  cPos.Y := mR ;

  RichEdit1.CaretPos := cPos;

 

  FLastMousePos.X := 0;

  FLastMousePos.Y := 0;

end;

 

end.


4. 使Richedit中的链接可以点击


Delphi/Pascal

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

unit Unit1;

 

interface

 

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, ComCtrls, RichEdit, ShellAPI;

 

type

  TForm1 = class(TForm)

    RichEdit1: TRichEdit;

    procedure FormCreate(Sender: TObject);

  private

    { Private declarations }

  public

    procedure WndProc(var Msg: TMessage); override;

  end;

 

var

  Form1: TForm1;

 

implementation

 

{$R *.dfm}

 

procedure TForm1.FormCreate(Sender: TObject);

var

  mask: Word;

begin

  mask := SendMessage(RichEdit1.Handle, EM_GETEVENTMASK, 0, 0);

  SendMessage(RichEdit1.Handle, EM_SETEVENTMASK, 0, mask or ENM_LINK);

  SendMessage(RichEdit1.Handle, EM_AUTOURLDETECT, Integer(True), 0);

end;

 

procedure TForm1.WndProc(var Msg: TMessage);

var

  p: TENLink;

  sURL: string;

  CE : TRichEdit;

begin

  if (Msg.Msg = WM_NOTIFY) then

  begin

    if (PNMHDR(Msg.lParam).code = EN_LINK) then

    begin

      p := TENLink(Pointer(TWMNotify(Msg).NMHdr)^);

      if (p.Msg = WM_LBUTTONDOWN) then

      begin

        try

          CE := tRichEdit(Self.ActiveControl);

          SendMessage(CE.Handle, EM_EXSETSEL, 0, Longint(@(p.chrg)));

          sURL := CE.SelText;

          ShellExecute(0, 'open', PChar(sURL), 0, 0, SW_SHOWNORMAL);

        except

        end;

      end;

    end;

  end;

  inherited;

end;

 

end.

-------------------------------

1.选择设置对齐

RichEdit1.SelectAll;
RichEdit1.Paragraph.Alignment:=taLeftJustify; // switch for other alignments
RichEdit1.SelLength:=0;

2.设置对齐和内容

redt1.Clear;

redt1.Lines.Add('XXXX分析报告' + #13#10);
strMsg := '打印时间:' + FormatDateTime('yyyy年hh月dd日 hh时MM分ss秒', Now);
redt1.Lines.Add(strMsg);
strMsg := '打印时间:' + FormatDateTime('yyyy年hh月dd日 hh时MM分ss秒', Now);
redt1.Lines.Add(strMsg);
strMsg := '进样时间:' + FormatDateTime('yyyy年hh月dd日 hh时MM分ss秒', Now);
redt1.Lines.Add(strMsg);
strMsg := '质检(E)字第()号';
redt1.Lines.Add(strMsg);
strMsg := '送样单位:' + 'XXX公司' + '仪器型号:' + 'SC-1001-09A';
redt1.Lines.Add(strMsg);
strMsg := '取样日期:' + FormatDateTime('yyyy年hh月dd日 ', Now) + '收样日期:' + FormatDateTime('yyyy年hh月dd日 ', Now);
redt1.Lines.Add(strMsg);
strMsg := '样品批号:' + '' + '样品名称:' + '固液';
redt1.Lines.Add(strMsg);
strMsg := '样品罐号:' + 'A-1-2';
redt1.Lines.Add(strMsg);
strMsg := '仪器文件控制参数:' + '#1111';
redt1.Lines.Add(strMsg);
strMsg := '';
redt1.Lines.Add(strMsg);
strMsg := 'C:\AAAAAAA\111.bmp';
redt1.Lines.Add(strMsg);
redt1.SelStart := 4;
redt1.Paragraph.Alignment := taCenter;

3.改变字体大小

//改变第二行的字体大小为15
RichEdit1.SelStart := Length(RichEdit1.Lines[0]);
RichEdit1.SelLength := Length(RichEdit1.Lines[1]) + 2;
richedit1.SelAttributes.Size := 15;

---------------------------------------

一、如何得知当前行号
  用RichEdit(或者memo)控件制作文本编辑器时,通过访问lines?count属性可以得到总行数,但是若想知道光标当前所在行的行号就麻烦了,因为delphi没有提供这个属性。要实现这个编辑器必备功能,就须调用em_ LineFromChar。请试试下面的程序。
  先在窗口中布置一个RichEdit或者memo(命名为editor),以及一个button。在button的onclick事件中写入下列代码。
  var
  CurrentLine:Integer;
  begin
  CurrentLine:=Editor?Perform(em_ LineFromChar,SFFFF,0);
  Application?MessageBox(PChar(′当前行号是′+IntToStr(CurrentLine)),′消息′,mb_ iconinformation);
  end;
  需要注意的是,第一行的行号为零。
  二、如何撤消操作(undo)
  对于memo来说,实现undo是不需编程的,只要让popupmenu属性为空,运行时就能用鼠标右键激活一个常用操作菜单,其中包括撤消、剪切、复制、粘贴、删除和全选六项。
  但可惜的是,这一招对于功能强大的RichEdit控件居然行不通,害得我们还要自己设计一个popupmemu。当你用CutToClipBoard等语句轻松而顺利地完成了“剪切”等功能,接着便会无奈地发现,竟找不到undo或cancel之类的语句来执行“撤消”。
  这时你需要这样处理:
  RichEdit1?Perform(EM_UNDO,0,0);
  另外还应检查是否允许撤消,从而开启或关闭弹出菜单中的“撤消”项:
  Undo1?Enabled:=RichEdit?Perform(EM_CANUNDO,0,0)<>0;

-------------------------

实现RichEdit中的整行选中


procedure TForm1.RichEdit1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
a:integer;
begin
a:=RichEdit1.Perform(EM_LINELENGTH,RichEdit1.SelStart,0);
RichEdit1.SelStart:=RichEdit1.Perform(EM_LINEINDEX,RichEdit1.CaretPos.Y,0);
RichEdit1.SelLength:=a;
end;
end.






相关推荐

[!--temp.pl--]