Saturday, September 15, 2012

Want to Display a Borderless Window in XNA?


It's not as obvious as you might think.

It used to be that games only had one way of being presented to the gamer: by consuming the entire screen.  Of course, dinosaurs were still roaming the earth back then.  Nowadays, modern games offer three different methods of presenting the game within the operating system.

A Lesson in Game Development


The first, and most popular mode, is called full-screen-mode.  When a game is run full-screen, it acquires exclusive access to the computer's graphics hardware and literally consumes the entire screen (hence the name). This tends to be the default method for most games being that it provides the most immersion and the best performance since the game does not have to compete with the operating system for drawing things to the screen.  The downside is that it takes a long time to switch to your operating system to do something else and switch back.  What's worse is that some games don't support this (or disable it altogether), and what's even worse than that is that some games won't handle it so well and will fail-crash (yes, I totally just made that word up).

The second display method is called windowed-mode.  Games that are run inside a normal window (like your web browser) are said to be in windowed-mode.  This method of displaying a game window provides no performance enhancements since the game is competing with the operating system to draw things, but it allows the gamer to easily do other things while playing the game, like check forums, look up cheat-codes, instant-message their friends, or read my blog (did I really write that?).

In the last few years, a third display mode has surfaced called borderless.  When a game window is displayed this way, it's run inside a window (just like in windowed-mode), only, it has no title bar or borders and is maximized to fill the entire screen.  Why the hell would anyone want to do that?  We live in a world where multitasking and multiple screens has become the norm.  So this mode provides the gamer with a more immersive experience without any of the downsides of the traditional full-screen method.

He shoots!  He ... misses?


Now, being that XNA Game Studio was designed entirely for sole purpose of making it extremely easy for game developers to get up and running quickly, you would think that the API would allow for the option to disable the borders on the game window.  After all, it supports full-screen and windowed-mode quite well, so it should have that option, right?  Nope.  Nuh-uh.  Didn't happen.  Not there.

EPIC FAIL.

I thought my old eyes were tired and missing something, so to the Internet I went!  ... and found squat.  How could the interwebs be devoid of information about creating borderless windows using XNA?  Blasphemy!

Do or Do Not.  There is No Try.


After hacking around for a few minutes, I found that the Game.Window class has a property that retrieved a (sort-of) managed window-handle for the game window.  Having done a ton of Win32-hacking when I was younger, I knew exactly what to do and this was the result:

IntPtr hWnd = this.Window.Handle;
var control = System.Windows.Forms.Control.FromHandle( hWnd );
var form = control.FindForm();
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
form.WindowState = System.Windows.Forms.FormWindowState.Maximized;

There it is.  Add a reference to System.Windows.Forms and call the above from your constructor and you'll have a borderless window with XNA Game Studio.