I have a WPF Window ("A") that handles windows messages using the WindowInteropHelper and HwndSource:
WindowInteropHelper helper = new WindowInteropHelper(this);
IntPtr windowHandle = helper.Handle;
source = HwndSource.FromHwnd(windowHandle);
source.AddHook(new HwndSourceHook(WndProc));
and WndProc looks like this:
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
...
else if (msg == WM_CLOSE)
{
StringBuilder strbTitle = new StringBuilder(255);
int nLength = User32.GetWindowText(hwnd, strbTitle, strbTitle.Capacity + 1); // my own method
string strTitle = strbTitle.ToString();
// log that I'm being shut down.
}
}
}
The only problem is that "hwnd" is always returning my own window ("A"), not the window that sent the message ("B"). How do I figure out what window sent the message?
Note: I've tried sending other windows messages (like move/hide/show/quit) from other applications and I always get the same window name in "strbTitle." I've tried restarting "B" and re-sending the message and the hwnd didn't change, but if I restart "A" and not "B" then it does change (so I'm positive hwnd is the handle for the window that the message is to for some reason). I'm compiling for .NET 4.0 if that matters.
I've done a bit of searching, but can't find anyone that brings this up... Thanks for taking the time to view this and help me out!