【发布时间】: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