如何让 .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 = -1;
        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);

 

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

 

 

posted @ 2014-03-04 23:46  cnkker.com  阅读(655)  评论(0编辑  收藏  举报