我希望这是有道理的.
解决方法 这需要P / Invoke. Project Add Reference,选择System.Drawing.再次为System. Windows.Forms.在项目中添加一个新类并粘贴此代码:using System;using System.ComponentModel;using System.Drawing; // NOTE: Project + Add Reference requiredusing System.windows.Forms; // NOTE: Project + Add Reference requiredusing System.Runtime.InteropServices;public static class ConsoleUtils { public static voID CenterConsole() { IntPtr hWin = GetConsoleWindow(); RECT rc; GetwindowRect(hWin,out rc); Screen scr = Screen.FromPoint(new Point(rc.left,rc.top)); int x = scr.WorkingArea.left + (scr.WorkingArea.WIDth - (rc.right - rc.left)) / 2; int y = scr.WorkingArea.top + (scr.WorkingArea.Height - (rc.bottom - rc.top)) / 2; MoveWindow(hWin,x,y,rc.right - rc.left,rc.bottom - rc.top,false); } // P/Invoke declarations private struct RECT { public int left,top,right,bottom; } [Dllimport("kernel32.dll",SetLastError = true)] private static extern IntPtr GetConsoleWindow(); [Dllimport("user32.dll",SetLastError = true)] private static extern bool GetwindowRect(IntPtr hWnd,out RECT rc); [Dllimport("user32.dll",SetLastError = true)] private static extern bool MoveWindow(IntPtr hWnd,int x,int y,int w,int h,bool repaint);}总结
以上是内存溢出为你收集整理的c# – Console.SetWindowPosition – >居中(每次)全部内容,希望文章能够帮你解决c# – Console.SetWindowPosition – >居中(每次)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)