views:

795

answers:

1

Hi,

I found some source code in this thread posted by Rex Logan here on SO :

link text

... there's also some very interesting code posted in this same thread by Foredecker, but it is incomplete and complex : I'm not up enough on the 'Trace facility to know how to fully implement it ...

I am able to use this Console code Rex (kindly) posted successfully in a WinForms application to log various events, and to push messages onto which are useful in debugging; I can clear it from the application code, also.

What I can't seem to do is to reliably set the screen position of the Console Window when I open the Console Window (in the Main Form load event). I get compile blocking System.ArgumentOutOfRangeException errors if I try to set WindowLeft or WindowTop properties like this :

The window position must be set such that the current window size fits within the console's buffer, and the numbers must not be negative. Parameter name: left Actual value was #

I am able, however, to set WindowWidth and WindowHeight properties.

I have tried moving the code that activates the Console various locations including :

  1. in the Program.cs file before the MainForm is 'run
  2. before and after the call to 'InitializeComponent() in the MainForm ctor
  3. in the Form Load event
  4. in the Form Shown event

The Console was activated okay in all these places in the code, but with no change in the seemingly random switching around of where in the upper-left quadrant of the screen it appeared.

Where the Console window opens seems to vary at random (the Main Form is always initialized in the same place on the screen).

Appreciate any pointers.

thanks, Bill

+5  A: 

you can try something like this.

This code set the position of the Console Window in a Console Application.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;


namespace ConsoleApplication10
{
  class Program
  {
    const int SWP_NOSIZE = 0x0001;


    [DllImport("kernel32.dll", ExactSpelling = true)]
    private static extern IntPtr GetConsoleWindow();

    private static IntPtr MyConsole = GetConsoleWindow();

    [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
    public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);

    static void Main(string[] args)
    {
      int xpos = 300;
      int ypos = 300;
      SetWindowPos(MyConsole, 0, xpos, ypos, 0, 0, SWP_NOSIZE);
      Console.WriteLine("any text");
      Console.Read();
    }
  }
}

This code set the position of the Console Window in a WinForm Application.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace WindowsFormsApplication10
{
  static class Program
  {

    const int SWP_NOSIZE = 0x0001;

    [System.Runtime.InteropServices.DllImport("kernel32.dll")]
    private static extern bool AllocConsole();

    [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
    public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);

    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern IntPtr GetConsoleWindow();

    [STAThread]
    static void Main()
    {
      AllocConsole();
      IntPtr MyConsole = GetConsoleWindow();
      int xpos = 1024;
      int ypos = 0;
      SetWindowPos(MyConsole, 0, xpos, ypos, 0, 0, SWP_NOSIZE);
      Console.WindowLeft=0;
      Console.WriteLine("text in my console");

      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new Form1());
    }
  }
}
RRUZ
Thank you, RRUZ; I was able to incorporate your code into the base code posted by Rex Logan and set the Console window where I wanted it.I am just curious : why the call to 'Console.Read() ? This is a WinForms app I am running, and I am using the Console only for "logging" : is this a standard thing to do when initializing a Console from within a WinForm app ?Many thanks ! best, Bill
BillW
@BillW - it's there so that the window stays visible until you hit `return`. The example does nothing after setting the window position so would simply close and you wouldn't see that it had the right position.
ChrisF
Hi ChrisF,Thanks for your response ! fyi : Using the code posted by Rex Logan that I linked to in my original request, the Console window does persist without any need for a call to Console.Read().best, Bill
BillW
Thanks, RRUZ, for that final "polishing" ! Too bad I can't vote answers (or comments) up yet ! In fact I think I'll write to SO mods to express my opinion that not allowing an OP to vote up (because they don't have the 'rep' earned yet) to an answer they find helpful is not ideal.best, Bill
BillW