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.

Delphi RxRichEdit高级操作的更多相关文章

  1. delphi的流操作的语法

    Delphi在这两方面都做的相当出色.在Delphi的早期版本Turbo Pascal 中就曾有流(Stream).群(Collection)和资源(Resource)等专门用于对象式数据管理的类.在 ...

  2. [Session] SessionHelper2---C#关于Session高级操作帮助类 (转载)

    点击下载 SessionHelper2.rar 这个类是关于Session的一些高级操作1.添加时限制时间2.读取对象3.读取数据等等看下面代码吧 /// <summary> /// 联系 ...

  3. cassandra高级操作之索引、排序以及分页

    本次就给大家讲讲cassandra的高级操作:索引.排序和分页:处于性能的考虑,cassandra对这些支持都比较简单,所以我们不能希望cassandra完全适用于我们的逻辑,而是应该将我们的逻辑设计 ...

  4. MySQL学习笔记_9_MySQL高级操作(上)

    MySQL高级操作(上) 一.MySQL表复制 create table t2 like t1;               #复制表结构,t2可以学习到t1所有的表结构 insert into t2 ...

  5. MySQL学习笔记_10_MySQL高级操作(下)

    MySQL高级操作(下) 五.MySQL预处理语句 1.设置预处理stmt,传递一个数据作为where的判断条件 prepare stmt from "select * from table ...

  6. python列表(list)的使用技巧及高级操作

    python列表(list)的使用技巧及高级操作置顶 2018年03月25日 13:39:41 顽劣的石头 阅读数:5478 标签: python extend bisect list enumera ...

  7. C语言指针的高级操作

    C语言指针的高级操作 指针  指针 在上篇博客中我介绍了C语言指针的最基本操作,那么我在这篇博客中会介绍一下C语言指针的一些骚操作. 指向指针的指针 这名字乍一听有点拗口,再次一听就更加拗口了.先看定 ...

  8. django之创建第8-3个项目-数据库数据提取之高级操作

    1.配置test2.html <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  9. SpringMVC整合Mongodb开发,高级操作

    开发环境: 操作系统:windows xpMongodb:2.0.6依 赖 包:Spring3.2.2 + spring-data-mongodb-1.3.0 + Spring-data-1.5 +  ...

随机推荐

  1. Java 类加载器(转)

    java虚拟机中可以安装多个类加载,系统默认三个主要类加载器,每个类负责加载特定位置的类:BootStrap(内嵌在java虚拟机中由C++编写),ExtClassLoader,AppClassLoa ...

  2. docker基本概念,创建、起动实例,保存自定义镜像等常用操作

    14年docker火了一阵,当时自学整理了一份文档,后来冷落了. 现在发现很多同事还是想学习docker,但无从下手,所以重新整理了这篇分享,10分钟就可以带你彻底理解docker,并能够创建属于自己 ...

  3. USACO Section 2.4: The Tamworth Two

    这题我是用蒙的方法来弄出最后的不能碰到的条件的(用1000试了下account跳出条件),结果竟然还过了,不过网上有精准的求出这个碰不到的条件,farm的状态为10*10*4 = 400,cow的状态 ...

  4. html5 getComputedStyle + resize 实现动态宽高度等比响应式页面设计

    序:通常我们只能控制div的宽度 而不能控制高度,在响应式页面里 如果要这个div是正方形那么必须的用媒体查询在不同的分辨率下写死宽高度 今天突发奇想研究了个 用百分比来动态控制div的高度让其与宽度 ...

  5. android backlight

    背光设置是在:设置->声音和显示->亮度,通过进度条来设置的. 文件:packages/apps/Settings/src/com/android/settings/BrightnessP ...

  6. Evaluate Reverse Polish Notation(堆栈)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  7. 《OD学Sqoop》数据转换工具Sqoop

    一. 第二阶段课程回顾 hadoop 2.x HDFS YARN MapReduce Zookeeper Hive 二.大数据协作框架 对日志类型的海量数据进行分析 hdfs mapreduce/hi ...

  8. 爬虫技术(四)-- 简单爬虫抓取示例(附c#代码)

    这是我的第一个爬虫代码...算是一份测试版的代码.大牛大神别喷... 通过给定一个初始的地址startPiont然后对网页进行捕捉,然后通过正则表达式对网址进行匹配. List<string&g ...

  9. HTML+CSS+JAVASCRIPT 总结

    1. HTML 1: <!doctype html> 2: <!-- This is a test html for html, css, javascript --> 3: ...

  10. 如何扩展VCL的hint

    默认的Hint窗口展现如下: 这种情况下可以操作有窗口的背景颜色,字体样式 Application.Color 有的时候仅仅是文字满足不了我们的需求,比例如下格式: 这个时候就应该执行以下步骤: 1. ...