|
如图,我想在点击最大化窗口的时候,窗口中的Datagv表仍然像图中那样停靠,保持相对位置和相对大小不变,怎么做?我按照通常的方法保存了最初位置控件和窗体大小的宽高比,发现对他不起作用了。代码如下:- public Form2()
- {
- InitializeComponent();
- Hashtable ht = new Hashtable();
- ht.Add("width", this.Size.Width);
- ht.Add("height", this.Size.Height);
- foreach (Control ctrl in this.Controls)
- {
- ht.Add(ctrl.Name + "X", ctrl.Location.X / this.Size.Width);
- ht.Add(ctrl.Name + "Y", ctrl.Location.Y / this.Size.Height);
- ctrl.Tag = ctrl.Size;
-
- }
- this.Tag = ht;
- }
- private void Form_Resize(object sender, EventArgs e)
- {
- Hashtable scale1 = (Hashtable)Tag;
- foreach (Control ctrl in this.Controls)
- {
- ctrl.Left = (int)(this.Size.Width * (double)scale1[ctrl.Name + "X"]);
- ctrl.Top = (int)(this.Size.Height * (double)scale1[ctrl.Name + "Y"]);
- ctrl.Width=(int)(this.Size.Width*1.0/(int)scale1["width"]*((Size)(ctrl.Tag)).Width);
- ctrl.Height=(int)(this.Size.Height*1.0/(int)scale1["height"]*((Size)(ctrl.Tag)).Height);
- }
- }
复制代码 |
|