1. 程式人生 > >C#:視窗大小,電腦螢幕寬高,程式的退出 及 窗體顯示位置、窗體顯示在最前 設定

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