【问题标题】:how can i simulate CTRL+C in C#如何在 C# 中模拟 CTRL+C
【发布时间】:2010-12-23 23:06:59
【问题描述】:

我有以下代码在记事本中可以正常工作,但在 WORD 中却不行!!

 [DllImport("user32.dll")]
 public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

 [DllImport("user32.dll")]
 public static extern IntPtr GetForegroundWindow();

 [DllImport("user32.dll", SetLastError = true)]
 public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

 [DllImport("kernel32.dll")]
 public static extern uint GetCurrentThreadId();

 [DllImport("user32.dll")]
 public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);

 [DllImport("user32.dll")]
 public static extern IntPtr GetFocus();

 [DllImport("user32.dll")]
 public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);

 // second overload of SendMessage
 [DllImport("user32.dll")]
 public static extern int SendMessage(IntPtr hWnd, uint Msg, out int wParam, out int lParam);

 [DllImport("user32.dll")]
 public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

 public const uint WM_GETTEXT = 0x0D;
 public const uint WM_GETTEXTLENGTH = 0x0E;
 public const uint EM_GETSEL = 0xB0;

        IntPtr hWnd = WinUser.GetForegroundWindow();
        uint processId;
        uint activeThreadId = WinUser.GetWindowThreadProcessId(hWnd, out processId);
        uint currentThreadId = WinUser.GetCurrentThreadId();

        WinUser.AttachThreadInput(activeThreadId, currentThreadId, true);
        IntPtr focusedHandle = WinUser.GetFocus();
        WinUser.AttachThreadInput(activeThreadId, currentThreadId, false);

        int len = WinUser.SendMessage(focusedHandle, WinUser.WM_GETTEXTLENGTH, 0, null);
        StringBuilder sb = new StringBuilder(len);
        int numChars = WinUser.SendMessage(focusedHandle, WinUser.WM_GETTEXT, len + 1, sb);

        int start, next;
        string selectedText = "";
        WinUser.SendMessage(focusedHandle, WinUser.EM_GETSEL, out start, out next);
        try
        {
            selectedText = sb.ToString().Substring(start, next - start);
        }

不幸的是,当在 WORD 或任何“richtextbox”中选择文本时,上述内容会返回“{Microsoft Word Document}”。 CTRL+C是怎么做到的?

注意:这在记事本或任何简单的文本编辑器中都可以正常工作。

【问题讨论】:

  • 你想模拟按键还是复制数据?
  • 不,我只想从另一个窗口复制数据。不使用 CTRL+C,而是执行上述操作。
  • 我不敢相信有人否决了这个问题,并且有超过 1K 的浏览量!!..
  • 我正在寻找模拟“Ctrl-C”的按键有没有办法在 C# 中做到这一点?

标签: c# richtextbox copy-paste


【解决方案1】:

【讨论】:

  • 这可能是!谢谢让我看看。事实上我从那个网站得到了基本代码
  • 嗨,这很好,但我完全忘记了这样做的主要需要不是将数据保存在 Windows 剪贴板中。这就是我需要模拟 Ctrl+c 而不是使用它的原因。 :)
  • 复制剪贴板内容,再次模拟Ctrl+c读取,恢复原状。
【解决方案2】:

我认为您应该在C# Clipboard Copy and Pasting 上查看本教程。在 C# 中使用复制粘贴实际上并没有您想象的那么难。

复制

Clipboard.SetText(txtClipboard.Text);

粘贴

txtClipboard.Text = Clipboard.GetText();

查看以上链接以获取更多信息和示例。您还应该查看MSDN pageClipboard

【讨论】:

  • ya 使用剪贴板很容易,但我希望在不使用 CTRL+C 的情况下复制文本,我正在尝试模拟它 - 不要使用它。因此剪贴板将是空的。
  • 好吧,priyank 的评论有助于在那个链接中他们发送一条消息来模拟 ctrc+c,然后我可以像你建议的那样使用剪贴板对象......谢谢
  • priyanks 评论工作正常,但我完全忘记了这样做的主要需要不是将数据保存在 Windows 剪贴板中。这就是我需要模拟 Ctrl+c 而不是使用它的原因。 :) 你知道我上面的代码哪里出错了。
  • 正是我想知道的??
【解决方案3】:

我很确定 Word 不会回复 EM_ 消息。这些消息特定于 Windows 编辑控件;只是碰巧记事本对其文本使用了普通的编辑控件。

您可以使用 Word COM 自动化接口实现您想要的。没有 100% 保证从其他应用程序检索文本的方法。

编辑:我不是这方面的专家,但您可能会在无障碍 API 方面取得更大的成功。应用(例如记事本或 Word)可以公开一组表示其 UI 的对象,您可以从应用中查询这些对象。

【讨论】:

  • 这是我正在尝试的:从活动窗口中,我需要复制已选择的文本。然后解析该文本以在我的服务中执行某些操作。是的,你是对的,这在任何普通的编辑控件上都可以正常工作。
  • 好的,Word 自动化 API 或更通用的可访问性 API 都是可行的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-01
  • 2017-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-24
  • 2012-03-14
相关资源
最近更新 更多