Mouse Vs Attached Mouse methods

Mar 16, 2011 at 5:16 PM
Edited Mar 16, 2011 at 5:17 PM

Hi, I am testing an application that has a 'create rectangle' icon, which when clicked lets me create a rectangle on an image. I have declared a attachedMouse object and want to do mousedown at one location on the image, and then mouseup at another point, so that the rectangle would be drawn. But I see that the only methods available for the attachedMouse object are 'click', 'doubleClick', 'rightClick', or 'RightClickAt(point)'. the mouse object has methods (mouse.LeftDown(), and mouse.LeftUp())  but I am not sure how to use that in my code. I need help with that. I am new to white and appreciate any help/suggestions I could get.

Here is the code snippet:

public static void AddRectangleWindow window)
        {
          
            White.Core.InputDevices.AttachedMouse mouse = window.Mouse;

......

        }

If I declare White.Core.InputDevices.Mouse mouse1 = window.Mouse;

i get the error Error    "Cannot implicitly convert type 'White.Core.InputDevices.AttachedMouse' to 'White.Core.InputDevices.Mouse'"

Is there another way to declare the mouse object?

Mar 17, 2011 at 4:45 PM
Edited Mar 17, 2011 at 4:46 PM

You can get an instance of the Mouse class by using the static property: Mouse.Instance, for example:

 

var mouse = Mouse.Instance;

 

For reference here are a bunch of Extension methods that I wrote for the O2 Platform that make it easier to consume:

 

public static class API_GuiAutomation_ExtensionMethods_Mouse
    {
    	public static T click<T>(this T uiItem)
    		where T : IUIItem
    	{
    		if (uiItem.notNull())
    			uiItem.Click();
    		return uiItem;    		
    	}
    	
    	public static T doubleClick<T>(this T uiItem)
    		where T : IUIItem
    	{
    		uiItem.DoubleClick();
    		return uiItem;    		
    	}
    	
    	public static T rightClick<T>(this T uiItem)
    		where T : IUIItem
    	{
    		uiItem.RightClick();
    		return uiItem;    		
    	}
    	
    	public static T mouse<T>(this T uiItem)
    		where T : IUIItem
    	{    		
    		if (uiItem.notNull())	    			
    			return uiItem.mouse_MoveTo();
			return default(T);    		
    	}
    	
    	public static T mouse_MoveTo<T>(this T uiItem)
    		where T : IUIItem
    	{
    		if (uiItem.notNull())
    		{    			
    			var location = uiItem.Bounds.Center();
    			Mouse.Instance.mouse_MoveTo(location.X, location.Y,true);
    		}
    		return uiItem;    		
    	}

			
		public static T mouse_MoveTo_WinForm<T>(this T control)		
			where T : System.Windows.Forms.Control 
		{
			var location1 = control.PointToScreen(System.Drawing.Point.Empty); 
			var xPos = (double)location1.X + control.width()/2;
			var yPos = (double)location1.Y  + control.height()/2;
			Mouse.Instance.mouse_MoveTo(xPos, yPos, true);		
			return control;
		}
    	
    	public static API_GuiAutomation mouse_MoveTo(this API_GuiAutomation guiAutomation, double x, double y)
    	{    		
    		Mouse.Instance.mouse_MoveTo(x, y, true);
    		return guiAutomation;
    	}    	
    	
    	public static Mouse mouse_MoveTo(this Mouse mouse, double x, double y, bool animate)
    	{
    		System.Windows.Forms.Application.DoEvents();
    		//"__moving mouse to:{0} {1}".debug(x,y);
    		return 	mouse.mouse_MoveBy(x - mouse.Location.X , y - mouse.Location.Y, animate);
    	}
    	
    	public static API_GuiAutomation mouse_MoveBy(this API_GuiAutomation guiAutomation, double x, double y)
    	{
    		Mouse.Instance.mouse_MoveBy(x, y, true);
    		return guiAutomation;
    	}
    	
    	public static T mouse_MoveBy<T>(this T uiItem, double x, double y)
    		where T : IUIItem
    	{
    		Mouse.Instance.mouse_MoveBy(x, y, true);
    		return uiItem;
    	}
    	
    	public static Mouse mouse_MoveBy(this Mouse mouse, double x, double y, bool animate)
    	{
    		var originalX = mouse.Location.X;
    		var originalY = mouse.Location.Y;
    		"moving mouse by:{0} {1}".debug(x,y);
    		if (animate)
    		{    			    		
	    		if (x != 0 || y != 0)
	    		{    			
	    			double currentX = mouse.Location.X;
	    			double currentY = mouse.Location.Y;
	    			//"__Start position mouse to :{0}x{1}".debug(currentX, currentY);
	    			double numberOfSteps = (Math.Abs(x) > Math.Abs(y)) ? Math.Abs(x) : Math.Abs(y);
	    			double xStep = ((x != 0) ? x / numberOfSteps : 0) * API_GuiAutomation.Mouse_Move_SkipValue;
	    			double yStep = ((y != 0) ? y / numberOfSteps : 0) * API_GuiAutomation.Mouse_Move_SkipValue;
	    			for(int i =0 ; i < numberOfSteps ; i += API_GuiAutomation.Mouse_Move_SkipValue)  
	    			{
	    				currentX += xStep; //(x >0) ? -xStep : -xStep;
	    				currentY += yStep; //(y >0) ? -yStep : -yStep;
	    				//"Moving mouse to :{0}x{1}".info(currentX, currentY);
	    				mouse.Location = new System.Windows.Point(currentX, currentY);
	    				//System.Windows.Forms.Application.DoEvents();
	    				if (API_GuiAutomation.Mouse_Move_SleepValue >0)
	    					mouse.sleep(API_GuiAutomation.Mouse_Move_SleepValue,false);
	    			}
	    			
	    		}
    		}
    		// this makes sure we always end up in the desired location (namely for the cases where the Mouse_Move_SkipValue is quite high)
    		mouse.Location = new System.Windows.Point(originalX + x, originalY + y);    			
    		return mouse;    		
    	}
    	
    	public static Mouse mouse(this API_GuiAutomation guiAutomation)
    	{
    		return Mouse.Instance;
    	}
    	
    	public static Mouse click(this Mouse mouse)
    	{
    		mouse.Click();
    		return mouse;
    	}
    	
    	public static Mouse doubleClick(this Mouse mouse)
    	{
    		var location = mouse.Location;
    		mouse.DoubleClick(location);
    		return mouse;
    	}
    	
    	public static Mouse rightClick(this Mouse mouse)
    	{
    		mouse.RightClick();
    		return mouse;
    	}
    	
    	public static API_GuiAutomation mouse_Click(this API_GuiAutomation guiAutomation)
    	{
    		guiAutomation.mouse().click();
    		return guiAutomation;
    	}
    	
    	public static API_GuiAutomation mouse_DoubleClick(this API_GuiAutomation guiAutomation)
    	{
    		guiAutomation.mouse().doubleClick();    		
    		return guiAutomation;
    	}
    	
    	public static API_GuiAutomation mouse_RightClick(this API_GuiAutomation guiAutomation)
    	{
    		guiAutomation.mouse().rightClick();
    		return guiAutomation;
    	}
    }