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
Related
The image with the compass and other UI is actually the top left corner and the tooltip is spawning outside the canvas as you can see.
Ive been searching for hours now on how to fix this:
Im using a Screen Space - Overlay Canvas.
Setting an UI Elements Anchored position through code to make it follow the mouse.
Vector2 anchoredPosition = Input.mousePosition;
rectTransform.anchoredPosition = anchoredPosition;
However this doesnt retun the right position.
I tried Input.mousePosition / Canvas.scaleFactor;
And I also tried Input.mousePosition / canvasRectTransform.localScale.x;
And finally Camera.main.ScreenToViewportPoint(Input.mousePosition);
But again, no luck at all... Free Aspect doesnt fix it either.
Changing the reference width and height doesnt fix it, setting from 16:9 to 16:10 does nothing to help me either.
The UI element is always way outside the actual canvas (camera view).
The UI element itself is a RectTransform with size of 1,1 with the anchor in the middle. Testing it by setting element position to 0,0 shows it perfectly centered.
Can someone please help me?
Thank you.
EDIT:
I got it to work by changing the reference resolution on the Canvas until I saw the canvas scale was exactly 1,1,1. Ofcourse I dont want this reference resolution, but atleast it tells us that it has to do with that:
Mouse position is read on screen coordinates. Canvas position is a combination of the canvas reference resolution, your actual screen size, and the canvas’ scale factor.
Say your screen is 1080p and your canvas reference resolution is 1080p. And the canvas is set to fill your screen. The coordinates will directly line up between mouse position and canvas position.
With different screen resolutions than the reference resolution, this Gets more complex to calculate. But there’s a handy function to handle this for you.
RectTransformUtility.ScreenPointToLocalPointInRectangle
https://docs.unity3d.com/ScriptReference/RectTransformUtility.ScreenPointToLocalPointInRectangle.html
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
I am trying to implement some kind of snapping functionality in WPF for a circle (which represents my mouse) and it should snap to another object (normally this would be a line or a rectangle).
Is there a way to do this kind of functionality with WPF without doing all the calculations on my own and if not is there an easy way (library?) to get this kind of information?
Edit: I want to snap the border of the circle to the border of the rectangle/line.
As a first step, you should find the point on the rectangle that is the closest to the cursor, and the distance between the two: extending the edges of the rectangle, you partition the plane into 9 regions. Depending on the region where the cursor lies, the searched distance will be the distance to a corner (Euclidean distance formula) or the distance to an edge (difference of abscissas or ordinates).
Subtract the circle radius from this distance. This will tell you if you are close enough for a snap.
When a snap is possible, move the cursor along the line from the current cursor position to the closest point until you hit the corner or edge. You will need to use the parametric equation of the line segment.
The complete discussion requires some care but only involves simple math.
A similar approach is possible to snap to a line segment. Here is a trick: if you rotate the line segment to make it horizontal, you can consider the line segment as a degenerate rectangle and use the same snapping algorithm. Rotate the line segment and the cursor, apply the snapping logics and then counter-rotate the updated cursor.
That kind of functionality only takes a few lines of code to replicate... I doubt that you'll find a 'library' of code to do it for you. The method is as follows:
Keep a collection that contains the 4 Points that form each shape's bounding box. You then need to handle to MouseMove event on the Canvas, or shape container. In this event, you simply need to ascertain whether the current mouse position is within a certain distance from any of the shape edges... you'll have a little bit more work to do with non-rectangular shapes to calculate their edges, but the principal is the same.
If you detect the presence of a nearby shape, then you simply need to change the value of the nearest dimension to that of the nearby shape... the snap. That's it... much easier than you think.
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.
There is a picture in my winform, I want to show pixel coordinates when mouse is moving, and
whether zoom in or out, the same pixel's coordinates don't change, just like what paint.net does.
Appreciate for any idea.
You need to handle the MouseMove event and get the location from e.X and e.Y.
You can divide the location by your zoom factor.
about the mouse coordinate, you should use Matrix to track back mouse coordinate.
see: Back track the mouse