I'm trying to simulate a mouse move using the following code: mouse_event(MOUSEEVENTF_MOVE,150 ,150, 0, 0);
It works fine, but the X and Y coordinates start from my current cursor position. Is there a way I can make them start at the upper left of my screen?
I'm working in a Windows form app using c#
If you have no clue of what I'm asking, I tried to draw it for you;
The MSDN documents say that you can simply set the position of a form's cursor. Just do that, then send your mouseevent.
System.Windows.Forms.Cursor.Position = new Point(0, 0);
followed by your mouse_event, should do the trick.
Related
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);
How can I get the Position of the Cursor in C# relative to the window im in?
I am using SharpDX and i tried
System.Drawing.Point loc = Cursor.Position;
and
System.Drawing.Point loc = Control.MousePosition;
which gave me the absolute X and Y Coordinates of the Cursor (X:0 and Y:0 being in the top left corner of my screen and not my window).
Is there something implemented in SharpDX to get this done?
I found nothing that worked for me on the internet
Assume you are using a winform? If so, look for mouse events for controls.
If you are not using Visual studio then you will have to add the event. The controls sample can be found here -> https://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousemove(v=vs.110).aspx
Sharpdx does not itself deal with mouse at all from my recollection. You need to understand the field of movement for the mouse within the window and convert to what ever input stream you need.
I have a main form (application's parent form) which is set to the back using p/Invoke SetWindowPos. SetWindowPos asks for the X,Y coordinates of the window which are to be in Client Coordinates.
When I start the program the window is displayed center of screen. I then move the window and save the new location to Properties.Settings.Default.frmMainLocation which is bound to the frmMain.Location property.
I stop the program and then restart it - and the form is back to the original center position.
How would I set SetWindowPos to locate the window at X, Y Coordinates which are derived from the form's Location.X, Location.Y coordinates?
If you take your properties and save them to a point, you can set the window location like this:
Point pos = new Point
(
Properties.Settings.Default.posX,
Properties.Settings.Default.posY
);
this.Location = pos;
I set this in my form's Form_Load() method for the Form.Load() event with a few different sets of simple coords and it changed the position that the form was displayed on load each time. It's just a hop, skip, and a jump to have it load the coords from Properties.Settings.Default as I've shown in the example.
Actually it was a bit of a mess getting it to work.
When the App starts you need to SetWindowPos without the SWP_NOMOVE flag
Then SetWindowPos with the SWP_MOVE Flag set
Then when you want to change the position repeat the App start process.
When I set the position of my game window using form.DesktopLocation(new Point(X, Y);, it works, but then XNA automatically resets it to the center of the screen. I tried googling and looking on stack overflow, but I can't seem to figure out how to prevent it centering. Any ideas?
Here's my code manipulating the GraphicsDevice, GraphicsDeviceManager, and the Form controlling the game:
// in the initialiser
window = (Form)Form.FromHandle(Window.Handle);
GraphicsDeviceManager = new GraphicsDeviceManager(this);
// in the draw method
g.PreferredBackBufferWidth = Width;
g.PreferredBackBufferHeight = Height;
g.ApplyChanges();
f.DesktopLocation = new System.Drawing.Point(X, Y);
And that's literally everything. I tried setting the desktop location more than once each frame, before and after the g.ApplyChanges(), and that doesn't change anything.
However, just now, I tried removing the g.ApplyChanges() and it doesn't do it anymore. Odd. Why would running a g.ApplyChanges() before changing the form only sometimes recenter the form AFTER I positioned the form?
There is no solution to this question.
XNA automatically resets the position of the window to the center of the screen on g.ApplyChanges, and whenever it hits another window-related operation, such as resizing, or moving the window from one monitor to another.
This is a problem in XNA, not with XNA code. XNA was written with no way to prevent this because their focus is on content, not windows. Therefore, if you want greater control over your forms, it's a better idea to not use XNA at all, or use XNA's output in a form you set up.
I am trying to lock the cursor into the form, this is for a mouse locker application, I am trying to dispose the cursor so it will reset the Cursor.Clip when they unlock it.
So far I have:
Cursor.Clip = new Rectangle(x +8, y +30, Size.Width -16, Size.Height -38);
That works fine.
But I cannot figure out how to clear the clip when they unlock it.
I have tried Cursor.Dispose(); But that doesn't work.
Any ideas?
Thanks.
actually the idea is to just set it to a new rectangle
Cursor.Clip = new Rectangle();
It works regardless of the situation.
Try this: when your application starts, get the value of Cursor.Clip and save it as the unclipped value. Then when you want to reset the clip, assign the unclipped value.
UPDATE: In this page it says that to unclip the cursor in VB.NET, it is enough to do Cursor.Clip=Nothing. But this is strange since Rectangle is a structure and thus it can't be set to null. So in C#, maybe it would be Cursor.Clip=Rectangle.Empty or Cursor.Clip=default(Rectangle)?
Set Clip to a Rectangle that contains the screen's dimensions.
Cursor.Clip = Screen.PrimaryScreen.Bounds;
Of course, this won't work with dual monitor setups, but you get the idea.