2012-10-03 162 views
-4

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

所以,我想知道我可以做到这一点。

+0

你有什么的画面和一个模拟了你想要什么,会增加2000个字的说明。 – Adrian

回答

6

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

5

据我所知,你将需要使用的Win32更改控制台窗口的外观。这意味着DllImport和许多复杂性,在您的替代方案中是非常不必要的:

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

0

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

网址: Make a borderless form movable?

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