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.
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.
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.
How can you centre a form in C#, so it appears in the middle of the screen
If you're talking about where it starts, use Form.StartPosition:
form.StartPosition = FormStartPosition.CenterScreen;
If you want to do it at an arbitrary time, you'll need to work out the size of the screen, the size of the form, and calculate it yourself before setting the Location property accordingly.
Use the StartPosition property of the form, change it to CenterScreen.
In Winforms, there is a property of a Form control called StartPosition; it's an enumeration, which includes the position "CenterScreen". Use this, and your window will appear, when first created, in the middle of the monitor on which the mouse is currently located.
You can set the StartPosition property in the designer
Click your form and press F4 to show the Properties tab, then scroll down to the StartPosition property and change the value to CenterScreen.
If you want it to be centered at startup, set your form's StartPosition ( http://msdn.microsoft.com/en-us/library/system.windows.forms.form.startposition.aspx ) to CenterScreen.
If you want it to be centered some other time, you have to use the SystemInformation.WorkingArea class ( http://msdn.microsoft.com/en-us/library/system.windows.forms.systeminformation.workingarea.aspx ) and perform arithmetic on that like so:
X = (Screen Width - Form Width) / 2
Y = (Screen Height - Form Height) / 2
And then consider what should happen if your form was too big. Setting either or both coordinates to a negative number is poor form, so don't forget that case.