简体   繁体   English

WPF c# 复制并粘贴到剪贴板

[英]WPF c# copy and paste to clipboard

I want to make a WPF program which converts unit (numbers from other applications).我想制作一个转换单位(来自其他应用程序的数字)的 WPF 程序。 I made my program can run at system tray and I can set global hotkey too.我让我的程序可以在系统托盘上运行,我也可以设置全局热键。 But it's my first time making a WPF program so I'm having some problems.但这是我第一次制作 WPF 程序,所以我遇到了一些问题。

This function is called when user presses global hotkey.当用户按下全局热键时调用此函数。 To calculate, I need to copy/paste selected text from other application.要计算,我需要从其他应用程序复制/粘贴选定的文本。 I would appreciate it if you help me.如果你能帮助我,我将不胜感激。 (The code might be a little messy) (代码可能有点乱)

  • My user wants automatically convert his selected text.我的用户想要自动转换他选择的文本。
  • He is using my application like this now: 1. Select text 2. Press Ctrl+C 3. Press Ctrl+D(Global Hotkey) 4. Press Ctrl+V.他现在像这样使用我的应用程序: 1. 选择文本 2. 按 Ctrl+C 3. 按 Ctrl+D(全局热键) 4. 按 Ctrl+V。
  • What he wants is: 1. Select text 2. Press Ctrl+D(Global Hotkey) and it's done.他想要的是: 1. 选择文本 2. 按 Ctrl+D(全局热键)就完成了。

     void ClipboardCalc() { string resultStr = string.Empty; string temp = string.Empty; string digit = string.Empty; double result = 0; temp = Clipboard.GetText(); // Need to copy "selected text from other application" to clipboard here. resultStr = Clipboard.GetText(); resultStr = string.Join(string.Empty, Regex.Match(resultStr, @"\\d+(\\.\\d+)?")); try { result = Convert.ToDouble(resultStr); } catch (Exception ee) { } if (reverse_mode == false) result *= 3.30579; else result /= 3.30579; digit = "F" + textBox2.Text; if (result == (int)result) resultStr = result.ToString(); else resultStr = result.ToString(digit); resultStr = string.Format("{0:0.##########}", Convert.ToDouble(resultStr)); if (print_measure != false) { if (reverse_mode != false) resultStr += "평"; else resultStr += "m²"; } Clipboard.SetText(resultStr); // Then paste clipboard text here. //Clipboard.SetText(temp); }

This is a bodge sort of answer, but it seems like a bodge sort of program, so I'll put it out there anyway.这是一个bodge类型的答案,但它似乎是一个bodge类型的程序,所以无论如何我都会把它放在那里。

If you've managed to set up a "global hotkey", then I'm assuming you've already gotten into p/invoke territory.如果您已经设法设置了“全局热键”,那么我假设您已经进入了 p/invoke 领域。 So I might as well introduce you to SendInput .所以我不妨向您介绍SendInput This native method lets an application "synthesize keystrokes, mouse motions, and button clicks to the currently active window".这种本机方法允许应用程序“将击键、鼠标运动和按钮点击合成到当前活动的窗口”。 You can use it to send Ctrl+C to the active window.您可以使用它来将 Ctrl+C 发送到活动窗口。

Again, a bodged solution with a lot of assumptions, but it will probably work in most cases.同样,一个有很多假设的bodged解决方案,但它可能在大多数情况下都有效。

Note that you'll have to send key down and key up inputs individually (ie "Ctrl down", "C down", "C up", "Ctrl up", in that order).请注意,您必须分别发送 key down 和 key up 输入(即按“Ctrl down”、“C down”、“C up”、“Ctrl up”的顺序)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM