我目前正试图在DirectX游戏中发送输入(鼠标当前,键盘很快:D),但我没有成功。 根据我目前的测试,如果窗口在前面,我只能设法点击它。 这是代码......
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace hots
{
public static class Mouse
{
[DllImport("user32.dll")]
private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
//[DllImport("user32.dll", CharSet = CharSet.Auto)]
//private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lparam);
// The SendMessage function sends the specified message to a window or windows.
// It calls the window procedure for the specified window and does not return
// until the window procedure has processed the message.
[DllImport("User32.dll")]
public static extern Int32 SendMessage(
IntPtr hWnd, // handle to destination window
int Msg, // message
IntPtr wParam, // first message parameter
ref Win32Api.tagWINDOWPOS lParam); // second message parameter
[DllImport("User32.dll")]
public static extern Int32 SendMessage(
IntPtr hWnd, // handle to destination window
int Msg, // message
IntPtr wParam, // first message parameter
int lParam); // second message parameter
[DllImport("User32.dll")]
public static extern Int32 SendMessage(
IntPtr hWnd, // handle to destination window
int Msg, // message
IntPtr wParam, // first message parameter
long lParam); // second message parameter
[DllImport("User32.dll")]
public static extern Int32 SendMessage(
IntPtr hWnd, // handle to destination window
int Msg, // message
IntPtr wParam, // first message parameter
IntPtr lParam); // second message parameter
public const int WM_MOUSEMOVE = 0x0200;
public const int WM_LBUTTONDOWN = 0x0201;
public const int WM_LBUTTONUP = 0x0202;
public const int WM_MOUSEACTIVATE = 0x0021;
public const int WM_ACTIVATE = 0x0006;
public const int MK_LBUTTON = 0x0001;
public const int WM_NCHITTEST = 0x0084;
public const int WM_SETCURSOR = 0x0020;
public const int HT_CLIENT = 1;
public const int WM_WINDOWPOSCHANGING = 0x0046;
public const int SWP_NOSIZE = 0x0001;
public const int SWP_NOMOVE = 0x0002;
public const int WM_ACTIVATEAPP = 0x001C;
public const int WM_NCACTIVATE = 0x0086;
public const int WM_IME_SETCONTEXT = 0x0281;
public const int WM_IME_NOTIFY = 0x0282;
public const int WM_SETFOCUS = 0x0007;
public const int WM_CAPTURECHANGED = 0x0215;
public const int WM_KILLFOCUS = 0x0008;
public static void Click(IntPtr mainWindowHandle, Point location)
{
/* SendMessage(mainWindowHandle, WM_NCHITTEST, IntPtr.Zero, 105 | (568 << 16));
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_SETCURSOR, (IntPtr)0x00422A84, 0x02000001);
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_MOUSEMOVE, IntPtr.Zero, 105 | (568 << 16));
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_MOUSEACTIVATE, mainWindowHandle, 1 | (WM_LBUTTONDOWN << 16));
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_MOUSEACTIVATE, mainWindowHandle, 1 | (WM_LBUTTONDOWN << 16));
Thread.Sleep(5);
var postChange = new Win32Api.tagWINDOWPOS()
{
cx = 0,
y = 0,
x = 0,
cy = 0,
flags = SWP_NOSIZE | SWP_NOMOVE,
hwnd = mainWindowHandle,
hwndInsertAfter = (IntPtr)0x00580E3C
};
SendMessage(mainWindowHandle, WM_WINDOWPOSCHANGING, IntPtr.Zero, ref postChange);
Thread.Sleep(5);
var postChange2 = new Win32Api.tagWINDOWPOS()
{
cx = 105 + 1,
y = 568,
x = 105,
cy = 568 + 1,
flags = SWP_NOSIZE | SWP_NOMOVE | 1800,
hwnd = mainWindowHandle,
hwndInsertAfter = (IntPtr)0x00580E3C
};
SendMessage(mainWindowHandle, WM_WINDOWPOSCHANGING, IntPtr.Zero, ref postChange2);
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_ACTIVATEAPP, (IntPtr)0x00000001, 0);
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_NCACTIVATE, (IntPtr)0x00000001, 0);
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_ACTIVATE, (IntPtr)0x00000002, 0);
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_IME_SETCONTEXT, (IntPtr)0x00000001, (IntPtr)(1 | (0xC000000F << 16)));
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_IME_NOTIFY, (IntPtr)0x00000002, 0);
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_SETFOCUS, (IntPtr)0, 0);
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_KILLFOCUS, (IntPtr)0x00232B2A, 0);
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_IME_SETCONTEXT, (IntPtr)0x00000000, (IntPtr)(1 | (0xC000000F << 16)));
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_IME_SETCONTEXT, (IntPtr)0x00000001, (IntPtr)(1 | (0xC000000F << 16)));
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_SETFOCUS, (IntPtr)0x00232B2A, 0);
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_IME_SETCONTEXT, (IntPtr)0x00000001, 0x0641);
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_IME_NOTIFY, (IntPtr)0x00000002, 0);
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_SETFOCUS, (IntPtr)0, 0);
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_SETCURSOR, (IntPtr)0x00422A84, 0x02000001);
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_LBUTTONDOWN, (IntPtr)0x00000001, 105 | (568 << 16));
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_LBUTTONUP, (IntPtr)1, 105 | (568 << 16));
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_CAPTURECHANGED, IntPtr.Zero, IntPtr.Zero);
Thread.Sleep(5);
PostMessage(mainWindowHandle, WM_MOUSEMOVE, (IntPtr)0x00000001, (IntPtr)(105 | (568 << 16)));
Thread.Sleep(5);
PostMessage(mainWindowHandle, WM_MOUSEMOVE, (IntPtr)0x00000001, (IntPtr)(105 | (568 << 16)));
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_CAPTURECHANGED, IntPtr.Zero, IntPtr.Zero);
Thread.Sleep(5);
PostMessage(mainWindowHandle, WM_LBUTTONUP, IntPtr.Zero, (IntPtr)(105 | (568 << 16)));
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_CAPTURECHANGED, IntPtr.Zero, IntPtr.Zero);
Thread.Sleep(5);
SendMessage(mainWindowHandle, WM_NCHITTEST, IntPtr.Zero, (IntPtr)0x02DE0ACF);
Thread.Sleep(5);*/
//SendMessage(mainWindowHandle, WM_LBUTTONUP, IntPtr.Zero, (IntPtr)(105 | (568 << 16)));
/* Win32Api.PostMessage(mainWindowHandle, WM_MOUSEACTIVATE, mainWindowHandle,(IntPtr) (1 | (Mouse.WM_LBUTTONDOWN << 16)));
//mouse activate ?
Thread.Sleep(50);
Win32Api.PostMessage(mainWindowHandle, WM_ACTIVATE, (IntPtr)2, (IntPtr)(0 | (0 << 0)));
Thread.Sleep(50);
Win32Api.SendMessage(mainWindowHandle, WM_LBUTTONDOWN, MK_LBUTTON, (105 | (568 << 16)));
Thread.Sleep(50);
Win32Api.SendMessage(mainWindowHandle, WM_LBUTTONUP, 0, (105 | (568 << 16)));
Win32Api.PostMessage(mainWindowHandle, WM_ACTIVATE, (IntPtr)0, (IntPtr)(0 | (0 << 0)));*/
//PostMessage(mainWindowHandle, Mouse.WM_MOUSEACTIVATE, mainWindowHandle, (IntPtr)(1 | (Mouse.WM_LBUTTONDOWN << 16)));
//PostMessage(mainWindowHandle, Mouse.WM_LBUTTONDOWN, IntPtr.Zero,(IntPtr)(72 | (568 << 16))); // 72 568 x | (y << 16)
//Thread.Sleep(50);
//PostMessage(mainWindowHandle, Mouse.WM_LBUTTONUP, (IntPtr)72,(IntPtr)(72 | (568 << 16)));
}
}
}
我尝试重新创建每个已发送/发布,但没有成功。 也许我错过了一些重要的信息? :d