Advertisement
TLama

Untitled

Mar 17th, 2015
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.08 KB | None | 0 0
  1. type
  2.   TRichEditHelper = class helper for TRichEdit
  3.   strict private
  4.     class function EditStreamCallback(dwCookie: DWORD_PTR; pbBuff: PByte; CB: Longint; var pCB: Longint): Longint; stdcall; static;
  5.   public
  6.     function GetSelectionAsRTF: string; virtual;
  7.   end;
  8.  
  9. implementation
  10.  
  11. { TRichEditHelper }
  12.  
  13. class function TRichEditHelper.EditStreamCallback(dwCookie: DWORD_PTR; pbBuff: PByte; CB: Longint; var pCB: Longint): Longint; stdcall;
  14. begin
  15.   // casting here to TStream makes the method more flexible because you can pass any kind of TStream implementation
  16.   // and so to use this callback for other functions using EM_STREAMOUT message passing streams
  17.   pCB := TStream(dwCookie).Write(pbBuff^, CB);
  18.   Result := IfThen(CB = pCB, 0, 1);
  19. end;
  20.  
  21. function TRichEditHelper.GetSelectionAsRTF: string;
  22. var
  23.   EditStream: TEditStream;
  24.   StringStream: TStringStream;
  25. begin
  26.   StringStream := TStringStream.Create;
  27.   try
  28.     EditStream.dwError := 0;
  29.     EditStream.dwCookie := DWORD_PTR(StringStream);
  30.     EditStream.pfnCallback := EditStreamCallback;
  31.     Perform(EM_STREAMOUT, SF_RTF or SFF_SELECTION, LPARAM(@EditStream));
  32.     Result := StringStream.DataString;
  33.   finally
  34.     StringStream.Free;
  35.   end;
  36. end;
  37.  
  38. // your TRichEdit instances gets the GetRTF helper method
  39. procedure TForm1.Button1Click(Sender: TObject);
  40. begin
  41.   ShowMessage(RichEdit1.GetSelectionAsRTF);
  42. end;
  43.  
  44. // another way of abstraction
  45. type
  46.   TRichEditHelper = class helper for TRichEdit
  47.   strict private
  48.     function TryGetStream(Stream: TStream; Flags: DWORD): Boolean; inline;
  49.     class function EditStreamCallback(dwCookie: DWORD_PTR; pbBuff: PByte; CB: Longint; var pCB: Longint): Longint; stdcall; static;
  50.   strict protected
  51.     function GetContentAsRTF: string;
  52.     function GetSelectionAsRTF: string;
  53.   public
  54.     property ContentAsRTF: string read GetContentAsRTF;
  55.     property SelectionAsRTF: string read GetSelectionAsRTF;
  56.   end;
  57.  
  58. implementation
  59.  
  60. { TRichEditHelper }
  61.  
  62. class function TRichEditHelper.EditStreamCallback(dwCookie: DWORD_PTR; pbBuff: PByte; CB: Longint; var pCB: Longint): Longint; stdcall;
  63. begin
  64.   pcb := TStream(dwCookie).Write(pbBuff^, CB);
  65.   Result := IfThen(CB = pcb, 0, 1);
  66. end;
  67.  
  68. function TRichEditHelper.TryGetStream(Stream: TStream; Flags: DWORD): Boolean;
  69. var
  70.   EditStream: TEditStream;
  71. begin
  72.   EditStream.dwCookie := DWORD_PTR(Stream);
  73.   EditStream.dwError := 0;
  74.   EditStream.pfnCallback := EditStreamCallback;
  75.   Result := (Perform(EM_STREAMOUT, WPARAM(Flags), LPARAM(@EditStream)) > 0) and (EditStream.dwError = 0);
  76. end;
  77.  
  78. function TRichEditHelper.GetContentAsRTF: string;
  79. var
  80.   Stream: TStringStream;
  81. begin
  82.   Result := '';
  83.   Stream := TStringStream.Create;
  84.   try
  85.     if TryGetStream(Stream, SF_RTF) then
  86.       Result := Stream.DataString;
  87.   finally
  88.     Stream.Free;
  89.   end;
  90. end;
  91.  
  92. function TRichEditHelper.GetSelectionAsRTF: string;
  93. var
  94.   Stream: TStringStream;
  95. begin
  96.   Result := '';
  97.   Stream := TStringStream.Create;
  98.   try
  99.     if TryGetStream(Stream, SF_RTF or SFF_SELECTION) then
  100.       Result := Stream.DataString;
  101.   finally
  102.     Stream.Free;
  103.   end;
  104. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement