Moving the Console Window
Just wanted to share a quick tidbit of code that's useful for making a game with a console window for debug information. A lot of times I want to launch my game and have the console window on a different monitor, or just out of the way in general. It gets super annoying to have to move it over every time I launch a game for debugging, but then I found that I can move it with programming!
I'm going to be honest and say I don't totally understand how this works. It uses DllImport to get some functions from the user32.dll in Windows and then uses those to move the window around... I think? It should be noted that this probably only works for Windows.
I use functions to find the console window by it's name which can be accessed through the Console class in .net, and move the window by its handle. I give it a negative X coordinate because that pushes it onto my left monitor, so now every time I launch the game my console window appears and immediately gets pushed over to the left monitor. Neat!
I'm going to be honest and say I don't totally understand how this works. It uses DllImport to get some functions from the user32.dll in Windows and then uses those to move the window around... I think? It should be noted that this probably only works for Windows.
class Program {
static void Main(string[] args) {
IntPtr handle = FindWindowByCaption(IntPtr.Zero, Console.Title);
MoveWindow(handle, -700, 50, 1000, 1100, true);
Core.Game.Start(); // Start the Otter game
}
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
public static extern IntPtr FindWindowByCaption(IntPtr zeroOnly, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hwnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
}
I use functions to find the console window by it's name which can be accessed through the Console class in .net, and move the window by its handle. I give it a negative X coordinate because that pushes it onto my left monitor, so now every time I launch the game my console window appears and immediately gets pushed over to the left monitor. Neat!
Post your comment!