WindowsForm窗體位置

轉載地址:http://www.cnblogs.com/mfk0506/p/6196020.html

 WindowsForm的窗體,是winForm程序的基本單元。窗體的大小和位置是如何控制的呢?

 

先看窗體的幾個屬性。如下圖所示

 

一、 設置窗體起始位置居中

窗口默認是在左上角的,可以用微軟定義好的FormStartPosition屬性來配置爲居中:

this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

等同於 

這個屬性的其他值也可以瞭解下:

屬性值

說明

CenterParent

在其父窗體中居中

Manual

位置由Location屬性決定

WindowsDefaultBounds

默認位置,邊界也默認

WindowsDefaultLocation

默認位置,尺寸在窗體大小中指定

 

其中提到了Location屬性,下面詳談。

 

二、 指定窗體位置和大小。

窗體有Left,Right,Top,Bottom屬性,似乎很完美,應該定義這四項,就能明確指定窗體的大小和位置了吧?然而並非如此。RightBottom是隻讀的屬性,可以用它們來獲取窗體邊距,卻不能用來指定邊距。

確定窗體位置,只需要窗體左上角這一個點的位置。Left,Top 就是這個點與屏幕左、上邊距的距離,換句話說,窗體的Location屬性,實際上就是(Left,Top)。 下面的語句是等價的:

this.Location=new Point(100,100);

this.Left = 100; this.Top = 100;

而確定窗體的大小,用Width 寬度和Height高度,就夠了。指定當前窗體的大小很簡單:

 this.Width = 600;

 this.Height =480;

窗體是有最小值和最大值的,可以通過設置MaximumSizeMinimumSize來指定。如果沒有指定,最大不能超過屏幕分辨率,最小不是(0,0),而是系統自己計算出的能讓這幾個圖標顯示出來的大小:   

有時候涉及到對於窗體大小的動態調整,就應該注意判斷,不要越界。那麼如果想設置窗體能根據不同屏幕的大小自動調整呢?

 

三、 根據分辨率調整窗體大小

首先要獲取屏幕大小:

            int width = Screen.PrimaryScreen.WorkingArea.Width;

            int height = Screen.PrimaryScreen.WorkingArea.Width;

Screen.PrimaryScrenn爲獲取顯示器,如果有多顯示器也可以用Screen.AllScreens[0],Screen.AllScreens[1]...WorkingArea爲桌面工作區域,不包括任務欄等。

然後設置寬度和高度:

            this.Width = (int)(width / 2);

            this.Height = (int)(height / 2);

 

四、窗體最大化、最小化和全屏

通過設置WindowsState屬性爲MaximizedManimized來實現窗體的最大化和最小化。如果需要全屏顯示,可將窗體大小設置爲屏幕大小(見三)。但此時可能需要隱藏上方的圖標、標題、最大最小化和關閉按鈕。方法爲設置FormBorderStyleNone

 

五、窗體置於頂層

設置TopMost屬性爲True即可。

 

由上述內容可見,基本上都是通過設置窗體的屬性來實現,在學習過程中,對各種屬性應該有一個基本的瞭解。.net將很多工作都隱藏在幕後,所以在屬性窗口的設置,其實也可以通過代碼來實現。如果有不清楚的地方,可以打開對應的Designer.cs文件來查看,更是大大方便了學習。可以看這一段:

           //

          // Form1

            //

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.Controls.Add(this.btnOpenNewForm);

            this.Controls.Add(this.btnChangePos);

            this.Controls.Add(this.txttop);

            this.Controls.Add(this.txtWidth);

            this.Controls.Add(this.txtHeight);

            this.Controls.Add(this.txtLeft);

            this.Controls.Add(this.label4);

            this.Controls.Add(this.label3);

            this.Controls.Add(this.label2);

            this.Controls.Add(this.label1);

            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

            this.Name = "Form1";

            //起始居中

            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

            //窗體位置和大小

            this.Location = new System.Drawing.Point(100, 100);

            this.ClientSize = new System.Drawing.Size(454, 400);

 

            this.Text = "Form1";

            //窗體置頂

            this.TopMost = true;

            //窗體最大化

            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

            this.Load += new System.EventHandler(this.Form1_Load);

            this.ResumeLayout(false);

            this.PerformLayout();


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章