【问题标题】:Borderless Console Application无边界控制台应用程序
【发布时间】:2012-10-03 08:57:54
【问题描述】:

我想知道如何获得无边界 C#.NET 控制台应用程序。我的应用程序工作正常,但我不希望我的应用程序看起来像一个正常的表单,带有最小化、最大化和关闭按钮以及左上角的图标和文本。

所以,我想知道如何实现这一目标。

【问题讨论】:

  • 一张你拥有的照片和你想要的模型,会在描述中增加 2000 个字。

标签: c# cmd console-application


【解决方案1】:

你不能,它甚至没有意义。由其输入和输出流表示的控制台是系统支持的资源,根本不需要由 控制台窗口 表示。例如,您的应用程序的输入和输出可以重定向。

【讨论】:

    【解决方案2】:

    据我所知,您需要使用 Win32 来更改控制台窗口的外观。这意味着 DllImport 和很多复杂性,考虑到您的替代方案,这是完全不必要的:

    如果您将应用程序重新创建为 WinForms 应用程序,则可以在主窗口中设置这些属性。然后在中间放一个文本框,让它停靠在窗口上,你正在模拟一个控制台。

    【讨论】:

      【解决方案3】:

      假设您(出于某种原因)不能使用无边框 WinForm,而您绝对必须使用控制台窗口。好吧,你可以有点让它工作。

      使用this similar problem over here 中的大部分代码,我们可以组合出一个可行的解决方案。

      这是一个无边框控制台窗口示例:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Runtime.InteropServices;
      
      namespace ConsoleBorderTest
      {
          class Program
          {
              [DllImport("USER32.DLL")]
              public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
      
              [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
              static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
      
              [DllImport("user32.dll")]
              static extern bool DrawMenuBar(IntPtr hWnd);
      
              [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("user32.dll", SetLastError = true)]
              static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
      
              [DllImport("user32", ExactSpelling = true, SetLastError = true)]
              internal static extern int MapWindowPoints(IntPtr hWndFrom, IntPtr hWndTo, [In, Out] ref RECT rect, [MarshalAs(UnmanagedType.U4)] int cPoints);
      
              [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
              public static extern IntPtr GetDesktopWindow();
      
              [StructLayout(LayoutKind.Sequential)]
              public struct RECT
              {
                  public int left, top, bottom, right;
              }
      
              private static readonly string WINDOW_NAME = "TestTitle";  //name of the window
              private const int GWL_STYLE = -16;              //hex constant for style changing
              private const int WS_BORDER = 0x00800000;       //window with border
              private const int WS_CAPTION = 0x00C00000;      //window with a title bar
              private const int WS_SYSMENU = 0x00080000;      //window with no borders etc.
              private const int WS_MINIMIZEBOX = 0x00020000;  //window with minimizebox
      
              static void makeBorderless()
              {
                  // Get the handle of self
                  IntPtr window = FindWindowByCaption(IntPtr.Zero, WINDOW_NAME);
                  RECT rect;
                  // Get the rectangle of self (Size)
                  GetWindowRect(window, out rect);
                  // Get the handle of the desktop
                  IntPtr HWND_DESKTOP = GetDesktopWindow();
                  // Attempt to get the location of self compared to desktop
                  MapWindowPoints(HWND_DESKTOP, window, ref rect, 2);
                  // update self
                  SetWindowLong(window, GWL_STYLE, WS_SYSMENU);
                  // rect.left rect.top should work but they're returning negative values for me. I probably messed up
                  SetWindowPos(window, -2, 100, 75, rect.bottom, rect.right, 0x0040);
                  DrawMenuBar(window);
              }
      
              static void Main(string[] args)
              {
                  Console.Title = WINDOW_NAME;
                  makeBorderless();
                  Console.WriteLine("Can you see this?");
                  Console.ReadLine();
              }
          }
      }
      

      这几乎是引用链接herehere 的直接副本。我尝试获取表格的位置,但我做不到。我确实设法获得了大小,但是该位置为我返回了负值。

      虽然不是很好,但它是一个无边框的控制台窗口。我真的建议只将文本框停靠在正常形式中。这是一张图片:"You need 10 reputation to post images"...

      【讨论】:

        【解决方案4】:

        我会设计一个具有所需外观(无边框)甚至黑色控制台外观的 Windows 窗体应用程序,然后使其可移动

        网址: Make a borderless form movable?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-11-04
          • 1970-01-01
          • 1970-01-01
          • 2011-01-22
          • 1970-01-01
          • 2012-02-11
          • 1970-01-01
          • 2019-01-24
          相关资源
          最近更新 更多