2

In my WPF Project I have two windows. I need to redirect all the events from Window1 to Window2.

Checkout this screenshot :

Sample

What i am trying to do ?


Take the screenshot as example, When I move mouse over the window1 button, it should highlight the button in window 2.

In real case scenario the window 2 will be having some other content, it's not just the mouse move event I am trying to implement, i am trying to implement all events, including the input handling. which means the window1 must forward all of its events to window2.

What i have tried so far ?


Since all the events are usually passed through WndProc, i created a hook to WPF WndProc of Window1 and Forwarded all its Events to Window2 using SendMessage().

Here is the code i used :

public partial class MainWindow : Window
{
    Window window1;
    IntPtr winHwnd;

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

    public MainWindow()
    {
        InitializeComponent();
        window1 = new Window1();
        window1.Show();
    }

    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        var hwnd = new WindowInteropHelper(this).EnsureHandle();
        winHwnd = new WindowInteropHelper(window1).EnsureHandle();
        HwndSource hwndSource = HwndSource.FromHwnd(hwnd);
        hwndSource.AddHook(WndProc);
    }

    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        if(winHwnd!=IntPtr.Zero)
        {
            SendMessage(winHwnd, msg, wParam, lParam);
        }
        return IntPtr.Zero;
    }
}

This is just a code that I used for testing, when I checked I was able to transfer win32 events from Window1 to Window2, but none of the WPF Events are working. but win32 events were working fine.

After some research I found that the WPF uses ComponentDispatcher for registering WPF Events, but how will I be able to share the event's through it.

trickymind
  • 557
  • 5
  • 21
  • There is absolutely no way to do what you are trying to do without understanding every detail of the platforms involved (the OS, .NET, and WPF). As long as you claim that *"win32 events were working fine"* you are still on your way to unlearn all the wrong you've accumulated. – IInspectable Jun 27 '21 at 09:55
  • can you please tell what is wrong here ?. Actually i have been trying to remove the airspace issue from wpf. currently i was able to render WPF contents on top of Direct Composition Visuals, the only thing left is to forward the inputs. some how it might be possible. don't know how?. Microsoft always hide things. – trickymind Jun 27 '21 at 10:11
  • "win32 events were working fine" - This means that i was able to synchronize the resize,maximize,minimize etc... of the window1 with window 2 but none of the wpf events were working, like the mouse move etc... – trickymind Jun 27 '21 at 10:12
  • @trickymind can you share your experience on this? were you able to solve it? – A-_-S Feb 06 '22 at 09:23

0 Answers0