Console.SetWindowPosition->Center(everytime)居中。我希望这是有道理的。这需要P/Invoke。项目+添加参考,选择System.Drawing。再次针对System.Windows.Forms。在项目中新建一个类,粘贴如下代码:以上是C#学习教程:Console.SetWindowPosition->Center(everytime)分享的所有内容,如果对大家有用需要进一步了解C#学习教程,希望大家关注——usingSystem;使用System.ComponentModel;使用系统绘图;//注意:Project+AddReferencerequiredusingSystem.Windows.Forms;//注意:Project+AddReferencerequiredusingSystem.Runtime.InteropServices;publicstaticclassConsoleUtils{publicstaticvoidCenterConsole(){IntPtrhWin=GetConsoleWindow();RECT;GetWindowRect(hWin,outrc);屏幕scr=Screen.FromPoint(newPoint(rc.left,rc.top));x=scr.WorkingArea.Left+(scr.WorkingArea.Width-(rc.right-rc.left))/2;inty=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声明privatestructRECT{publicintleft,top,right,bottom;}[DllImport("kernel32.dll",SetLastError=true)]privatestaticexternIntPtrGetConsoleWind哇();[DllImport("user32.dll",SetLastError=true)]privatestaticexternboolGetWindowRect(IntPtrhWnd,outRECTrc);[DllImport("user32.dll",SetLastError=true)]privatestaticexternboolMoveWindow(IntPtrhWnd,intx,inty,intw,inth,boolrepaint);}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
