Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

When looking into making my XNA game's window border-less, I found no properties or methods under Game.Window that would provide this, but I did find a window handle to the form.

I was able to accomplish what I wanted by doing this:

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

I don't know why but this feels like a dirty hack. Is there a built-in way to do this in XNA that I'm missing?

share|improve this question
2  
No there isn't. –  ClassicThunder Sep 15 '12 at 23:41

2 Answers 2

up vote 5 down vote accepted

Unfortunately there isn't a "simpler" way.

You're right in thinking that it is "hacky". It depends on internal implementation details of XNA. Which means it could break in future versions of XNA (there's nothing saying that XNA's Game class needs to use a Form). Also - in theory it might fail in unexpected ways - the XNA team hasn't necessarily tested this behaviour at all - let alone extensively.

(Note that XNA games are version-specific. For example: An XNA 3 game requires XNA 3 and won't run on XNA 4. So your binary is pretty safe against framework updates - but not necessaraly your code.)

There is a non-hacky way, by creating your own alternative to the Game class. The WinForms sample shows you how. But then you lose all the helpful stuff that Game and its friends provides (most notably the timing stuff).

But - because this is such a trivial settings change, it's probably completely safe to do in this case. And worth the risk, given the alternative is much trickier to implement. Maybe you could add some error-checking/exception handling - but even that's probably not necessary for this specific case.

I'm pretty sure XNA won't care if the FormBorderStyle changes out from under it.

(Of course, I've seen people pulling out the Form and doing some extremely brazen things with it. If you need to do anything beyond tweaking a few settings - I recommend going the "WinForms Sample" route.)

share|improve this answer
    
"there's nothing saying that XNA's Game class needs to use a Form" Well he is saying it (at least that his game needs a form), when he says that he needs border-less window... –  Kikaimaru Sep 17 '12 at 15:35
    
it might fail in unexpected ways - This has been my primary concern. The only reason I'm doing this is to provide a feature to display the game in a borderless-maximized (sort of faux-fullscreen) mode for the gamer. I suppose if this ends up causing any issues down the road, I could always just yank the feature and blame XNA. :P –  Cypher Sep 17 '12 at 15:59
    
I have a few editors that use the Windows Forms route (so I'm familiar with the technique), but to be honest, it would have been a lot more work than the feature it would implement is even worth. Thanks for the feedback. –  Cypher Sep 17 '12 at 16:02

Not that's it's any different, but you can also do this.

using System.Windows.Form;

Application.EnableVisualStyles();
Form gameForm = (Form)Form.FromHandle(Window.Handle);
gameForm.FormBorderStyle = FormBorderStyle.None;

I think it's worth posting this here as i've never run into any problems at all with this code.

share|improve this answer
    
Not that there's anything wrong with this method, but I built a Control from the IntPtr because I didn't want to assume that it would always be a pointer to a Form since I don't really know what the underlying XNA code is actually doing. And since just about everything GUI-related in .NET is inherited from Control, I figured I was pretty safe. –  Cypher Sep 17 '12 at 15:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.