C# 无边界控制台应用程序
我想知道如何获得无边界C#.NET控制台应用程序。我的应用程序运行良好,但我不希望我的应用程序看起来像一个正常的表单,有最小化、最大化和关闭按钮,图标和文本在左上方C# 无边界控制台应用程序,c#,cmd,console-application,C#,Cmd,Console Application,我想知道如何获得无边界C#.NET控制台应用程序。我的应用程序运行良好,但我不希望我的应用程序看起来像一个正常的表单,有最小化、最大化和关闭按钮,图标和文本在左上方 所以,我想知道我如何才能做到这一点。你做不到,这甚至没有意义。由其输入和输出流表示的控制台是系统支持的资源,根本不需要由控制台窗口表示。例如,可以重定向应用程序的输入和输出。据我所知,您需要使用Win32更改控制台窗口的外观。这意味着DllImport和许多复杂性,考虑到您的备选方案,这些复杂性是完全不必要的: 如果将应用程序重新创
所以,我想知道我如何才能做到这一点。你做不到,这甚至没有意义。由其输入和输出流表示的控制台是系统支持的资源,根本不需要由控制台窗口表示。例如,可以重定向应用程序的输入和输出。据我所知,您需要使用Win32更改控制台窗口的外观。这意味着DllImport和许多复杂性,考虑到您的备选方案,这些复杂性是完全不必要的:
如果将应用程序重新创建为WinForms应用程序,则可以在主窗口上设置这些属性。然后在中间放置一个文本框,让它停靠在窗口上,然后模拟一个控制台。 < p>我将设计一个具有所需外观(无边界)的黑色窗体应用程序,甚至是带有控制台外观的黑色,然后使其可以移动 网址: 假设您(出于某种原因)不能使用无边界WinForm,并且您必须使用控制台窗口。嗯,你可以让它工作 使用中的大部分代码,我们可以组合出一个可行的解决方案 下面是一个无边框控制台窗口示例:
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();
}
}
}
这几乎是引用链接的直接副本。我试着找到表格的位置,但我做不到。我确实设法得到了尺寸,但位置为我返回了负值
虽然不是很好,但它是一个无边界的控制台窗口。我真的建议将文本框固定到普通表单中。这是一张图片:一张你拥有的图片和一个你想要的模型,会在描述中增加2000个单词。