关于使用SendMessage PostMessage 实现模拟鼠标的一些问题

ActionEnd 2014-01-12 11:44:45
想实现一个通过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");

}

}
}




以上代码能成功的编译,但是目标对象的窗口只能接收到我的鼠标左键按下的消息,无法接收到鼠标位置的消息,请教各位大神是不是哪里出现了问题?
...全文
573 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ActionEnd 2014-01-12
  • 打赏
  • 举报
回复
引用 3 楼 bdmh 的回复:
对啊,你跟没也没法送鼠标位置啊
那请问,SetCursorPos这个方法是不可以后台的,那我如何用SendMessage这个方法去模拟鼠标的点击呢?还是说SetCursorPos这个方法是可以后台的呢?
ActionEnd 2014-01-12
  • 打赏
  • 举报
回复
引用 2 楼 ActionEnd 的回复:
[quote=引用 1 楼 tcmakebest 的回复:] 搞笑,代码中也没有代码让鼠标移动位置啊. 应该要用 SetCursorPos 方法.
是不是应该让鼠标线移动到某个位置,然后再用sendmessage发送信息呢?[/quote] 我刚刚查了一下,SetCursorPos方法只能是前台的,但是,我既然用了SendMessgae了,我就想实现后台的鼠标模拟....
bdmh 2014-01-12
  • 打赏
  • 举报
回复
对啊,你跟没也没法送鼠标位置啊
ActionEnd 2014-01-12
  • 打赏
  • 举报
回复
引用 1 楼 tcmakebest 的回复:
搞笑,代码中也没有代码让鼠标移动位置啊. 应该要用 SetCursorPos 方法.
是不是应该让鼠标线移动到某个位置,然后再用sendmessage发送信息呢?
tcmakebest 2014-01-12
  • 打赏
  • 举报
回复
搞笑,代码中也没有代码让鼠标移动位置啊. 应该要用 SetCursorPos 方法.
ActionEnd 2014-01-12
  • 打赏
  • 举报
回复
难道,没有人对这块比较熟悉的吗??

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧