I have a block of code like:
IntPtr hWnd = new WindowInteropHelper(this).Handle;
HwndSource source = HwndSource.FromHwnd(hWnd);
source.AddHook(new HwndSourceHook(WndProc));
NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.WM_CALL, IntPtr.Zero, IntPtr.Zero);
This was originally in a WPF application. However, I need to replicate the functionality in a WinForms application. Also, NativeMethods.PostMessage just maps to user32.dll PostMessage:
[DllImport("user32")]
public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
Are there a 1 to 1 equivalents of WindowInteropHelper/HwndSource/HwndSourceHook
that I can use in my WinForms applications?
Am not more of WPF background. but for me it sounds like you're looking for NativeWindow.
The basic point is: you don't need anything except
AddHook
from your source. Each WinForm has a methodGetHandle()
which will give you the handle of the Window/Form (and you foundPostMessage
already by yourself).Too translate
AddHook
you either write your own class implementingIMessageFilter
(1) or you overrideWndProc()
(2).(1) will receive messages application-wide, regardless to which form you send them while (2) only receives messages for the specific form overriding the method.
I could'nt find anything regarding
WM_CALL
, as you have to specify the window message as an integer (usually in hex), so this is up to you.(1):
(2):