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);
Related
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 winform (c#, let's say 250px by 250px) that needs to stay in one location on the screen regardless of screen resolution i.e 800x600, 1920x1080 etc. The Winform itself contains only one element - a picturebox so what's inside really doesn't matter (no need to worry about fonts, etc.). I just need it to stick in one place on the screen from one monitor to another.
Any ideas? Thanks in advance.
Could you use one of these?
1) WindowsState = Maximized (then you dont worry, it always takes whole screen)
2) StartPosition = CenterScreen (then it always shows as centered), or CenterParent to center within parent form
3) Location = in this case you would have to do some math to get screen size, your form size than based on that center it but I dont see point of using this considering that StartPosition does it already for you.
Hope that helps
Ok, so in order to get the window at a fixed location given in % of the screen size, you need the screen size (e.g. using this answer), compute the desired position, and set it as the window-location.
Since you need to do it at startup, you could do it before you show the window, or maybe best inside a Frame.Loaded event handler.
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'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.
Im trying to move my cursor according to my hand point in kinect, I can get the real coordinates I mean I can move an image on screen but I want real cursor to be settled according to my hand coordinates. I tried Console.SetCursor(x,y) but it gives exception I also tried to download windows forms dll but I cant find the version 4.00 . Is there any simple way to set cursor in a desired position? (which is working by the way and as I said Console.SetcursorPosition is not wodking?)
You didn't provide very much information about you app but I suspect that you just need to assign to Cursor.Position from System.Windows.Forms. You may need to add a reference to System.Windows.Forms in order to gain access to this, depending on exactly what type of project you have.
If you want to keep it lightweight and avoid taking a reference to WinForms then you could just pinvoke to SetCursorPos.
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
Just use
Cursor.Position = new Point();
You can find more information's here
Thank you for question and answer.
I found strange and unobvious behavior.
Let you use multi-monitor (two or three monitor) configuration, and use two windows in your application.
One window is active, and you set cursor position for second windows.
And this two windows located on different monitors.
Then, you need call SetCursorPos TWICE!
TWICE, who would have thought?
Firs call move cursor to virtual borderline between monitors.
And only second call move cursor to required position second window!
Example of code (after 6 hours of experiments):
SetForegroundWindow(this.Handle); // set second window active
SendMessage(this.Handle, 0x20, this.Handle, (IntPtr)1); // Send WM_SETCURSOR
SetCursorPos(400, 600);
Thread.Sleep(50);
SetCursorPos(400, 600);