In my WPF Project I have two windows. I need to redirect all the events from Window1 to Window2.
Checkout this screenshot :
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.