Thursday, June 4, 2009

Reposition a console window in C#

Motivation

When writing demo code I think a very nice interface is a console application. It is quick way of getting an application to input and output data without having to think on all those complex UI problems.

When you have to console projects, eg. client and server, the windows will most likely start cascaded. You then have to move one of them to be able to read stuff in both at the same time. This is really annoying.

How to

First you will need to get some native APIs:

   1: public struct RECT
   2: {
   3:    public int left;
   4:    public int top;
   5:    public int right;
   6:    public int bottom;
   7: }
   8:  
   9: public static class NativeMethods
  10: {
  11:    public const UInt32 WM_MOVE = 0x0003;
  12:  
  13:    [DllImport("kernel32.dll")]
  14:    public static extern IntPtr GetConsoleWindow();
  15:  
  16:    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
  17:    public static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);
  18:  
  19:    /// <summary>
  20:    /// The MoveWindow function changes the position and dimensions of the specified window. For a top-level window, the position and dimensions are relative to the upper-left corner of the screen. For a child window, they are relative to the upper-left corner of the parent window's client area.
  21:    /// </summary>
  22:    /// <param name="hWnd">Handle to the window.</param>
  23:    /// <param name="X">Specifies the new position of the left side of the window.</param>
  24:    /// <param name="Y">Specifies the new position of the top of the window.</param>
  25:    /// <param name="nWidth">Specifies the new width of the window.</param>
  26:    /// <param name="nHeight">Specifies the new height of the window.</param>
  27:    /// <param name="bRepaint">Specifies whether the window is to be repainted. If this parameter is TRUE, the window receives a message. If the parameter is FALSE, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a result of moving a child window.</param>
  28:    /// <returns>If the function succeeds, the return value is nonzero.
  29:    /// <para>If the function fails, the return value is zero. To get extended error information, call GetLastError.</para></returns>
  30:    [DllImport("user32.dll", SetLastError = true)]
  31:    public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
  32:  
  33:    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
  34:    public static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
  35: }

First you use the GetConsoleWindow API to get a handle to the window where the console buffers are being outputted.

The second step is to get the RECT structure populated with the window rectangle coordinates. We need this to compute the width and height of the window because we don’t want to change that and those are needed for the next API call.

The third and final step is calling MoveWindow API passing in the new origin and the same dimensions. Where goes a method that does just that:

   1: public static void RePositionWindow(int x, int y)
   2: {
   3:     IntPtr windowHandle = NativeMethods.GetConsoleWindow();
   4:     RECT rect = new RECT();
   5:     NativeMethods.GetWindowRect(windowHandle, ref rect);
   6:     NativeMethods.MoveWindow(windowHandle, x, y, rect.right - rect.left, rect.bottom - rect.top, true);
   7: }

I call this in the Main method before doing anything:

   1: class Program
   2: {
   3:     static void Main(string[] args)
   4:     {
   5:         ConsoleUtils.RePositionWindow(800, 100);
   6:  
   7:         // add code bellow
   8:     }
   9: }

I give each window a hardcoded position but you can try a more elaborate algorithm like dividing the available desktop space.

No comments: