예제 #1
0
        /// <summary>
        ///   Simulate pressing the mouse button(s) at the specified <see cref="PointF"/>.
        /// </summary>
        /// <param name="buttons">
        ///   A bitwise combination of the <see cref="MouseButtons"/> enumeration values.
        /// </param>
        /// <param name="point">
        ///   A <see cref="PointF"/>, relative to the control under test,
        ///   to <see cref="Position">move</see> the mouse to.
        /// </param>
        /// <remarks>
        ///   <b>PressAndRelease</b> positions the mouse over the control under test
        ///   at the specified <paramref name="point"/>
        ///   and then simulates pressing the specified <paramref name="buttons"/>.
        /// </remarks>
        /// <exception cref="NotSupportedException">
        ///   When <paramref name="buttons"/> contains <see cref="MouseButtons">MouseButtons.XButton1</see>
        ///   or <see cref="MouseButtons">MouseButtons.XButton2</see> and the installed mouse does have 4
        ///   or 5 buttons, respectively.
        /// </exception>
        public void Press(MouseButtons buttons, PointF point)
        {
            Win32.MOUSEINPUT mi = new Win32.MOUSEINPUT(0);
            if ((buttons & MouseButtons.Left) != 0)
            {
                mi.dwFlags |= Win32.MOUSEEVENTF_LEFTDOWN;
            }
            if ((buttons & MouseButtons.Right) != 0)
            {
                mi.dwFlags |= Win32.MOUSEEVENTF_RIGHTDOWN;
            }
            if ((buttons & MouseButtons.Middle) != 0)
            {
                mi.dwFlags |= Win32.MOUSEEVENTF_MIDDLEDOWN;
            }
            if ((buttons & MouseButtons.XButton1) != 0)
            {
                if (SystemInformation.MouseButtons < 4)
                {
                    throw new NotSupportedException("A mouse with at least 4 buttons is required.");
                }
                mi.dwFlags   |= Win32.MOUSEEVENTF_XDOWN;
                mi.mouseData |= Win32.XBUTTON1;
                //mi.mouseData = Win32.XBUTTON1;
            }
            if ((buttons & MouseButtons.XButton2) != 0)
            {
                if (SystemInformation.MouseButtons < 5)
                {
                    throw new NotSupportedException("A mouse with at least 5 buttons is required.");
                }
                mi.dwFlags   |= Win32.MOUSEEVENTF_XDOWN;
                mi.mouseData |= Win32.XBUTTON2;
                //mi.mouseData = Win32.XBUTTON2;
            }

            Position = point;
            if (mi.dwFlags != 0)
            {
                if (0 == Win32.SendMouseInput(1, ref mi, Marshal.SizeOf(mi)))
                {
                    throw new Win32Exception();
                }
                Application.DoEvents();
            }
        }
예제 #2
0
		/// <summary>
		///   Simulate releasing the mouse button(s) at the specified <see cref="PointF"/>.
		/// </summary>
		/// <param name="buttons">
		///   A bitwise combination of the <see cref="MouseButtons"/> enumeration values.
		/// </param>
		/// <param name="point">
		///   A <see cref="PointF"/>, relative to the control under test,
		///   to <see cref="Position">move</see> the mouse to.
		/// </param>
		/// <remarks>
		///   <b>Release</b> positions the mouse over the control under test
		///   at the specified <paramref name="point"/>
		///   and then simulates releasing the specified <paramref name="buttons"/>.
		/// </remarks>
		/// <exception cref="NotSupportedException">
		///   When <paramref name="buttons"/> contains <see cref="MouseButtons">MouseButtons.XButton1</see>
		///   or <see cref="MouseButtons">MouseButtons.XButton2</see> and the installed mouse does have 4
		///   or 5 buttons, respectively.
		/// </exception>
		public void Release(MouseButtons buttons, PointF point)
		{
			Win32.MOUSEINPUT mi = new Win32.MOUSEINPUT(0);
			if((buttons & MouseButtons.Left) != 0)
			{
				mi.dwFlags |= Win32.MOUSEEVENTF_LEFTUP;
			}
			if((buttons & MouseButtons.Right) != 0)
			{
				mi.dwFlags |= Win32.MOUSEEVENTF_RIGHTUP;
			}
			if((buttons & MouseButtons.Middle) != 0)
			{
				mi.dwFlags |= Win32.MOUSEEVENTF_MIDDLEUP;
			}
			if((buttons & MouseButtons.XButton1) != 0)
			{
				if(SystemInformation.MouseButtons < 4)
				{
					throw new NotSupportedException("A mouse with at least 4 buttons is required.");
				}
				mi.dwFlags |= Win32.MOUSEEVENTF_XUP;
				mi.mouseData = Win32.XBUTTON1;
			}
			if((buttons & MouseButtons.XButton2) != 0)
			{
				if(SystemInformation.MouseButtons < 5)
				{
					throw new NotSupportedException("A mouse with at least 5 buttons is required.");
				}
				mi.dwFlags |= Win32.MOUSEEVENTF_XUP;
				mi.mouseData = Win32.XBUTTON2;
			}

			if(mi.dwFlags != 0)
			{
				Position = point;
				if(0 == Win32.SendMouseInput(1, ref mi, Marshal.SizeOf(mi)))
				{
					throw new Win32Exception();
				}
				Application.DoEvents();
			}
		}