【问题标题】:Setting position of a Console Window opened in a WinForms App设置在 WinForms 应用程序中打开的控制台窗口的位置
【发布时间】:2010-12-05 15:38:02
【问题描述】:

我在 Rex Logan 在 SO 上发布的这个线程中找到了一些源代码:

link text

...Foredecker 在同一个线程中也发布了一些非常有趣的代码,但它不完整且复杂:我在“跟踪工具”上还不够了解如何完全实现它.. .

我可以使用这个控制台代码 Rex(好心)在 WinForms 应用程序中成功发布来记录各种事件,并推送对调试有用的消息;我也可以从应用程序代码中清除它。

当我打开控制台窗口(在主窗体加载事件中)时,我似乎无法可靠地设置控制台窗口的屏幕位置。如果我尝试像这样设置 WindowLeft 或 WindowTop 属性,则会出现编译阻塞 System.ArgumentOutOfRangeException 错误:

窗口位置必须这样设置 当前窗口大小适合 在控制台的缓冲区内,并且 数字不能为负数。 参数名称:left 实际值为 #

但是,我可以设置 WindowWidth 和 WindowHeight 属性。

我已尝试将激活控制台的代码移动到各个位置,包括:

  1. 在 MainForm 运行之前的 Program.cs 文件中
  2. 在 MainForm ctor 中调用 'InitializeComponent() 之前和之后
  3. 在表单加载事件中
  4. 在表单显示事件中

在代码中的所有这些地方都可以正常激活控制台,但在屏幕左上象限中看似随机的切换位置并没有改变。

控制台窗口的打开位置似乎随机变化(主窗体始终在屏幕上的同一位置初始化)。

【问题讨论】:

    标签: c# winforms console runtime window


    【解决方案1】:

    你可以试试这样的。

    此代码设置控制台窗口在控制台应用程序中的位置。

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

    此代码设置控制台窗口在 WinForm 应用程序中的位置。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-27
      • 1970-01-01
      • 1970-01-01
      • 2011-03-20
      • 2011-02-13
      • 2017-05-15
      • 2011-12-24
      • 2021-04-15
      相关资源
      最近更新 更多