如何在Windows窗体应用程序中使用此WndProc?

时间:2014-04-22 12:49:30

标签: c# winforms wndproc

请指导我如何在Windows窗体应用程序中使用此WndProc

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    if (msg == NativeCalls.APIAttach && (uint)lParam == NativeCalls.SKYPECONTROLAPI_ATTACH_SUCCESS)
    {
        // Get the current handle to the Skype window
        NativeCalls.HWND_BROADCAST = wParam;
        handled = true;
        return new IntPtr(1);
    }

    // Skype sends our program messages using WM_COPYDATA. the data is in lParam
    if (msg == NativeCalls.WM_COPYDATA && wParam == NativeCalls.HWND_BROADCAST)
    {
        COPYDATASTRUCT data = (COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(COPYDATASTRUCT));
        StatusTextBox.AppendText(data.lpData + Environment.NewLine);

        // Check for connection
        if (data.lpData.IndexOf("CONNSTATUS ONLINE") > -1)
            ConnectButton.IsEnabled = false;

        // Check for calls
        IsCallInProgress(data.lpData);
        handled = true;
        return new IntPtr(1);
    }

    return IntPtr.Zero;
}

我见过人们在WPF中以这种方式使用上面的代码,如

protected override void OnSourceInitialized(EventArgs e)
{
    base.OnSourceInitialized(e);

    // Attach WndProc
    HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
    source.AddHook(WndProc);
}

2 个答案:

答案 0 :(得分:2)

C#中WndProc的原型是:

protected virtual void WndProc(ref Message m)

因此,您需要在类中重写此过程,假设它来自Control

protected override void WndProc(ref Message m)
{
    Boolean handled = false; m.Result = IntPtr.Zero;
    if (m.Msg == NativeCalls.APIAttach && (uint)m.Param == NativeCalls.SKYPECONTROLAPI_ATTACH_SUCCESS)
    {
        // Get the current handle to the Skype window
        NativeCalls.HWND_BROADCAST = m.WParam;
        handled = true;
        m.Result = new IntPtr(1);
    }

    // Skype sends our program messages using WM_COPYDATA. the data is in lParam
    if (m.Msg == NativeCalls.WM_COPYDATA && m.WParam == NativeCalls.HWND_BROADCAST)
    {
        COPYDATASTRUCT data = (COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, typeof(COPYDATASTRUCT));
        StatusTextBox.AppendText(data.lpData + Environment.NewLine);

        // Check for connection
        if (data.lpData.IndexOf("CONNSTATUS ONLINE") > -1)
            ConnectButton.IsEnabled = false;

        // Check for calls
        IsCallInProgress(data.lpData);
        handled = true;
        m.Result = new IntPtr(1);
    }

    if (handled) DefWndProc(ref m); else base.WndProc(ref m);
}

答案 1 :(得分:2)

您可以使用Application.AddMessageFilter方法。

[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public class TestMessageFilter : IMessageFilter
{
    public bool PreFilterMessage(ref Message m)
    {
        // Blocks all the messages relating to the left mouse button. 
        if (m.Msg >= 513 && m.Msg <= 515)
        {
            Console.WriteLine("Processing the messages : " + m.Msg);
            return true;
        }
        return false;
    }
}