I'm trying to get the position of the cursor from a multi-monitor display. Using
Point cursorPosition = Cursor.Position;
Returns the cursor's position relative to the monitor it is currently on. If I had two monitors, and I used the above lines of code, and my mouse was on my second monitor (Which is to the right of my first monitor), and I drew the cursor to an image, it would appear on the left hand side monitor, where as it should be on the right hand side monitor.
EDIT:
I'm taking a screenshot of all my monitors and would like to add the cursor to the Bitmap, however using Cursor.Position will put the cursor on the "left hand side" monitor of the screenshot Bitmap even if the cursor was on the "right hand side" monitor.
Thanks
You can get screen where cursor is:
var screen = Screen.FromPoint(Cursor.Position);
Take screen into account before you display your image
Related
I am currently working on a dual monitor system where the secondary monitor is virtual. My requirements are that I connect to this via Teamviewer and run a customer application on the secondary screen which will control the mouse cursor on the primary. At this stage no code exists and I am just trying to wrap my head around what needs to be done.
My issue is that the coordinates seem to begin at the secondary screen and extend positively onto the primary and negatively onto the primary screens ( see screenshot of a mouse tracking app with the cursor on the primary screen)
I am able to work with the mouse in positive coordinates but trying to move to the left of the secondary screen requires x to be below 0. Can anyone suggest how I can work with this?
Whilst experimenting with basic mouse movement routines found online I was using this:
private void MoveCursor(int xpos, int ypos)
{
// Set the Current cursor, move the cursor's Position,
// and set its clipping rectangle to the form.
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(xpos, ypos);
//Cursor.Clip = new Rectangle(this.Location, this.Size);
}
and discovered that the cursor.clip line I have commented out was for whatever reason forcing the cursor to remain on the primary screen. Thanks everyone.
I have two image in my unity scene.
One of this images is an square as background Recttransform and another image is a button.
I use Drag Event for this button to detect movement in run time.
But I want to move this button only inside the background image.
And I don't know how to make this limitation.
Is there any solution for this issue ?
For more explanation please see this image :
Off the top of my head, here are a two ways to do this:
Create a few invisible box colliders and place them at the edge of the red square. Probably easiest but more prone to issues when screen size changes.
Get Screen.width, Screen.height, Rect.width, and Rect.height. Only allow drag up to the edges of the screen minus 1/2 Rect.width and 1/2 Rect.height.
I need a way to map some absolutes coordinates (relative to main screen) to relative coordinates to some screen.
For example, if you have two screens configured, if the main screen is the one of the right, there are some valid negative coordinates that point to the left screen. I need to convert these absolute coordinates always to a positive number relative to the left screen.
Edit: the goal is to know if some absolute coordinates are valid in some available screen using
Screen.WorkingArea.Contains(thePoint),
iterating over the available screens.
Thank you
This should do for any screen configuration:
static Point GetRelativeCoordinates(Point absoluteCoordinates)
{
var screen = Screen.FromPoint(absoluteCoordinates);
return new Point(absoluteCoordinates.X - screen.Bounds.Left, absoluteCoordinates.Y - screen.Bounds.Top);
}
The Bound property stores the absolute coordinates of the Top and Left corners of the screen ([0, 0] being the top, left of the primary screen) so converting absolute coordinates to relative ones is pretty straightforward no matter how many screens you have.
I haven't tested the code but it should work.
What I'm actually trying to do, is to get the mouse coordinates inside a windowed game. So far, I've only found ways to retrieve the screen coordinates of the mouse. Therefore, I would like to know the position of the window, so that I can subtract that from the mouse's screen coordinates in order to get the mouse's window coordinates.
Is this possible, and if so, how?
Have you tried Mouse.GetState? It returns a MouseState with X and Y properties on it. The documentation on MSDN is pretty sparse, but if you look carefully at documentation for the specific X and Y properties you'll see that it returns a position relative to the upper left corner of the window which is more in line with what your after.
MouseState.X - Horizontal position of the mouse cursor in relation to the upper-left corner of the game window.
MouseState.Y - Vertical position of the mouse cursor in relation to the upper-left corner of the game window.
The advantage of doing this way is that it's not platform specific, so if you port your game to another platform in the future, the code won't have to change.
the problem is that I try to get current position of picturebox. When I use MouseEvenArgs everything is ok. But when I try to detect position using
Cursor.Position.X;
It doesn't work the right way. The difference is near 20-30px. It seems to me that it gives me a bit different positions.
So, the question: is there any difference between the positions that were get byMouseEventArgs or Cursor.Position? And are there any other ways to detect my current cursor position without using Mouse Events?
Cursor.Position measures screen coordinates. You can map between screen and client (the form) coordinates using PointToClient and PointToScreen methods.
MouseEventArgs.GetPosition returns
the position of the mouse pointer relative to the specified element.
MSDN reference - Windows Forms Coordinates
The default clipping rectangle of the Cursor is the whole screen and the coordinates you receive from MouseMove are translated to the current control:
http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position.aspx