如何使用C#剪贴板从记事本中获取选定的文本 [英] How can I use C# Clipboard to get selected text from notepad

查看:67
本文介绍了如何使用C#剪贴板从记事本中获取选定的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否仍然可以使用Clipboard类从当前活动的进程中复制所选文本?我已经设置了键盘挂钩,但确实抓住了按键.

更新:嗯,我什么都没尝试,因为我不知道该怎么做!我有办法基本上从记事本中返回所选文本.

Is there anyway to use the Clipboard class to copy the selected text from the currently active process? I have a keyboard hook set up and I do catch the key press.

Update: Well, I haven''t tried anything because I dont know what to try! Is there a way for me to return to my program the selected text from notepad basically.

推荐答案

不是很好!但这有效:
It''s not very nice! But it works:
[DllImport("User32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
private void button1_Click(object sender, EventArgs e)
    {
    foreach (Process proc in Process.GetProcesses())
        {
        if (proc.MainWindowTitle.Contains("Notepad"))
            {
            SetForegroundWindow(proc.MainWindowHandle);
            Thread.Sleep(1000);
            SendKeys.SendWait("^C");
            //IDataObject idata = Clipboard.GetDataObject();
            //Console.WriteLine(idata.ToString());
            if (Clipboard.ContainsText())
                {
                string text = Clipboard.GetText(TextDataFormat.UnicodeText);
                Console.WriteLine(text);
                }
            else
                {
                Console.WriteLine("No Text");
                }
            }
        }
    }

您可以降低sleep值,但是您需要短暂地暂停任务以使设置的前景窗口正常工作.

You can cut down the sleep value, but you do need to suspend the task briefly to allow the set foreground window to work.


您还可以考虑嵌入一个将记事本克隆到您的应用程序中,从而可以完全控制其行为.为此,我创建了一个精确的记事本克隆.您可以在这里找到它: http://www .simplygoodcode.com/2012/04/notepad-clone-in-net-winforms.html [
As an alternative have you considered embedding a notepad clone into your application that way you have full control over how it behaves. I created an exact clone of notepad exactly for that purpose. You can find it here: http://www.simplygoodcode.com/2012/04/notepad-clone-in-net-winforms.html[^]


这篇关于如何使用C#剪贴板从记事本中获取选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆