2011-06-16 131 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(); 
    }