我是delphi的新手,我正试图弄清楚如何从一个richtextbox加载特定行(而不是完整的文本)到另一个。
procedure TForm1.richedit1change(Sender: TObject);
var
ms: TMemoryStream;
begin
ms := TMemoryStream.Create;
try
RichEdit1.Lines.SaveToStream(ms);
ms.Seek(0, soFromBeginning);
RichEdit2.Lines.LoadFromStream(ms);
finally
ms.Free;
end;
end;
答案 0 :(得分:3)
您不需要使用流将文本行从一个TRichEdit
传输到另一个Lines
。只需使用Lines
属性即可。
TRichEdit
类型为TStrings
,因此请使用其方法操作procedure TForm1.richedit1change(Sender: TObject);
var
i: Integer;
begin
RichEdit2.Lines.Clear;
for i := 0 to Pred(RichEdit1.Lines.Count) do
begin
if YourSpecificTestFunction(i) then
RichEdit2.Lines.Add(RichEdit1.Lines[i]);
end;
end;
文本。
procedure CopyRichEditSelection(Source,Dest: TRichEdit);
begin
// Copy Source.Selection to Dest via ClipBoard.
Dest.Clear;
if (Source.SelLength > 0) then
begin
Source.CopyToClipboard;
Dest.PasteFromClipboard;
end;
end;
如果要保留RTF格式,可以使用Zarko Gajic,Append or Insert RTF from one RichEdit to Another
描述的技术。
另一个简单的选择是使用Windows剪贴板和TRichEdit.Selection:
TRichEdit.SelStart
这也将保留您的格式,复制所选部分。
如果要在没有用户控制的情况下控制选择,请使用SelLength
将插入符号放置到选择开始的字符处,并选择RichEdit1.SelStart := RichEdit1.Perform(EM_LINEINDEX, Line, 0);
作为选择长度。要将插入符号放在特定行上,请使用:
Uses RichEdit;
function EditStreamOutCallback(dwCookie: DWORD_PTR; pbBuff: PByte; cb: Longint;
var pcb: LongInt): LongInt; stdcall;
begin
pcb := cb;
if cb > 0 then
begin
TStream(dwCookie).WriteBuffer(pbBuff^, cb);
Result := 0;
end
else
Result := 1;
end;
procedure GetRTFSelection(aRichEdit: TRichEdit; intoStream: TStream);
type
TEditStreamCallBack = function (dwCookie: DWORD_PTR; pbBuff: PByte;
cb: Longint; var pcb: Longint): Longint; stdcall;
TEditStream = packed record // <-- Note packed !!
dwCookie: DWORD_PTR;
dwError: Longint;
pfnCallback: TEditStreamCallBack;
end;
var
editstream: TEditStream;
begin
with editstream do
begin
dwCookie := DWORD_PTR(intoStream);
dwError := 0;
pfnCallback := EditStreamOutCallBack;
end;
aRichedit.Perform( EM_STREAMOUT, SF_RTF or SFF_SELECTION, LPARAM(@editstream));
end;
procedure CopyRichEditSelection(Source,Dest: TRichEdit);
var
aMemStream: TMemoryStream;
begin
Dest.Clear;
if (Source.SelLength > 0) then
begin
aMemStream := TMemoryStream.Create;
try
GetRTFSelection(Source, aMemStream);
aMemStream.Position := 0;
Dest.Lines.LoadFromStream(aMemStream);
finally
aMemStream.Free;
end;
end;
end;
如果您不想使用Windows剪贴板进行复制/粘贴操作,可以使用流:
{{1}}