【问题标题】:Subclass a native application from C#从 C# 子类化本机应用程序
【发布时间】:2012-06-19 10:57:44
【问题描述】:

我想从 C# 应用程序处理原生 MFC 应用程序中的鼠标单击。 为此,我正在尝试对本机应用程序进行子类化。我没有收到任何错误,但 wndproc 被较新地调用。

    private const int GwlWndProc = -4;
    private delegate int Win32WndProc(IntPtr hWnd, int msg, int wParam, int lParam);
    [DllImport("user32")]
    private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, Win32WndProc newProc);

    Win32WndProc _newWndProc = MyWndProc;

    SetLastError(0);
    IntPtr oldWndProc = SetWindowLong(hWnd, GwlWndProc, _newWndProc);
    if (oldWndProc == IntPtr.Zero)
    {
        int errorCode = Marshal.GetLastWin32Error();
        if (errorCode != 0)
            throw new Win32Exception(errorCode);
    }

private int MyWndProc(IntPtr hWnd, int msg, int wParam, int lParam)
    {
        Debug.WriteLine("MyWndProc " + (WindowsMessage)msg);
        if (msg == (int) WindowsMessage.LeftButtonDown)
        {
            MessageBox.Show("Clicked");
            return 0;
        }
        else return CallWindowProc(_subclasses[hWnd], hWnd, msg, wParam, lParam);
    }

编辑: 要获得 hWnd,我使用 GetForegroundWindow()

我试图做的是防止应用程序获得鼠标点击

【问题讨论】:

  • 你能添加你的代码来找到正在运行的本机应用程序的窗口句柄吗?
  • 您不能在 C# 中执行此操作。函数指针必须在拥有窗口的进程内有效。这需要注入 DLL,您不能注入托管代码。
  • @MusiGenesis 我使用 GetForegroundWindow

标签: c# winapi subclass


【解决方案1】:

我认为您需要使用挂钩,因为 SetWindowLong 不能跨不同的进程工作:看看这里http://www.codeproject.com/Articles/5264/Cross-Process-Subclassing

【讨论】:

    猜你喜欢
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2011-03-15
    相关资源
    最近更新 更多