首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

2014-01-17 
关于使用SendMessage PostMessage 实现模拟鼠标的一些问题想实现一个通过PostMessage 或者 SendMessage在

关于使用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 方法.
[解决办法]
对啊,你跟没也没法送鼠标位置啊

热点排行