c# - 如何在 Windows 窗体应用程序中使用此 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)
    {
        // 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);
}

最佳答案

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

关于c# - 如何在 Windows 窗体应用程序中使用此 WndProc?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23220111/

相关文章:

c# - 解析基类

c# - 如何从 C# 返回集合对象并在 C++ 中访问它们?

c# - 拒绝访问!?我无法从 C 驱动器上的安装文件夹中打开文件

wpf - 双击标题禁用最大化 WPF 窗口

c# - 为什么我得到 "Invalid algorithm specified"异常

c# - 从任务栏隐藏 winforms 应用程序

vb.net - 从组合框中获取所选项目

c# - .NET Winforms 垂直进度条文本

windows - 将 Win32 API WndProc Key 消息从一个窗口传输到另一个窗口

c# - 程序集加载失败,没有融合日志错误