C#:窗口大小,电脑屏幕宽高,程序的退出 及 窗体显示位置、窗体显示在最前 设定

1。直接上代码

using Microsoft.VisualBasic;
using System;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApp13 {
    public partial class Form1 : Form {
        

        public Form1() {
            InitializeComponent();
        }
        

        private void Form1_Load(object sender, EventArgs e) {
            //一般窗口的设置如下
            //this.FormBorderStyle = FormBorderStyle.Fixed3D; //窗口大小不可调
            //this.FormBorderStyle = FormBorderStyle.Sizable; //窗口大小可调
            //this.WindowState = FormWindowState.Normal;

            //窗口全屏设置如下:
            //this.FormBorderStyle = FormBorderStyle.None; //无边框
            //this.WindowState = FormWindowState.Maximized; //最大化

            //电脑屏幕的宽和高
            int cwidth = System.Windows.Forms.SystemInformation.WorkingArea.Width;
            int cheight = System.Windows.Forms.SystemInformation.WorkingArea.Height;
            //窗体的宽和高
            int width = this.Size.Width;
            int height = this.Size.Height;
            //this.result.Text = cwidth + "   " + cheight + "   "+ width+ "   " + height+"\n"+this.Location.X+"\n"+this.Location.Y;
            //让窗体显示在屏幕的正中央
            //①方法
            //this.StartPosition = FormStartPosition.CenterScreen;
            //②方法
            this.StartPosition = FormStartPosition.Manual;
            this.Location = new System.Drawing.Point((cwidth-width)/2,(cheight-height)/2);


            

        }

        private void button1_Click(object sender, EventArgs e) {
            //程序退出的3种方式:
            //①this.Close();         //只是关闭当前窗口,并不能退出程序
            //②Application.Exit();   //退出所有窗口,中止所有消息,但线程无法全部安全退出
            //③
            System.Environment.Exit(0); //彻底退出
        }
        
    }
}

窗体显示在最前

private void Form1_Load(object sender, EventArgs e) {
            this.TopMost = true;
        }

 

 

你可能感兴趣的:(C#)