【问题标题】:Can't select text in RichEditComponent control无法在 RichEditComponent 控件中选择文本
【发布时间】:2018-06-22 08:49:38
【问题描述】:

我想将我的应用程序的窗口保持在另一个程序的窗口前面。 我的应用程序是使用 WPF 创建的,我将所有者设置为另一个窗口的 hwnd,如下所示:

// this: my wpf window   
WindowInteropHelper helper = new WindowInteropHelper(this);

//The hwnd is handle of window that other program I want to follow    
helper.Owner = new IntPtr(hwnd); 

一切都很完美,但我无法在窗口(hwnd 窗口)的 RichEditComponent 中用鼠标选择文本。

有办法解决这个问题吗?


不知道其他程序是用哪种语言编写的,可能是c++。使用 windows api "FindWindowEx" 获得的其他程序窗口的句柄。

【问题讨论】:

  • 您的屏幕截图不是很有用。似乎选择了一些文本?

标签: c# wpf hwnd owner


【解决方案1】:

如果您的其他程序是Winforms程序,则需要添加对

的引用

System.Windows.Forms.Integration.dll

并添加对ElementHost.EnableModelessKeyboardInterop(Window window) 的调用,如下所示:

WindowInteropHelper helper = new WindowInteropHelper(this);
helper.Owner = new IntPtr(hwnd);

ElementHost.EnableModelessKeyboardInterop(this);

因为显然 Winforms 和 WPF 有不同的处理文本输入的方式(因此也会影响文本选择 - 更具体地说,复制和粘贴所选文本)。


除此之外,问题可能是 HWND 指针 - 你如何获得它?

  • 例如这是通过指定进程名称来获取主窗口句柄的方法:

    Process process = Process.GetProcessesByName("...")[0];
    IntPtr hwnd = process.MainWindowHandle;
    

【讨论】:

  • 谢谢!不知道其他程序用哪种语言编写,也许是 c++。你贴的方法不行。用windows api“FindWindowEx”获取的其他程序窗口的句柄。
  • @cqsir 它应该仍然可以工作。不过,您是否尝试过 ElementHost.EnableModelessKeyboardInterop 的方法?
  • public void SetOwner(Window wnd, int hwnd) { if (wnd == null) return; WindowInteropHelper 助手 = new WindowInteropHelper(wnd); helper.Owner = new IntPtr(hwnd); ElementHost.EnableModelessKeyboardInterop(wnd); //WindowsFormsHost.EnableWindowsFormsInterop(); }
【解决方案2】:

我已经通过检测鼠标拖动事件使用全局挂钩解决了这个问题。开始拖拽时取消设置属主,拖拽完成后再次通过跟随窗口设置属主。

使用 MouseKeyHook 检测全局鼠标拖动事件。

https://www.nuget.org/packages/MouseKeyHook

再次感谢@Thomas Flinkow,感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多