Hi I have been using code similar to this in a piece of automation I have been working on
public static void LeftClick(int x, int y)
{
Cursor.Position = new System.Drawing.Point(x, y); //<= fails without this
mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);
}
However unless I am being dumb this move the mouse to the x,y from the top left of the screen which causes me problems if the active window isn't where Im expecting it to be, can anyone suggest a way of achieving the same functionality with moving the mouse to a point relative to the active window.
Thanks
You need to pinvoke GetWindowRect() to find out where the window is located. So you can adjust x and y by the window position. Visit pinvoke.net for the declarations.
Just subtract the location (relative to the screen) of the window you are targeting.
What you're seeing is indeed the expected behavior. The Cursor.Position property describes the cursor's location in screen coordinates, not relative to your form.
However, every control exposes two handy methods that you can take advantage of to convert between screen coordinates and control coordinates:
The Control.PointToClient method computes the location of the specified screen point into client coordinates. Use this to convert from screen coordinates into client coordinates (i.e., those relative to your control, such as a form).
The Control.PointToScreen method computes the location of the specified client point into scren coordinates. Use this to convert from client coordinates into screen coordinates.
try PointToClient and PointToScreen of the control you are trying to find relative points to.
Related
I created simple form and now I want to draw single objects (rectangle, circle, lines..) in relative coordinates.
Main problem for me here is to create 2D cartesian coordinate system in middle of the form.Is it possible and how to do it??
Main question is how to efficiently transform absolute coordinates to relative? How to create my own system so I get results in numbers (negative, positive depending on quadrant) and not in pixel?
.
Currently I made MouseMove event to display current location of mouse with Cursor.Position.X, Cursor.Position.Y and display it in label. Displayed coordinates are pixels, how to change that? I've read something about converting with PointToClient method but I don't really understand it.
I am tied to windows forms becouse I already made a program in win forms and now I just want to add this feature to it.
Thanks
What you're most likely looking for are Graphics.TranslateTransform and Graphics.ScaleTransform.
private void Transform(PaintEventArgs e)
{
e.Graphics.ScaleTransform(width, height);
e.Graphics.TranslateTransform(xOffset, yOffset);
}
You'll also need to make sure that whatever drawing method you're using has an overload for either PointF structures or Singles.
Of course, you could also just handle all of this on your end. Scaling your input/output coordinates by the width and height of the form and then translating by half of that will give you the same results.
Edit- To add some clarity, Windows always expects your coordinates to be in pixel form. You can use the above transformations to do your work at a different scale and then transform into the form that Windows expects.
Setting the translation should be straightforward. If you want the middle of the form, you simply want to translate by half the width and half the height. How you choose to scale your coordinates is going to depend on the range that you need to work in (ie, -1.0 to 1.0, -0.5 to 0.5, etc.).
I'm using bmp but I'm open to other suggestions since I found barely any info while searching for bmp interaction.
I finally plotted my drawing into a pictureBox
Everything is working properly, but when i click the drawing, I'd like to read where is the pointer on the bmp. If it is above the displaying graph, I would wanna take the X position out of it.
Edit:
Another not important but interesting question:
Is it possible to stick the mouse position whenever it is above the bmp, to move only above or below the graph function?
I've found my own answer, here :)
private void obterposicaonografico()
{
// Set the Current cursor, and display how many points in X axis you took
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y);
MessageBox.Show(Cursor.Position.X.ToString());
//Define it's position on X axis, subtracting the whole form X location
//This will ensure that if the user moves the form around, it will still be relative to the own form
//and not the windows positioning
decimal posicaoX = Cursor.Position.X -this.Location.X
MessageBox.Show(Cursor.Position.X.ToString());
}
I could not find anything on this particular problem, so here it goes!
I am interested in changing the position of the mouse cursor programmatically.
Cursor.Position = new Point(x, y);
works as expected using a single monitor. However, when I plug in a second one and choose to extend the desktop, changing the cursor's coordinates only works relative to the main screen.
So, does anyone know how to tell the cursor that I want to move to a different screen?
Get the width and height of your required screen and move the cursor relative to it
int width=Screen.AllScreens[1].Bounds.Width;
int height=Screen.AllScreens[1].Bounds.Height;
Cursor.Position=new Point(width-x,height-y);
I can get the coordinates of a windows entire area, and the coordinates of the client area using the GetClientRect and GetWindowRect Win32 calls. My problem is that the GetClientRect always returns 0,0 for the top left. How do I figure out what the actual client region is relative to the window rect?
You can use ClientToScreen to get the coordinates of the upper left (0,0) point in screen coordinates. The RECT returned by GetClientRect will be appropriate to get you the lower right corner (just add to the POINT set by ClientToScreen).
Use ClientToScreen to convert the client coordinates to screen coordinates. The window rect (GetWindowRect) is already in screen coordinates, and includes the non-client area (borders, caption, etc)
And if you are working with WinForms then you can use PointToScreen instead of ClientToScreen for solution proposed by Reed Copsey.
The relation between window rect (with borders etc) and the client rect (inside borders) is most easily found using AdjustWindowRectEx(). Get the window style and ex style of the window, and call that function, to see how much border is on each side.
You can also use the MapWindowPoints function to convert an entire RECT to screen coordinates at once.
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.