关于使用SendMessage PostMessage 实现模拟鼠标的一些问题
想实现一个通过PostMessage 或者 SendMessage在后台对一个DX游戏的窗口进行鼠标的模拟。
以下为代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll", EntryPoint = "SendMessage")]
static extern bool PostMessage(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
public static uint WM_LBUTTONDOWN = 0x201;
//按下鼠标左键
public static uint WM_LBUTTONUP = 0x202;
//释放鼠标左键
private void Form1_Load(object sender, EventArgs e)
{
}
public static IntPtr gethWnd()
{
String s = "EVE - xxxxx";
IntPtr hWnd = FindWindow("triuiScreen", s);
if (hWnd == IntPtr.Zero)
{
MessageBox.Show("error");
return IntPtr.Zero;
}else
{
return hWnd;
}
}
IntPtr hWnds = gethWnd();
private void button1_Click(object sender, EventArgs e)
{
int x = 60;
int y = 85;
IntPtr p = (IntPtr)((y << 16) | x);
IntPtr wParam = IntPtr.Zero;
PostMessage(hWnds, WM_LBUTTONDOWN, wParam, p);
PostMessage(hWnds, WM_LBUTTONUP, wParam, p);
Application.DoEvents();
MessageBox.Show("Successful");
}
}
}
[解决办法]
搞笑,代码中也没有代码让鼠标移动位置啊.
应该要用 SetCursorPos 方法.
[解决办法]
对啊,你跟没也没法送鼠标位置啊