-1

I want to keep my app's window in front of another programs's window. My app is created with WPF, I set owner with another window's hwnd like this:

// 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); 

Everything is perfect, but I can't select text with mouse within RichEditComponent of the window (the hwnd window).

Any idea to fix this?


Don't know other program write with which language,maybe c++. Handle of other program's window obtained with windows api "FindWindowEx".

cqsir
  • 1
  • 1

2 Answers2

0

If your other program is a Winforms program, you need to add a reference to

System.Windows.Forms.Integration.dll

and add a call to ElementHost.EnableModelessKeyboardInterop(Window window) like shown below:

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

ElementHost.EnableModelessKeyboardInterop(this);

because apparently Winforms and WPF have different ways of handling text input (and therefore also affecting text selection - more specific, copy & paste of selected text - as well).


Other than that, the issue might be the HWND pointer - how do you obtain it?

  • e.g. this is how you can obtain the main window handle by specifying a process name:

    Process process = Process.GetProcessesByName("...")[0];
    IntPtr hwnd = process.MainWindowHandle;
    
Thomas Flinkow
  • 4,845
  • 5
  • 29
  • 65
  • Thanks! Don't know other program write with which language,maybe c++. The method you post can't work.Handle of other program's window obtained with windows api "FindWindowEx". – cqsir Jun 22 '18 at 09:47
  • @cqsir it should still work though. Nevertheless, did you try the approach with `ElementHost.EnableModelessKeyboardInterop`? – Thomas Flinkow Jun 22 '18 at 09:53
  • public void SetOwner(Window wnd, int hwnd) { if (wnd == null) return; WindowInteropHelper helper = new WindowInteropHelper(wnd); helper.Owner = new IntPtr(hwnd); ElementHost.EnableModelessKeyboardInterop(wnd); //WindowsFormsHost.EnableWindowsFormsInterop(); } – cqsir Jun 22 '18 at 10:01
0

I had resolved the question by detect Mouse drag event use global hook. When drag start,cancel set owner,and then,when drag finished,set owner with the follow window again.

Use MouseKeyHook to detect global mouse drag event.

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

Thanks @Thomas Flinkow again,for your help!

cqsir
  • 1
  • 1