C# 在不同的应用程序中复制和修改选定的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/235972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy and Modify selected text in different application
提问by Vinod
I have a windows application running at the backend. I have functions in this applications mapped to hot keys. Like if I put a message box into this function and give hot key as Alt+Ctrl+D. then on pressing Alt, Ctrland Dtogether the message box comes up. My application is working fine till this point.
我有一个在后端运行的 Windows 应用程序。我将此应用程序中的功能映射到热键。就像如果我把一个消息框,这个功能并给热键为Alt+ Ctrl+ D。然后按Alt,Ctrl并D一起消息框出现。到目前为止,我的应用程序运行良好。
Now I want to write a code inside this function so that when I am using another application like notepad, I select a particular line of text and press the hot key Alt+ Ctrl+ Dit is supposed to copy the selected text append it with "_copied" and paste it back to notepad.
现在我想在这个函数中编写一个代码,这样当我使用另一个应用程序(如记事本)时,我选择特定的文本行并按热键Alt+ Ctrl+D它应该复制所选文本并附加“_copied”和将其粘贴回记事本。
Anyone who has tried a similar application please help me with your valuable inputs.
任何尝试过类似应用程序的人请帮助我提供宝贵的意见。
采纳答案by Eduardo Molteni
Your question has two answers
你的问题有两个答案
How can my app set a global hotkey
我的应用程序如何设置全局热键
You have to call an API funcion called RegisterHotKey
您必须调用一个名为 RegisterHotKey 的 API 函数
BOOL RegisterHotKey(
HWND hWnd, // window to receive hot-key notification
int id, // identifier of hot key
UINT fsModifiers, // key-modifier flags
UINT vk // virtual-key code
);
More info here: http://www.codeproject.com/KB/system/nishhotkeys01.aspx
更多信息在这里:http: //www.codeproject.com/KB/system/nishhotkeys01.aspx
How to get the selected text from the foreground window
如何从前台窗口获取选定的文本
Easiest way is to send crl-C to the window and then capture the clipboard content.
最简单的方法是将 crl-C 发送到窗口,然后捕获剪贴板内容。
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
static public extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
.....
private void SendCtrlC(IntPtr hWnd)
{
uint KEYEVENTF_KEYUP = 2;
byte VK_CONTROL = 0x11;
SetForegroundWindow(hWnd);
keybd_event(VK_CONTROL,0,0,0);
keybd_event (0x43, 0, 0, 0 ); //Send the C key (43 is "C")
keybd_event (0x43, 0, KEYEVENTF_KEYUP, 0);
keybd_event (VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);// 'Left Control Up
}
Disclaimer: Code by Marcus Peters from here: http://bytes.com/forum/post1029553-5.html
Posted here for your convenience.
免责声明:Marcus Peters 的代码来自此处:http: //bytes.com/forum/post1029553-5.html
为方便起见,在此处发布。
回答by Omer van Kloeten
Use the Clipboardclass to copy the contents to the clipboard, then paste in the notepad.
使用Clipboard类将内容复制到剪贴板,然后粘贴到记事本中。
You could also write the contents to a text file and open it with notepad by running the notepad.exe application with the text file's path as a command line parameter.
您还可以将内容写入文本文件,并通过使用文本文件的路径作为命令行参数运行 notepad.exe 应用程序来使用记事本打开它。
回答by PhiLho
回答by DurkoMatko
UPDATE 2020
2020 年更新
How to get the selected text from the foreground window
如何从前台窗口获取选定的文本
No idea for how long has this been possible but instead of fighting with Win32 programming (mostly user32.dll
and various Windows messages like WM_GETTEXT, WM_COPY
and various SendMessage(handle, WM_GETTEXT, maxLength, sb)
calls) which is advised in most of SO threads on this topic, I easily managed to access selected text in any window in my C# code followingly:
不知道这可能有多长时间,但我没有与 Win32 编程(主要是user32.dll
各种 Windows 消息WM_GETTEXT, WM_COPY
和各种SendMessage(handle, WM_GETTEXT, maxLength, sb)
调用)进行斗争,这在该主题的大多数 SO 线程中都建议,我轻松地设法访问了任何窗口中的选定文本我的 C# 代码如下:
// programatically copy selected text into clipboard
await System.Threading.Tasks.Task.Factory.StartNew(fetchSelectionToClipboard);
// access clipboard which now contains selected text in foreground window (active application)
await System.Threading.Tasks.Task.Factory.StartNew(useClipBoardValue);
Here the methods being called:
这里调用的方法:
static void fetchSelectionToClipboard()
{
Thread.Sleep(400);
SendKeys.SendWait("^c"); // magic line which copies selected text to clipboard
Thread.Sleep(400);
}
// depends on the type of your app, you sometimes need to access clipboard from a Single Thread Appartment model..therefore I'm creating a new thread here
static void useClipBoardValue()
{
Exception threadEx = null;
// Single Thread Apartment model
Thread staThread = new Thread(
delegate ()
{
try
{
Console.WriteLine(Clipboard.GetText());
}
catch (Exception ex)
{
threadEx = ex;
}
});
staThread.SetApartmentState(ApartmentState.STA);
staThread.Start();
staThread.Join();
}