窗體起始位置為頂部中間,WinForm居中顯示:
int x = (System.Windows.Forms.SystemInformation.WorkingArea.Width - this.Size.Width) / 2; int y = (System.Windows.Forms.SystemInformation.WorkingArea.Height - this.Size.Height) / 2; this.StartPosition = FormStartPosition.Manual; //窗體的位置由Location屬性決定 this.Location = (Point)new Size(x, y); //窗體的起始位置為(x,y)
其他注意點:
System.Windows.Forms.SystemInformation.WorkingArea.Width //屏幕寬度 System.Windows.Forms.SystemInformation.WorkingArea.Height //屏幕高度(去系統任務欄,當顯示有任務欄的時候) this.Size.Width //自己窗體的寬度, this.Size.Width //自己窗體的高度 this.ClientRectangle.Width //工作區域寬度 this.ClientRectangle.Height //工作區域高度設置窗口初始位置 this.StartPosition = FormStartPosition.Manual; //窗體的位置由Location屬性決定 this.StartPosition = FormStartPosition.CenterParent; //窗體在其父窗體中居中 this.StartPosition = FormStartPosition.CenterScreen; //窗體在當前顯示窗口中居中,尺寸在窗體大小中指定 this.StartPosition = FormStartPosition.WindowsDefaultBounds; //窗體定位在windows默認位置,邊界也由windows默認決定 this.StartPosition = FormStartPosition.WindowsDefaultLocation; //窗體定位在windows默認位置,尺寸在窗體大小中指定
通過指定窗體Locaiton來,設定窗體位置
this.StartPosition = FormStartPosition.Manual; //窗體的位置由Location屬性決定 this.Location = (Point)new Size(0, 0); //窗體的起始位置為0,0
創建窗體時, 設置寬度和高度
this.ClientSize = new System.Drawing.Size(x1,y1); //X1 為寬度,Y1為高度
獲取屏幕大小(using System.Drawing)
Rectangle rect = Screen.GetWorkingArea(this); Point p = new Point(rect.Width,rect.Height); this.Location = p;