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

C# 如何在Windows窗体应用程序中使用此WndProc?,c#,winforms,wndproc,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) {

请指导我如何在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);
}

WndProc在C中的原型为:

protected virtual void WndProc(ref Message m)
所以,您需要在类中重写这个过程,假设它是从控件派生的


WndProc在C中的原型为:

protected virtual void WndProc(ref Message m)
所以,您需要在类中重写这个过程,假设它是从控件派生的

你可以使用这个方法

你可以使用这个方法



你的问题是什么?如何在win表单应用程序中附加上述WndProc功能?如何挂接和解除挂接上述WndProc?你的问题是什么?如何在win表单应用程序中附加上述WndProc功能?如何挂接和解除挂接上述WndProc?谢谢你的回答,但我不明白为什么在我执行操作时,控件永远不会成为第二个if阻塞我在调试吗?你能告诉我原因吗。谢谢你,我编辑了我的文章。问题是m.Result总是在函数的底部设置为零。在开始时将其设置为零将解决此问题,也可能解决您的问题。仍然存在相同的问题。可能您与Skype有一些问题,而不是WndProc。您可以设置断点并检查传入的消息吗?你收到正常的窗口消息吗?我想没有,因为我开发我的应用程序时遵循了基于wpf的应用程序,这些应用程序还可以捕获skype声音并保存到文件中……这非常有效。我的申请不仅有效。我想我遗漏了一些东西,或者我的代码中有一些错误,这就是问题发生的原因。谢谢你的回答,但我不明白为什么调试时控件永远不会进入第二个if块?你能告诉我原因吗。谢谢你,我编辑了我的文章。问题是m.Result总是在函数的底部设置为零。在开始时将其设置为零将解决此问题,也可能解决您的问题。仍然存在相同的问题。可能您与Skype有一些问题,而不是WndProc。您可以设置断点并检查传入的消息吗?你收到正常的窗口消息吗?我想没有,因为我开发我的应用程序时遵循了基于wpf的应用程序,这些应用程序还可以捕获skype声音并保存到文件中……这非常有效。我的申请不仅有效。我想我遗漏了一些东西,或者代码中有一些错误,这就是问题发生的原因。IMessageFilter是什么,它是做什么的?为什么你把它交给我使用?什么是IMessageFilter,它做什么?你为什么把它交给我使用?
[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;
    }
}