子类化本机应用程序

子类化本机应用程序

本文介绍了从 C# 子类化本机应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to handle mouse click in a native MFC application from a C# application.To do so I'm trying to subclass the native application. I don't get any errors, but the wndproc are newer invoked.

    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()

To get the hWnd I use GetForegroundWindow()

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

What I try to do is is to prevent the application to get the mouse click

推荐答案

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

I think you need to use hooking because SetWindowLong does not work across different processes: have a look here http://www.codeproject.com/Articles/5264/Cross-Process-Subclassing

这篇关于从 C# 子类化本机应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 01:36