How to set window position on current monitor?

I have a simple method that gets called by pressing f11. It reads out the resolution of the monitor the window is currently displayed on and sets the window to that resolution.

It also sets Is Borderless to true and sets the position of the window to (0, 0) using Window.position.

Now to my problem:
The method works fine on my primary monitor, but since Window.position uses global coordinates (resolution of all monitors combined), (0, 0) always refers to the top left corner of my primary monitor.

Therefore whenever I try to use said method on another display, the resolution gets set correctly but the window is then put on my primary monitor.

Is there a way to set the window position using coordinates that only include the resolution of the monitor the window is currently on?
Or does anyone have an idea how I could work around that problem?

I’ve been thinking and using my google skills for the past 2-3 days but always gave up after a few hours and worked on something else.

Any solutions/ideas/suggestions are appreciated.

Side note:
I use Windows 10, VS studio 2019 and my project is a Cross Platform Desktop Application project, meaning it uses openGL.

Is this what you’re looking for? Fullscreen mode - Learn MonoGame

1 Like

Hey,
First of all thanks for the reply.

I looked at the code and it looked like what you sent explains how the toggle for Fullscreen and Borderless Window works as well as how to properly set the resolution.

I should clarify that the question refers to Window mode but using the monitor’s full resolution. So Fullscreen is not part of the problem.

The reason for choosing borderless window over fullscreen being that window mode stays up even when you do something on a different monitor.

Setting the window to the monitor’s resolution is also not the problem.

It is only when I want to actually center the window on the monitor the window is on that I run into problems.

That being that I don’t know how I can check on which monitor the window is or rather how to center/set the window to that monitor’s position.

If that explication still is to confusing or more information is needed feel free to ask, any help is appreciated.

The code I linked to does borderless so you will be able to focus other applications and have the game stay up.

Okay,
so I thought maybe I was overlooking something so I tried the code, but I ended up with the same problem.
Toggeling Fullscreen or Borderless window Fullscreen worked fine on my primary monitor just like my own code, but not on my other monitors.
The game resolution would be set to the monitor’s resolution the game was on, but the window always ended up un my primary display.

My problem is that I want the game to go into borderless Fullscreen on whatever monitor the game is currently on.

One solution to that would be a way to set the window.position with coordinates that are local to the monitor the window is on, but I don’t know if something like that exists.

I would also be interested in the solution to this. Also you mentioned before that you got the resolution for whatever monitor the game was currently on? Can you share that code? Whenever I use the graphicsAdapter.CurrentDisplayMode (this variable is recalled from memory and may be different) it only gets me the resolution of the primary display.

Weird, I also just use GraphicsDevice.DisplayMode.Width/Height.
Are you making a project using OpenGL or DirectX?

I call this

 //Set Game Resolution
public void SGR(float Xf, float Yf)
    {
        Window.IsBorderless = !Window.IsBorderless;

        int X = Convert.ToInt32(Xf);
        int Y = Convert.ToInt32(Yf);

        if (Window.IsBorderless == true)
        {
            _graphics.PreferredBackBufferWidth = X;
            _graphics.PreferredBackBufferHeight = Y;

            _graphics.ApplyChanges();
            Window.Position = new Point(0, 0);
        }
        else
        {
            _graphics.PreferredBackBufferWidth = 800;
            _graphics.PreferredBackBufferHeight = 480;

            _graphics.ApplyChanges();
            Window.Position = new Point(GraphicsDevice.DisplayMode.Width / 2 - _graphics.PreferredBackBufferWidth / 2, GraphicsDevice.DisplayMode.Height / 2 - _graphics.PreferredBackBufferHeight / 2);

with this whenever F11 is pressed:

    //Controls
    public void CM()
    {
        if (KR(Keys.F11))
        {
            SGR(GraphicsDevice.DisplayMode.Width, GraphicsDevice.DisplayMode.Height);
        }
    }

Here is a sample project.
Just replace this with the Game1.cs in a default Cross Platform Desktop Application project to recreate the exact same setup I have. With this you also recreate the problem I described earlier.

Edit: I just realised you were talking about

GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width/Height

to answer your question then I use

GraphicsDevice.DisplayMode.Width/Height

Maybe there’s a bug in MonoGame? On my system I can borderless fullscreen on all my monitors with that code.

Okay so I’m not sure what ended up fixing it but after implementing the code in an entirely new project it works.
Since my project isn’t that large because I just started learning C#, Monogame and programming in general I’ll just copy my code into that new project.

I fell kinda dumb that it ended up being the first response after all, but at least it works now.
And also thank you for helping me and being so considerate Apostolique. You really helped me out.
This was actually my first ever post here or on any other community threads for that matter and it motivated me to get more involved, help others and make new post instead of just reading existing ones.

Edit: Just wanted to say I hope that I was able to answer your question as well TheKelsam.

1 Like

I had a similar issue on another project. I spent 2+ hours fixing something that wasn’t broken. Putting the code in a blank project worked instantly. I still don’t know what was wrong…