如何让 .Net Console 控制台显示界面在最上层

时间:2023-03-09 09:03:30
如何让 .Net Console 控制台显示界面在最上层

可以利用 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);

既可以完成控制台窗口永远在最上层!!