Lemmi
Lemmi

Reputation: 23

Changing where the .exe window appears when debugging?

First of all I'm not sure if ".exe window" is the proper term. It's the window that pops up when you start the application.

I'm a game programmer, and when I'm debugging, I very rapidly start it up, look at the problem, then close it down again to make minor changes in the code, then start it again etc. I do this like once per minute, so it happens a lot. My problem is that the .exe window always appears at the middle of my main screen (where I'm coding), and I'm running double monitors, and I'd like the game window to appear on my second screen instead of my main screen (obscuring my code).

Can I change where the exe window appears in VS2010? I've looked around everywhere, it feels like. Or is it something that will have to be managed by a 3rd party program? If so, what program?

Edit: OK, OK, I found the solution. I did a really dumb mistake where I didn't mention that I am using XNA, and not using winforms. Sorry for misleading you guys. Here's how I solved it:

First off I had to include:

    using System.Runtime.InteropServices;

Then at the top of my main class I created a tiny class:

    public static class User32 
    { 
    [DllImport("user32.dll")] public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); 
    }

Then in my Initialize function I simply call:

    #if DEBUG
              User32.MoveWindow(Game.Window.Handle, 2000, 400, 600, 480, true);
    #endif

It's a little ugly, but it's only for debugging and only called once, so psh.

Original solution found here: http://social.msdn.microsoft.com/forums/en-US/xnagamestudioexpress/thread/bc9588a9-542f-425b-9025-d69fe2b0b182/

Upvotes: 2

Views: 215

Answers (4)

Steve H
Steve H

Reputation: 5519

Although you are not using winforms, you still change it in Xna by using winforms objects. I know you found a solution but here is how to change it without using interop.

Add a reference to System.Windows.Forms and System.Drawing to the References in the game project.

Resist the temptation to add using statements for these as it can cause ambiguity with some Xna objects (Point, for instance, which in Xna uses floats).

In the Game.Initialize method:

System.Drawing.Point p = new System.Drawing.Point(2000, 400);// or wherever you want
System.Windows.Forms.Control c =  Control.FromHandle(this.Window.Handle);
c.Location = p;

the game window will now start at the screen 2000,400 location.

Upvotes: 1

Nick Spreitzer
Nick Spreitzer

Reputation: 10588

I would just call it the "main application window". Anyway, assuming you're using WinForms, this would put the window in the top left corner of the first screen that isn't your primary screen:

void Form1_Load(object sender, EventArgs e)
{
#if DEBUG
    Location = Screen.AllScreens.First(s => !s.Primary).Bounds.Location;
#endif
}

If you've only got two monitors hooked up, it'll work fine. You could also get more creative and center the application window on the other monitor, maximize it, whatever. The #if could be substituted with if (System.Diagnostics.Debugger.IsAttached) as suggested by @Daniel if you wanted. I used the former just to present another alternative.

Upvotes: 1

Daniel Rose
Daniel Rose

Reputation: 17638

Option 1: You could set the appropriate properties on the window/form if a debugger is attached.

if (System.Diagnostics.Debugger.IsAttached)
{
    // Set the window/form's top/left properties.
}

Option 2: Add a command line switch, use that as startup parameter (Properties->Debug->Commandline arguments), and then set the appropriate properties in the window/form:

private void Application_Startup(object sender, StartupEventArgs e)
{
    if (e.Args.Any(arg => arg.Equals("/debugmode", StringComparison.OrdinalIgnoreCase))
        // Set some value which you check in your main window.
}

Upvotes: 2

Blindy
Blindy

Reputation: 67362

You can set the Form.StartPosition property, or just manually write to the Left and Top properties of the form you want to move.

Upvotes: 2

Related Questions