How do i get the mouse position? - c#

-edit- I dont need the answer anymore now that i calculate based on mousedown position.
Basically in relation to this question It seems like e.X,y and e.Location.X,Y is relative to mousedown? I seen negative values when i havent moved it left/above the parent picbox.
So it seems like the values are relative to my mousedown, but i need values relative to the parent because i update the left/top every event and other datat and relative to mousedown is breaking my code.

Did you try to do this ( assuming your control is named picbox )
Point pt = picbox.PointToClient(point)

Related

Unexpected output from checking if mouse within control

I am implementing a custom drag and drop interface with winForm Buttons and after viewing several solutions on how to obtain mouse position and check it against control bound have not been able to get it to work.
I have tried:
button.ClientRectangle.Contains(PointToClient(Cursor.Position))
and
button.ClientRectangle.Contains(PointToClient(Control.MousePosition))
Both of these have failed to work. Checking the mouse bounds seem like a simple operation, but I really am stumped.
My speculation of the unexpected values are:
Process of obtaining cursor position may be in wrong corner of cursor image
Method/Function does not work on Buttons for some reason
You are using the wrong object reference, calculating the mouse position relative to the form instead of the button. And you are writing it in a way that make this very hard to debug. Fix:
var pos = button.PointToClient(Cursor.Position);
System.Diagnostics.Debug.WriteLine(pos); // Now it is easy
if (button.ClientRectangle.Contains(pos)) {
// etc...
}

Recover previous absolute position for PictureBox when Parent is changed?

In my GUI, i need to change Parents of PictureBoxes to the background PictureBox so that the transparency works correctly.
However, changing the parent also changes the location of the pictureboxes. I have tried grabbing the absolute location of the picture box via the PointToClient, but it doesn't work. I put the coordinates in the comments, and they don't change after assigning the new parent even though the image visibly changes location. Furthermore, I don't expect that it could possibly work as it's being passed a point, not an object with more information about parents and whatnot that's needed to deduce the absolute position.
What is the correct way to deduce the absolute position of an element so that I can move the image to the correct location after its parent changes? Or is there a better way to do this?
Point oldRel = pictureBox4.Location; //258, 109
Point oldAbs = PointToClient(oldRel); //75, -96
//Commenting out this line fixes the image shift but ruins the transparency
pictureBox4.Parent = pictureBox2;
Point newRel = pictureBox4.Location; //258, 109
Point newAbs = PointToClient(pictureBox4.Location); //75, -96
This will move a Control child from one Parent to a new one, keeping the absolute screen position intact:
void MoveTo(Control child, Control newParent )
{
child.Location = newParent.PointToClient(child.PointToScreen(Point.Empty));
child.Parent = newParent;
}
The trick with PointToClient and PointToScreen is to use them from the right parent control; not setting the control will default to the Form, which will miss out on the actual position of the parent..

How to know where the cursor is in a WpfEdit field with CodedUI automation?

On the CodedUI WpfEdit class there is a way to get the selected text, but I cannot find a way to get the cursor position when nothing is selected (i.e. the index of the caret in the text). Is there anything available for that in the CodedUI framework?
My goal is to assert the position of the cursor in the text contained by the control.
There isn't a codedui method for that but try the following:
add the reference:
using System.Windows.Forms
in the code where you need to get the moue coordinated type:
Point p = new Point(Cursor.Position.X, Cursor.Position.Y);
remember that this is not a relative position to the control but the position of the mouse on the screen.
calculating the position of the point relative to control shouldn't be much problem.
I'm not sure there is a way, and I would imagine that there is a different requirement than actually finding the cursor position.
If you are trying to insert some text, you can always copy the text out to the test method, insert the text, and write it back.
Or, if you need to not do that, you could always use the Keyboard.SendKeys method to send a home command and then any number of right arrow commands you need to place the cursor where you'd like it.
Can you elaborate further as to what exactly you need with the cursor position?

How to get the mouse position and use it in the client

I am making a form which has a ListView. I want to show a "Picture Window" whenever mouse hovers above one of the listView Item in the ListView. I implemented this by using "ListView"'s "ItemMouseHover" event. I want to show this "Picture Window" just near to the mouse position. But , I couldn't get the mouse position from the itemmousehover event. Can anybody say how to achieve this?
Note: I am using .Net 4.0
EDIT:
"Danbystrom" gave the exact answer. But I found the root cause is some other. See my answer for that.
The static method Control.MousePosition will give you the coordinates in screen space. Then you can convert that into client coordinates with the Control.PointToClient instance method.
var pt = listView.PointToClient( Control.MousePosition );
The new window will be placed on your will, only if you choose "Manual" instead of "WindowsDefaultLocation" in the Property "StartPosition". Otherwise "OS" will try to put your new window's location somewhere near to the mouse position. If the new window hides the mouse in that position, then it will try through some nearby points which will result in flickering.

Find Coordinates for point on screen?

The thing is I have some graphics shown in a form, rectangle for example, and I want to capture when the point gets over thees fields. So I Thoght I try to find the corrrdinates for thees rectangles, but as thay are the coords in the form it dose not match the ones with the mouse location.
So I wonder is there a way to find what coord on the screen a Point has on the screen and not in the form or controller?
Each control hs PointToFoo methods for conversion. Note that you should call this from the parent of the object who's location you want:
Point scrPos = this.PointToScreen(panel1.Location);
Alternatively, you can get the panel's screen coordinates with:
Point scrPos = panel1.PointToScreen(new Point(0,0));
Note that the above two examples could gve different result due to the border-size of the panel.
If you are using the graphics object for the form by calling this.CreateGraphics() within the form, then the coordinates used when you draw the rectangle should be exactly the same as those returned by the click event on the form.
Do you know what coordinates your pointer is in? You can get the coordinates for your window with a call to GetWindowRect() and subtract the top/left from your mouse cursor to get client coordinates.
I seem to remember there being a function to do that for you in fact, but it's been some time since I dabbled in custom GUI controls.

Categories