2011-06-16 53 views
1

反正有做到這一點之前,獲取表格的位置?在我的理解應該是可以的,但如果我展示的形式之前嘗試myForm.Location我得到(0, 0)Winform的:顯示(StartupPosition爲中心屏幕)

謝謝!

+0

你需要什麼信息呢? – Bernard 2011-06-16 20:43:30

+0

儘管這種形式示出了作爲一個模態對話框('ShowDialog'),顯示它之前,我需要顯示,這將根據這形成位置被定位其它形式。 – Diego 2011-06-16 20:46:17

回答

2

知道表單的位置,最好的辦法是決定你想要和把它放在那裏。

Form formDemo = new DemoForm(); 
    formDemo.Location = new Point(20, 30); 
    formDemo.Show(); //or formDemo.ShowDialog(); 

後@Diego澄清了他的問題,我相信這將做什麼是需要:

int ScreenWidth = Screen.PrimaryScreen.WorkingArea.Width; 
    int ScreenHeight = Screen.PrimaryScreen.WorkingArea.Height; 
    int formTop = (ScreenHeight/2) - (formDemo.Height/2); 
    int formLeft = (ScreenWidth/2) - (formDemo.Width/2); 
+0

對不起,我覺得有一個誤解。該窗體具有作爲StartupPosition的CenterScreen,以及它的需求。我需要的是要知道這個表單在屏幕中居中的位置。 – Diego 2011-06-16 20:52:17

+0

我想是的,我會接受答案,但明天我會進行測試。希望它有效! – Diego 2011-06-16 21:08:18

+0

這是不正確的窗體的高度和寬度屬性更改以適應視頻適配器的DPI設置。你*有*要等到Load事件觸發。只有那時你才知道表單的實際大小(和位置)。 – 2011-06-16 21:50:26

0

我覺得技術上繪製之前,你沒有一個位置。我個人不知道如何將「System.Windows.Forms.FormStartPosition」枚舉轉換爲Point。

不過你想要的東西,它應該是很簡單的。我將顯示第一種形式,然後將其傳遞給第二種形式,您可以將其設置爲啓動邏輯,以便將其用於定位。像這樣。

public Form1() 
    { 
     InitializeComponent(); 

     StartPosition = FormStartPosition.CenterScreen; 
     this.Shown += new EventHandler(Form1_Shown); 
    } 

    void Form1_Shown(object sender, EventArgs e) 
    { 
     ShowForm2(this.Bounds); 
    } 

    public void ShowForm2(Rectangle ParentBounds) 
    { 
     Form2 f = new Form2(); 

     int x = ParentBounds.Right+2; 
     int y = ParentBounds.Y + (ParentBounds.Height/2) - (f.Height/2); 

     Point childStartPosition = new Point(x,y); 

     f.StartPosition = FormStartPosition.Manual; 
     f.Location = childStartPosition; 
     f.Show(); 
    }