Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

c# - Setting position of a Console Window opened in a WinForms App

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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());
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...