c# – Console.SetWindowPosition – >居中(每次)

c# – Console.SetWindowPosition – >居中(每次),第1张

概述我正在尝试将通过我的C-sharp代码执行的控制台窗口设置为每次生成代码时都居中. 我希望这是有道理的. 这需要P / Invoke. Project Add Reference,选择System.Drawing.再次为System. Windows.Forms.在项目中添加一个新类并粘贴此代码: using System;using System.ComponentModel;using S 我正在尝试将通过我的C-sharp代码执行的控制台窗口设置为每次生成代码时都居中.

我希望这是有道理的.

解决方法 这需要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 – >居中(每次)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1258868.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存