码迷,mamicode.com
首页 > Windows程序 > 详细

WinForm 窗体初始位置篇

时间:2017-02-19 12:29:06      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:for   pre   point   forms   class   initial   构造   .text   控制   

1.在C#中,From本身有个StartPosition属性可以控制居中显示。

 StartPosition 默认值是WindowsDefaultLocation ,我们只需要改成CenterScreen. 注意MSDN上的一句备注:             

   显示窗体之前,应设置此属性。 可在调用 Show 或 ShowDialog 方法之前或在窗体构造函数中设置此属性。 所以不要试图在Load事件中改变此属性,没用的


winform每个窗体都可以自定义位置的,通过StartPosition的Manual属性,即通过location位移来确定窗体的起始位置。

2.改变窗体的位置

  在Form1窗体的右侧显示Form2  

StartPosition=Manual
 public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
            Test.from1 = this;
        }
        private void button1_Click(object sender, EventArgs e) {
            Form2 f = new Form2();
            f.Location = new Point(this.Left + this.Width,this.Top);
            f.Show(this);
            //MessageBox.Show(this,f.Width+":"+f.Height);
        }
    }

 

  窗体的位置主要由Left(咱都是成年人,不用解释Left什么意思吧)属性和Top属性决定,(Width,Height)决定窗体的大小,对 Width 和 Left 属性值所做的更改导致该控件的 Right 属性值更改。其中Left 属性值等效于控件的 Location 属性值的 Point.X 属性。示例代码:

            this.Left = Convert.ToInt32( this.textBox1.Text);
this.Top = Convert.ToInt32(this.textBox2.Text);

WinForm 窗体初始位置篇

标签:for   pre   point   forms   class   initial   构造   .text   控制   

原文地址:http://www.cnblogs.com/as3lib/p/6414873.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!