Recent

Author Topic: CopyToClipboard  (Read 4547 times)

rick2691

  • Sr. Member
  • ****
  • Posts: 394
Re: CopyToClipboard
« Reply #15 on: October 14, 2021, 04:28:34 pm »
I have been trying to get rid of the erratic italics problem, but with no success.

So I looked in RichMemoUtils for the CopyRichText functions ...

I have questions about the following:

_CopyDirect ... variables: ofs, ln, fnt  ...do not appear to be initialized.

I see that GetTextAttributes conditionally sets ofs & ln, but I haven't noticed anything for when they are outside the condition.

I also don't see how InitFontParams is assigning anything to variable fnt, and it doesn't appear to be initialized before going to the function.

Rick
Windows 10, LAZ 2.0.12, FPC 3.2.0, SVN 64642, i386-win32-win32/win64, using windows unit

rick2691

  • Sr. Member
  • ****
  • Posts: 394
Re: CopyToClipboard
« Reply #16 on: December 23, 2021, 04:40:49 pm »
I have tried every option to make CopyRichText work for my needs. With no more response from anyone I started researching things on my own.

I found the following code on a forum. I think it was for RichEdit and Delphi, so I tweaked it.

It has two methods: GetRTFselection and PutRTFselection.

I also found some code on this forum by "typo". He used TMemoryStream, and that made everything work. Before finding that, it was not working on account of using TStringStream.

I have been testing it all, and have not found any failures yet.

Rick

Code: Pascal  [Select][+][-]
  1. //======================== begin: stream callback for Get/PutRTFselection
  2. // (this code section must precede any use of Get/PutRTFselection)
  3.  
  4. // by Peter Below (TeamB)  < 100113.1...@compuserve.com >
  5.  
  6. // Stream Callback function
  7. type
  8.   TEditStreamCallBack = function(dwCookie: Longint; pbBuff: PByte;
  9.                                  cb: Longint; var pcb: Longint
  10.                                 ): DWORD; stdcall;
  11.   TEditStream = record
  12.                 dwCookie: Longint;
  13.                 dwError: Longint;
  14.                 pfnCallback: TEditStreamCallBack;
  15.                 end;
  16.  
  17. // EditStreamOutCallback callback function
  18. function EditStreamOutCallback(dwCookie: Longint; pbBuff: PByte;
  19.                                cb: Longint; var pcb: Longint
  20.                               ): DWORD; stdcall;
  21. var theStream: TStream;
  22. begin
  23.   theStream:= TStream(dwCookie);
  24.   with theStream do
  25.        begin
  26.        If cb > 0 Then
  27.           begin
  28.           pcb:= Write(pbBuff^, cb);
  29.           Result:= pcb;
  30.           end else Result:= 0;
  31.        end;
  32. end;
  33.  
  34. // EditStreamInCallback callback function
  35. function EditStreamInCallback(dwCookie: Longint; pbBuff: PByte;
  36.                               cb: Longint; var pcb: Longint
  37.                              ): DWORD; stdcall;
  38. var
  39.   theStream: TStream;
  40.   dataAvail: LongInt;
  41. begin
  42.   theStream:= TStream(dwCookie);
  43.   with theStream do // open theStream attributes for access; ie. theStream.size
  44.        begin
  45.          dataAvail:= Size - Position; // size & position are attributes of "theStream"; ie. theStream.size
  46.          Result:= 0;
  47.          if dataAvail<=cb then
  48.             begin
  49.               pcb:= read(pbBuff^, dataAvail);
  50.               if pcb<>dataAvail then Result:= UINT(E_FAIL);
  51.             end
  52.             else
  53.             begin
  54.               pcb:= read(pbBuff^, cb);
  55.               if pcb<>cb then Result:= UINT(E_FAIL);
  56.             end;
  57.        end;
  58. end;
  59.  
  60. // copyout Stream from RichEdit: uses stream callback above
  61. Procedure GetRTFSelection(Amemo: TRichMemo; IntoStream: TStream);
  62. Var editstream: TEditStream;
  63. Begin
  64.   with editstream Do
  65.        Begin
  66.        dwCookie:= Longint(IntoStream);
  67.        dwError:= 0;
  68.        pfnCallback:= @EditStreamOutCallBack;
  69.        end;
  70.   Amemo.Perform(EM_STREAMOUT, SF_RTF or SFF_SELECTION, longint(@EditStream));
  71. End;
  72.  
  73. // Insert Stream into RichEdit: uses stream callback above
  74. procedure PutRTFSelection(Amemo: TRichMemo; SourceStream: TStream);
  75. var EditStream: TEditStream;
  76. begin
  77.   with EditStream do
  78.        begin
  79.        dwCookie:= Longint(SourceStream);
  80.        dwError:= 0;
  81.        pfnCallback:= @EditStreamInCallBack;
  82.        end;
  83.   Amemo.Perform(EM_STREAMIN, SF_RTF or SFF_SELECTION, Longint(@EditStream));
  84. end;
  85.  
  86. //=================================== end: stream callback for Get/PutRTFselection
  87.  
  88.  
  89.  
  90. //=========== begin: my application of the functions
  91.  
  92. procedure TCmdForm.CopyPageMemo;  // PageMemo to SearchBox & ReplaceBox
  93. var MS: TMemoryStream; { TMemoryStream use by typo;
  94.                          https://forum.lazarus.freepascal.org/index.php?topic=26425.45 }
  95. begin
  96.   SearchBox.Clear;
  97.   // PageMemo section already selected
  98.   MS:= TMemoryStream.Create;  { TMemoryStream use by Author: typo }
  99.   try
  100.     GetRTFSelection(PageMemo,MS);  // PageMemo is an RTF container
  101.     MS.Position:= 0;          { TMemoryStream use by Author: typo }
  102.     PutRTFSelection(SearchBox,MS);  // SearchBox is an RTF container
  103.   finally
  104.     MS.Free;
  105.     PageMemo.SelLength:= 0;
  106.   end;
  107. end;
  108.  
  109. //=========== end: my application of the functions  
  110.  
« Last Edit: December 23, 2021, 04:56:51 pm by rick2691 »
Windows 10, LAZ 2.0.12, FPC 3.2.0, SVN 64642, i386-win32-win32/win64, using windows unit

 

TinyPortal © 2005-2018