如何在Delphi中模拟Ctrl + c [英] how to Simulate Ctrl+ c in Delphi
问题描述
问题是我想从另一个应用程序,例如从选择目标文本之后从记事本复制一个文本。
(让我来说说使用剪贴板进行进程间通信是一个坏主意,剪贴板属于用户和您的应用程序只能使用它作为用户选择的结果。)
如果您在记事本中选择了文本,则会获取内容在Delphi表单中使用 TMemo
(仅使用 TMemo
和 TButton
;将 ClipBrd
添加到您的uses子句中):
TForm1.Button1Click(Sender:TObject);
var
NpWnd,NpEdit:HWnd;
begin
Memo1.Clear;
NpWnd:= FindWindow('Notepad',nil);
如果NpWnd<> 0然后
begin
NpEdit:= FindWindowEx(NpWnd,0,'Edit',nil);
如果NpEdit<> 0然后
begin
SendMessage(NpEdit,WM_COPY,0,0);
Memo1.Lines.Text:= Clipboard.AsText;
结束
结束
结束
结果示例:
如果文本未被首先选择请先发送一条 WM_SETSEL
消息。通过 0
和'-1'的值选择所有文本。
程序TForm1.Button1Click(Sender:TObject);
var
NpWnd,NpEdit:HWnd;
begin
Memo1.Clear;
NpWnd:= FindWindow('Notepad',nil);
如果NpWnd<> 0然后
begin
NpEdit:= FindWindowEx(NpWnd,0,'Edit',nil);
如果NpEdit<> 0然后
begin
SendMessage(NpEdit,EM_SETSEL,0,-1);
SendMessage(NpEdit,WM_COPY,0,0);
Memo1.Lines.Text:= Clipboard.AsText;
结束
结束
结束
is there any way to simulate Ctrl+C command in delphi ? the problem is i want that from another application for example copy a text from Notepad after select the target text .
(Let me preface this by saying that using the clipboard for inter-process communication is a bad idea. The clipboard belongs to the user, and your application should only use it as a result of the user choosing to do so.)
If you have text selected in Notepad, this will get the contents into a TMemo
on a Delphi form (uses just a TMemo
and TButton
; add ClipBrd
to your uses clause):
procedure TForm1.Button1Click(Sender: TObject);
var
NpWnd, NpEdit: HWnd;
begin
Memo1.Clear;
NpWnd := FindWindow('Notepad', nil);
if NpWnd <> 0 then
begin
NpEdit := FindWindowEx(NpWnd, 0, 'Edit', nil);
if NpEdit <> 0 then
begin
SendMessage(NpEdit, WM_COPY, 0, 0);
Memo1.Lines.Text := Clipboard.AsText;
end;
end;
end;
Sample of results:
If the text is not selected first, send it a WM_SETSEL
message first. Passing values of 0
and '-1' selects all text.
procedure TForm1.Button1Click(Sender: TObject);
var
NpWnd, NpEdit: HWnd;
begin
Memo1.Clear;
NpWnd := FindWindow('Notepad', nil);
if NpWnd <> 0 then
begin
NpEdit := FindWindowEx(NpWnd, 0, 'Edit', nil);
if NpEdit <> 0 then
begin
SendMessage(NpEdit, EM_SETSEL, 0, -1);
SendMessage(NpEdit, WM_COPY, 0, 0);
Memo1.Lines.Text := Clipboard.AsText;
end;
end;
end;
这篇关于如何在Delphi中模拟Ctrl + c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!