WinForms equivalent of WPF WindowInteropHelper, HwndSource, HwndSourceHook

kmarks2 picture kmarks2 · Nov 20, 2013 · Viewed 7.1k times · Source

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?

Answer

KeyNone picture KeyNone · Nov 20, 2013

The basic point is: you don't need anything except AddHook from your source. Each WinForm has a method GetHandle() which will give you the handle of the Window/Form (and you found PostMessage already by yourself).

Too translate AddHook you either write your own class implementing IMessageFilter (1) or you override WndProc() (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):

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

public partial class Form1 : Form
{
    [DllImport("user32")]
    public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);

    //private const int WM_xxx = 0x0;
    //you have to know for which event you wanna register

    public Form1()
    {
        InitializeComponent();

        IntPtr hWnd = this.Handle;
        Application.AddMessageFilter(new MyMessageFilter());
        PostMessage(hWnd, WM_xxx, IntPtr.Zero, IntPtr.Zero);
    }        
}

class MyMessageFilter : IMessageFilter
{
    //private const int WM_xxx = 0x0;

    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == WM_xxx)
        {
            //code to handle the message
        }
        return false;
    }
}

(2):

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

public partial class Form 1 {
    [DllImport("user32")]
    public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);

    //private const int WM_xxx = 0x0;
    //you have to know for which event you wanna register

    public Form1()
    {
        InitializeComponent();

        IntPtr hWnd = this.Handle;
        PostMessage(hWnd, WM_xxx, IntPtr.Zero, IntPtr.Zero);
    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WMK_xxx)
        {
            //code to handle the message
        }
    }
}