本人是个菜鸟,呵呵在玩游戏时想在游戏里实现自动点击鼠标右键的功能,自己写了个程序,出现一些问题:
   1.在别的地方,程序运行正常,比如在桌面等。
   2.在游戏里应该也是点击了,因为看得出来:鼠标点击已经自动把游戏窗口点为“当前激活窗口”了可是在游戏里没有用!!
(即不能实现自动完成加血功能!)
  3.我写的程序和游戏一起运行时,几乎每次都失去响应!!他为什么会失去响应呢??
   以下是程序的源代码,请指点一下:
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 MyMouse
{
    public partial class Form1 : Form
    {
        [StructLayout(LayoutKind.Sequential)]
        struct NativeRECT   //结构
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }        [Flags]
        enum MouseEventFlag : uint  //枚举
        {
            Move = 0x0001,
            LeftDown = 0x0002,
            LeftUp = 0x0004,
            RightDown = 0x0008,
            RightUp = 0x0010,
            MiddleDown = 0x0020,
            MiddleUp = 0x0040,
            XDown = 0x0080,
            XUp = 0x0100,
            Wheel = 0x0800,
            VirtualDesk = 0x4000,
            Absolute = 0x8000
        }
        bool flag = true;
       
        [DllImport("user32.dll")]
        static extern bool SetCursorPos(int X, int Y);
        //该函数可以改变鼠标指针的位置。其中X,Y是相对于屏幕左上角的绝对位置        [DllImport("user32.dll")]
        static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
        // 这个函数不仅可以设置鼠标指针绝对的位置,而且可以以相对坐标来设置。
        //另外,该函数还可以模拟鼠标左右键点击、鼠标滚轮操作等。其中的MouseEventFlag是一个基于uint类型的枚举        [DllImport("user32.dll")]
        static extern IntPtr FindWindow(string strClass, string strWindow);        [DllImport("user32.dll")]
        static extern IntPtr FindWindowEx(HandleRef hwndParent, HandleRef hwndChildAfter, string strClass, string strWindow);        [DllImport("user32.dll")]
        static extern bool GetWindowRect(HandleRef hwnd, out NativeRECT rect);        public Form1()
        {
            InitializeComponent();
        }        private void btnStart_Click(object sender, EventArgs e)
        {
           int time = int.Parse(this.txtTime.Text);
           IntPtr hWnd = FindWindow(null, "Chin Online 1.1029.1 Beta - 网通1区 石破天惊");
            if (hWnd != IntPtr.Zero)
            {
                while (flag)
                {
                    System.Threading.Thread.Sleep(time * 1000);
                    mouse_event(MouseEventFlag.RightDown, 0, 0, 0, UIntPtr.Zero);
                    System.Threading.Thread.Sleep(200);
                    mouse_event(MouseEventFlag.RightUp, 0, 0, 0, UIntPtr.Zero);
                    if (flag == false)
                        break;
                }
            }
        }        private void btnOver_Click(object sender, EventArgs e)
        {
            flag = false;
        }
    }
}

解决方案 »

  1.   

    http://hi.baidu.com/starsblue/blog/item/3f7626c4072cbdad8226acbb.html
    http://bbs.vrbrothers.com/viewthread.php?tid=48277
      

  2.   

    mouse_event需要找句柄吗? mouse_event是模拟鼠标点击,就是在现在鼠标停留的位置点击IntPtr hWnd = FindWindow(null, "Chin Online 1.1029.1 Beta - 网通1区 石破天惊"); 这句是不是找到游戏的句柄了?找到了直接sendmessage鼠标右键消息过去不可以?
      

  3.   

    要用mouse_event就要先设置鼠标的位置
     Cursor.Position = new Point(x,y);
    x,y可以是游戏窗口的任意一点的坐标
      

  4.   

    很多游戏屏蔽了api控件
    我自己也试过向游戏发送键盘指令,结果失败
      

  5.   

    发送个鼠标不会有什么问题。
    关键是窗口。
    你试着先用鼠标点。然后再模仿。
    不要用FINDWINDOW
    这个只是找到最外层的窗口。并不是窗口中的窗口。