如何将任何应用程序的选定文本添加到Windows窗体应用程序中

Thi*_*a H 11 c# clipboard hook winforms

这就是我想要做的,

当用户通过双击鼠标选择任何正在运行的应用程序的任何单词(文本)时,应将特定突出显示的单词插入已经运行的Windows应用程序中.

到目前为止,我已经使用Global Keystroke用户必须触发CRT+ C键盘组合键将所选单词复制到win表单应用程序中来实现逻辑.

我想知道的是有没有办法将这些选定的文本放入应用程序而无需按键盘按键?

jcr*_*ada 6

经过一番阅读,我找到了方法:

  1. 使用globalmousekeyhook.codeplex.com 之类的东西挂钩双击事件
  2. (可选)保存剪贴板的当前状态
  3. 使用GetCursorPosfrom获取当前鼠标位置user32.dll
  4. 使用WindowFromPointfrom 根据光标位置获取窗口user32.dll

    [DllImport("user32.dll")]
    public static extern IntPtr WindowFromPoint(Point lpPoint);
    
    [DllImport("user32.dll")]
    public static extern bool GetCursorPos(out Point lpPoint);
    
    public static IntPtr GetWindowUnderCursor()
    {
       Point ptCursor = new Point();
    
       if (!(PInvoke.GetCursorPos(out ptCursor)))
          return IntPtr.Zero;
    
       return WindowFromPoint(ptCursor);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  5. 使用SendMessage表单发送复制命令user32.dll(请参阅 使用 User32.dll SendMessage 发送带有 ALT 修饰符的键

  6. 你的代码
  7. (可选)恢复步骤2中保存的剪贴板内容

  • 选项1是什么?这是应该遵循的一系列步骤:P (3认同)