0

I am currently working on an overlay, which attaches itself to a different process window and allows text input via a wpf textbox.

I achieve this by invoking the windows SetWindowLong function to draw the UserControl on top and keep it from getting activated. The latter is necessary to not steal focus from the main window, that the overlay gets applied on:

    [DllImport("user32.dll")]
    static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);

    [DllImport("user32.dll")]
    static extern int GetWindowLong(IntPtr hwnd, int index);

Called like this:

        var extendedStyle = GetWindowLong(hwnd, GwlExstyle);
        SetWindowLong(hwnd, GwlExstyle, extendedStyle | 0x00000008 | 0x08000000);

The problem with this approach is, that the textbox can't be used as usual. I managed to use global key hooks to intercept keyboard input, which is working fine, but plugging the cuaght keys into the textBox proves to be difficult.

I tried to use textBox.RaiseEvent(), raising the KeyDown event with the appropriate key, but it does not result in any input on the inactive window.

    public void SimulateKeypress(Key k)
    {
        textBox.Focus();
        Keyboard.Focus(textBox);

        textBox.RaiseEvent(new KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromVisual(textBox), 0, k)
        { RoutedEvent = Keyboard.PreviewKeyDownEvent });
     }

On the other hand using the TextInputEvent I can input text, but that does not allow me to mark and delete text conveniently.

    public void SimulateKeypress(Key k)
    {
        Focus();
        Keyboard.Focus(textBox);

        textBox.RaiseEvent(new TextCompositionEventArgs(InputManager.Current.PrimaryKeyboardDevice, new TextComposition(InputManager.Current, textBox, "This works"))
        { RoutedEvent = TextCompositionManager.TextInputEvent });
    }

So far recreating textBox functionality (inputting, deleting and marking text, ctrl+c etc.) seems to be the only possible option. While that sure is possible, it does not seem very elegant. It's an option which I'd like to avoid if possible.

Any ideas how I could allow input on the inactive textBox?

Community
  • 1
  • 1
PsHe
  • 1
  • 1
  • Hello and welcome to stack overflow. Please review [How do I ask good question](http://stackoverflow.com/help/how-to-ask) in the Help section. Posting some code may be of assistance to those who can help you – Paul Stoner Jan 05 '17 at 16:54
  • See http://stackoverflow.com/questions/30872931/how-can-i-add-a-wpf-overlay-over-an-external-win32-applications-window – PaulF Jan 05 '17 at 17:22
  • The linked answer does not allow parenting a window from a different, running process. – PsHe Jan 05 '17 at 19:36

0 Answers0