using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            ////移动到左上角
            Cursor.Position = new Point(Screen.PrimaryScreen.WorkingArea.Left, Screen.PrimaryScreen.WorkingArea.Top);
            //System.Threading.Thread.Sleep(1000);
            ////移动到右上角
            //Cursor.Position = new Point(Screen.PrimaryScreen.WorkingArea.Right, Screen.PrimaryScreen.WorkingArea.Top);
            int dx = 10 * 65535 / Screen.PrimaryScreen.Bounds.Width;
            int dy = 10 * 65535 / Screen.PrimaryScreen.Bounds.Height;            //mouse_event((int)MouseEventFlags.LeftDown, 0, 0, 0, UIntPtr.Zero);            ///MessageBox.Show(Screen.PrimaryScreen.Bounds.Width.ToString());
            //改变鼠标位置OK
            Cursor.Position = new Point(1000,100);
            System.Threading.Thread.Sleep(1000);
            //
            //右键OK
            mouse_event((int)MouseEventFlags.RightDown, 0, 0, 0, IntPtr.Zero);
            System.Threading.Thread.Sleep(1000);
            Cursor.Position = new Point(800,200);
            //改变鼠标位置OK
            //右键 这里不OK ??是怎么回事
            mouse_event((int)MouseEventFlags.RightDown, 0, 0, 0, IntPtr.Zero);
            
        }        [Flags]
        public enum MouseEventFlags
        {
            Move = 0x0001,
            LeftDown = 0x0002,
            LeftUp = 0x0004,
            RightDown = 0x0008,
            RightUp = 0x0010,
            MiddleDown = 0x0020,
            MiddleUp = 0x0040,
            Wheel = 0x0800,
            Absolute = 0x8000
        }        [DllImport("user32.dll")]
        public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);    }
}

解决方案 »

  1.   


    //右键 这里不OK ??是怎么回事
    mouse_event((int)MouseEventFlags.RightDown, 0, 0, 0, IntPtr.Zero);
    //为什么第一次是OK的而第二次却不行呢
    谢谢
      

  2.   

    这样就ok了////移动到左上角
                Cursor.Position = new Point(Screen.PrimaryScreen.WorkingArea.Left, Screen.PrimaryScreen.WorkingArea.Top);
                //System.Threading.Thread.Sleep(1000);
                ////移动到右上角
                //Cursor.Position = new Point(Screen.PrimaryScreen.WorkingArea.Right, Screen.PrimaryScreen.WorkingArea.Top);
                int dx = 10 * 65535 / Screen.PrimaryScreen.Bounds.Width;
                int dy = 10 * 65535 / Screen.PrimaryScreen.Bounds.Height;            //mouse_event((int)MouseEventFlags.LeftDown, 0, 0, 0, UIntPtr.Zero);            ///MessageBox.Show(Screen.PrimaryScreen.Bounds.Width.ToString());
                //改变鼠标位置OK
                Cursor.Position = new Point(1000, 100);
                System.Threading.Thread.Sleep(1000);
                //
                //右键OK
                mouse_event((int)MouseEventFlags.RightDown, 0, 0, 0, IntPtr.Zero);
                mouse_event((int)MouseEventFlags.RightUp, 0, 0, 0, IntPtr.Zero);
                System.Threading.Thread.Sleep(1000);
                Cursor.Position = new Point(800, 200);
                //改变鼠标位置OK
                //右键 这里不OK ??是怎么回事
                
                mouse_event((int)MouseEventFlags.RightDown, 0, 0, 0, IntPtr.Zero);
                mouse_event((int)MouseEventFlags.RightUp, 0, 0, 0, IntPtr.Zero);
      

  3.   


    Windows.Forms.Cursor.Position = new Point(1000, 100); 
        System.Threading.Thread.Sleep(10); 
        
        mouse_event((int)MouseEventFlags.RightDown, 0, 0, 0, IntPtr.Zero); 
        System.Threading.Thread.Sleep(10); 
        mouse_event((int)MouseEventFlags.RightUp, 0, 0, 0, IntPtr.Zero); 
        System.Threading.Thread.Sleep(1000); 
        
        Windows.Forms.Cursor.Position = new Point(800, 200); 
        mouse_event((int)MouseEventFlags.RightDown, 0, 0, 0, IntPtr.Zero); 
        System.Threading.Thread.Sleep(10); 
        mouse_event((int)MouseEventFlags.RightUp, 0, 0, 0, IntPtr.Zero);