tags:

views:

456

answers:

3

I want to make the XNA game window be in "windowed" mode but "always on top", is there a way to do this?

+3  A: 

http://www.pinvoke.net/default.aspx/user32/SetWindowPos.html

Include the values from the sample Page in a WinApi class and call this function from your game class:

WinApi.SetWindowPos(this.Window.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);

That should do it.

Marcus
+2  A: 

Thanks for your response, the code from that webpage wouldn't compile for me, however it did point me in the right direction, this is the code I am using (using XNA 3.1)

First, within the same namespace as the game copy and paste in this code

class User32
    {
        [DllImport("user32.dll")]
        public static extern void SetWindowPos(uint Hwnd, int Level, int X, int Y, int W, int H, uint Flags);
    }

I just wrote it above my main "Game" class, since I only use it within my Game class.

Then within the LoadContent() of the game class (MUST be within the LoadContent() method, doesn't work properly anywhere else), write this somewhere...

User32.SetWindowPos((uint)this.Window.Handle, -1, 0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight, 0);

note: "graphics" is the instance of GraphicsDeviceManager that is premade for you whenever you start your project.

This can also be used to position the game window wherever you want on the screen. For me I wanted it in the top left corner of the screen.

Danny
I like more Marcus solution without size stuff :)
mnn
We actually have very close to the same solution.. if you actually looked at the code from that page, you would see his solution ALSO has the "size stuff"The difference is that my User32 class is much simpler, uses primitive variables instead of objects, and doesn't have the unneeded convenience methods.However the reason I am using my solution was because I had compile time errors when making the several const and static variables in the User32 class.
Danny
A: 

The easiest way is to set the graphics device to display full screen. Place the following code in your main game method:

this.graphics.IsFullScreen = true;
Gavin O'Brien
Yes I know I can do that, but my requirement is to keep the window in windowed mode and make it the topmost. I've already gone with the answer I've provided for my own question, StackOverflow just isn't let me accept my own answer yet.
Danny