【问题标题】:How to use this WndProc in Windows Forms application?如何在 Windows 窗体应用程序中使用这个 WndProc?
【发布时间】:2014-04-22 12:49:29
【问题描述】:

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

【问题讨论】:

  • 你有什么问题?
  • 如何在win表单应用中附加上述WndProc函数?
  • 如何对上面的 WndProc 进行 hook 和 unhook ?

标签: c# winforms wndproc


【解决方案1】:

你可以使用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;
    }
}

【讨论】:

  • 什么是 IMessageFilter 以及它的作用?为什么你把它推荐给我使用它?
【解决方案2】:

WndProc 在 C# 中的原型是:

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);
}

【讨论】:

  • 感谢您的回答,但我只是不明白为什么在我调试时如果阻塞,控件永远不会排在第二位?你能告诉我原因吗?谢谢
  • 抱歉,我编辑了我的帖子。问题是 m.Result 在函数的底部始终设置为零。一开始将其设置为零将解决这个问题,也许也是你的问题。
  • 仍然存在同样的问题。
  • 也许您的 Skype 有问题,而不是 WndProc。你可以设置断点并检查传入的消息吗?您是否收到正常的窗口消息?
  • 我猜不,因为我开发我的应用程序遵循基于 wpf 的应用程序,该应用程序还捕获 Skype 声音并保存到文件...。效果很好。我的应用程序不仅工作。我想我遗漏了一些东西,或者我的代码中有一些错误,这就是问题发生的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-25
  • 2010-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-13
相关资源
最近更新 更多