可以利用 Win32 API 来控制 Console 窗口的 最大化 或 最小化。
废话不多说见以下代码:
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetWindowPos(
IntPtr hWnd,
IntPtr hWndInsertAfter,
int x,
int y,
int cx,
int cy,
int uFlags); private const int HWND_TOPMOST = -;
private const int SWP_NOMOVE = 0x0002;
private const int SWP_NOSIZE = 0x0001;
在Main函数调用以上方法
IntPtr hWnd = Process.GetCurrentProcess().MainWindowHandle;
SetWindowPos(hWnd, new IntPtr(HWND_TOPMOST), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
既可以完成控制台窗口永远在最上层!!