How to force game to always be in windowed mode?

Hi there,

I’m writing a small application for a friend and I’ve noticed that when the preferred back buffer is set to a size the same as or greater than my screen size, it automatically makes the game run full screen. Is it possible to set the game size to be a large value like 1920x1080 but still keep the game in windowed mode, even if the window size exceeds my screen size?

For reference, here is the code I’m currently using in my constructor:

    graphics.PreferredBackBufferHeight = 1080;
    graphics.PreferredBackBufferWidth = 1920;
    graphics.IsFullScreen = false; //This doesn't seem to matter with large sizes
    graphics.ApplyChanges();

Thank you!

The game should be in windowed mode in that instance. Note that calling GraphicsDeviceManager.ToggleFullScreen() internally sets IsFullScreen to !IsFullScreen, so if you have full screen functionality you can wrap it like so:

public void ToggleFullscreen(bool fullScreen)
{
    graphicsDeviceManager.IsFullScreen = !fullScreen;
    graphicsDeviceManager.ToggleFullScreen();
}

You can see that it’s not full screen if you set the game window’s position (Window.Position) after changing the backbuffer size. Since it’s taking up the entire screen, you can’t see the window border if the window is centered.

1 Like

Are you sure it’s not windowed but the frame part is just not visible because it’s outside screen? Anyway setting resolution to be bigger than your computer resolution in windowed mode makes no sense, it means that you’ll always have invisible pixels out of screen. You shouldn’t allow that.

1 Like

Ah, I suppose it was just that the window borders were off the screen! Moving the window position through code verified that. Thank you Kimimaru.

GeonBit, I do agree with you on this: [quote=“GeonBit, post:3, topic:10539”]
Anyway setting resolution to be bigger than your computer resolution in windowed mode makes no sense, it means that you’ll always have invisible pixels out of screen. You shouldn’t allow that.
[/quote] but as I said, I’m just writing a small application for a friend. His monitor has a slightly higher resolution than mine, and I just wanted to be sure monogame wasn’t trying to force something I didn’t want by forcing the game fullscreen.

Anyways, thank you both for your time and help!

@LunaArgenteus Is this a DesktopGL project? Recently a PR was merged that made sure the window border is visible after resizing (link). I’m not sure what the behavior is for DX projects.

Side note: You should not call ApplyChanges in your Game1 constructor. The GraphicsDevice is not created yet in your constructor, so initialization happens after you set the GraphicsDeviceManager properties. If you want to change any of those properties after your constructor you should call ApplyChanges though.

[quote=“Kimimaru, post:2, topic:10539”][/quote]
public void ToggleFullscreen(bool fullScreen)
{
graphicsDeviceManager.IsFullScreen = !fullScreen;
graphicsDeviceManager.ToggleFullScreen();
}

You can just set it directly like so.

public void SetFullscreen(bool fullScreen)
{
    graphicsDeviceManager.IsFullScreen = fullScreen;
    graphics.ApplyChanges();
}

If your going to toggle it that is ussually linked to a key like f11 or a button ect.Then you should check which one its in or just call toggle,
but…
Have some sort of pause timer even if its just a simple 20 updates timer, that way it doesn’t register twice or more in a row.

For reference, the implementation of ToggleFullScreen is literally just the following:

public void ToggleFullScreen()
{  
    IsFullScreen = !IsFullScreen;
    ApplyChanges();
}

@Jjagg[quote=“Jjagg, post:5, topic:10539”]
You should not call ApplyChanges in your Game1 constructor. The GraphicsDevice is not created yet in your constructor, so initialization happens after you set the GraphicsDeviceManager properties.
[/quote]

Ah, I’d noticed it seemed to work either way with or without ApplyChanges in the constructor - I guess that’s why. Thank you for that insight. The project is indeed a DesktopGL project, but I’ve had monogame on my machine for a few months and didn’t update it before starting this project so it’s possible I don’t have that update.

1 Like