C#控制台窗口居中显示(转)

        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); 

        /// <summary>
        /// 控制台窗体居中
        /// </summary>
        public static void SetWindowPositionCenter()
        {
            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, true);
        }
时间: 2024-08-08 09:20:31

C#控制台窗口居中显示(转)的相关文章

MFC 窗口居中显示 VS2010

MFC 窗口居中显示  VS2010 (2011-09-11 19:52:05) 转载▼ 标签: 窗口居中显示 it 分类: MFC MFC窗口居中显示   VS2010   工程名为:MFC22_6 目的:点击居中按钮,窗口将在文档的中央显示思路:使窗口居中显示可以调用CWnd::CenterWindow函数.void CenterWindow(CWnd* pAlternateOwner=NULL):步骤: 1.   用创建一个单文档应用程序 2.   添加一个对话框资源//(类视图 右键—添

JavaScript特效实例005-控制弹出窗口居中显示

实例005                控制弹出窗口居中显示 实例说明 点击页面上的按钮,弹出窗口,并将窗口居中显示. 技术要点 本例首先应用JavaScript中的window对象的open()方法,打开指定大小的新窗口,然后通过screen对象,获取屏幕分辨率,再根据获取的值通过window对象的moveTo()方法,将新窗口移动到屏幕居中位置. moveTo()方法将窗口移动到指定坐标(x,y)处,其语法格式如下. window.moveTo(x,y) 其中,参数x,y表示窗口移动到的位

Qt 设置窗口居中显示和窗体大小

设置窗口居中显示 方法一:在窗口(QWidget类及派生类)的构造函数中添加如下代码: #include <QDesktopWidget> //....... QDesktopWidget* desktop = QApplication::desktop(); // =qApp->desktop();也可以 move((desktop->width() - this->width())/2, (desktop->height() - this->height())

弹出窗口居中显示

$(function() {    // 居中    function center(obj) {               var screenWidth = $(window).width(), screenHeight = $(window).height();  //当前浏览器窗口的 宽高        var scrolltop = $(document).scrollTop();//获取当前窗口距离页面顶部高度           var objLeft = (screenWidt

让div自适应浏览器窗口居中显示

今天做 banner 时发现一个问题,就是浏览器窗口水平拉伸时 banner 图未能居中,所以网上找了些资料,自己写了个小 demo html代码: <div class="div1"> <div class="div2">自适应浏览器水平垂直居中</div> </div> css代码: .div1{ width: auto; height: 600px; background: #cccccc; position:

C# 控制台窗口的显示与隐藏

1. 定义一个Consolse帮助类,如下: [csharp] view plaincopy /// <summary> /// 控制台帮助类 /// </summary> public static class ConsoleHelper { /// <summary> /// 获取窗口句柄 /// </summary> /// <param name="lpClassName"></param> /// <

newWindow 弹出的新窗口居中显示

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user

C#窗口居中显示

static class Program { /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //Application.Run(new Form1()); Form1 mainForm = new Fo

PYQT窗口居中

#UI.py,通过UI设计师制作后直接转换为UI.py脚本 # -*- coding: utf-8 -*-from PyQt4 import QtCore, QtGui try:    _fromUtf8 = QtCore.QString.fromUtf8except AttributeError:    _fromUtf8 = lambda s: s class Ui_Form(object):    def setupUi(self, Form):        Form.setObject