• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

DelphiRxRichEdit高级操作

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

unit InsertRichEditUnit;

interface

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

type
  TEditStreamCallBack = function(dwCookie: Longint; pbBuff: PByte; cb: Longint;
    var pcb: Longint): DWORD; stdcall;

  TEditStream = record
    dwCookie: Longint;
    dwError: Longint;
    pfnCallback: TEditStreamCallBack;
  end;

procedure GetRTFSelection(aRichEdit: TUHISRichEdit; IntoStream: TStream);
procedure PutRTFSelection(aRichEdit: TUHISRichEdit; SourceStream: TStream);
procedure InsertRTF(aRichEdit: TUHISRichEdit; S: string);
procedure CopyRTF(aSource, aDest: TUHISRichEdit);
procedure CopyAllRTF(aSource, aDest: TUHISRichEdit);
procedure AppendRTF(aRichEdit: TUHISRichEdit; S: string);

implementation

function EditStreamInCallback(dwCookie: Longint; pbBuff: PByte; cb: Longint;
  var pcb: Longint): DWORD; stdcall;
var
  TheStream: TStream;
  DataAvail: LongInt;
begin
  TheStream := TStream(dwCookie);
  with TheStream do
  begin
    DataAvail := Size - Position;
    Result := 0;
    if DataAvail <= cb then
    begin
      pcb := Read(pbBuff^, DataAvail);
      if pcb <> DataAvail then
        result := DWord(E_FAIL);
    end
    else
    begin
      pcb := Read(pbBuff^, cb);
      if pcb <> cb then
        result := DWord(E_FAIL);
    end;
  end;
  TheStream := TStream(dwCookie);
end;

function EditStreamOutCallback(dwCookie: Longint; pbBuff: PByte; cb: Longint;
  var pcb: Longint): DWORD; stdcall;
var
  TheStream: TStream;
begin
  TheStream := TStream(dwCookie);
  with TheStream do
  begin
    if cb > 0 then
      pcb := Write(pbBuff^, cb);
    Result := 0;
  end;
end;

procedure GetRTFSelection(aRichEdit: TUHISRichEdit; IntoStream: TStream);
var
  EditStream: TEditStream;
begin
  with EditStream do
  begin
    dwCookie := Longint(IntoStream);
    dwError := 0;
    pfnCallback := EditStreamOutCallBack;
  end;
  aRichEdit.Perform(EM_STREAMOUT, SF_RTF or SFF_SELECTION, longint(@EditStream));
end;

procedure PutRTFSelection(aRichEdit: TUHISRichEdit; SourceStream: TStream);
var
  EditStream: TEditStream;
begin
  with EditStream do
  begin
    dwCookie := Longint(SourceStream);
    dwError := 0;
    pfnCallback := EditStreamInCallBack;
  end;
  aRichEdit.Perform(EM_STREAMIN, SF_RTF or SFF_SELECTION, longint(@EditStream));
end;

procedure InsertRTF(aRichEdit: TUHISRichEdit; S: string);
var
  aMemStream: TMemoryStream;
begin
  if Length(S) > 0 then
  begin
    aMemStream := TMemoryStream.Create;
    try
      aMemStream.Write(S[1], length(S));
      aMemStream.Position := 0;
      PutRTFSelection(aRichEdit, aMemStream);
    finally
      aMemStream.Free;
    end;
  end;
end;

procedure CopyRTF(aSource, aDest: TUHISRichEdit);
var
  aMemStream: TMemoryStream;
begin
  aMemStream := TMemoryStream.Create;
  try
    GetRTFSelection(aSource, aMemStream);
    aMemStream.Position := 0;
    PutRTFSelection(aDest, aMemStream);
  finally
    aMemStream.Free;
  end;
end;

procedure CopyAllRTF(aSource, aDest: TUHISRichEdit);
var
  aMemStream: TMemoryStream;
begin
  aMemStream := TMemoryStream.Create;
  try
    aSource.SelectAll;
    GetRTFSelection(aSource, aMemStream);
    aMemStream.Position := 0;
    aDest.SelStart := Length(aDest.Lines.Text);
    PutRTFSelection(aDest, aMemStream);
  finally
    aMemStream.Free;
  end;
end;

procedure AppendRTF(aRichEdit: TUHISRichEdit; S: string);
var
  Start, Length, EventMask: Integer;
begin
  EventMask := SendMessage(aRichEdit.Handle, EM_SETEventMask, 0, 0);
  SendMessage(aRichEdit.Handle, WM_SETREDRAW, 0, 0);
  Start := aRichEdit.SelStart;
  Length := aRichEdit.SelLength;
  aRichEdit.SelLength := 0;
  aRichEdit.SelStart := System.Length(aRichEdit.Text);
  InsertRTF(aRichEdit, s);
  aRichEdit.SelStart := Start;
  aRichEdit.SelLength := Length;
  SendMessage(aRichEdit.Handle, WM_SETREDRAW, 1, 0);
  InvalidateRect(aRichEdit.Handle, nil, True);
  SendMessage(aRichEdit.Handle, EM_SETEventMask, 0, EventMask);
end;

end.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap