设计的是一个非最大化的窗口,要求最大化后,控件大小随之改变,不用 通过设置控件的Anchor属性的方法(panel设置Anchor属性就不能正常显示),需要各位提供的代码提示,谢谢!

解决方案 »

  1.   

    panel设置Anchor属性就不能正常显示
    -----------不懂,panel还有这个问题吗?
      

  2.   

    Anchor是最好的解决办法,如果你自己写也和Anchor效果一样的。
      

  3.   

    僅供參考:http://blog.csdn.net/loveme1204/archive/2007/12/24/1964577.aspx
      

  4.   


    但是我设置panel的Anchor,显示的位置也不对啊 ,还有如果picturebox也这么设置,就更不对了啊
      

  5.   

    在窗体的resize事件中写代码啊,调整控件的大小就可以了
      

  6.   

    可以通过使用tablelayerpanel和控件的dock属性来实现,不过比Anchor要费很多事啊。
      

  7.   

    4樓結合這個網站:http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx
    翻譯不一定很准,你稍微微調下。經測試可用。
      

  8.   


            private void  setTag(Control cons)
            {
                foreach (Control con in cons.Controls)
                {
                    con.Tag = con.Width +":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
                    if (con.Controls.Count > 0)
                        setTag(con);                
                }
            }
            private void setControls(float   newx, float  newy, Control  cons)
            {
                foreach (Control  con in cons .Controls )
                {                string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
                    float a = Convert.ToSingle(mytag[0]) * newx;
                    con.Width = (int)a;
                    a=Convert.ToSingle(mytag[1]) * newy;
                    con.Height = (int)(a);
                    a=Convert.ToSingle(mytag[2]) * newx;
                    con.Left = (int)(a);
                    a=Convert.ToSingle(mytag[3]) * newy;
                    con.Top = (int)(a);
                    Single currentSize = Convert.ToSingle (mytag[4]) * newy;
                    con .Font =new Font (con.Font .Name ,currentSize,con.Font .Style ,con.Font .Unit );
                    if(con.Controls .Count >0)
                    {
                        setControls (newx ,newy ,con );
                    }
                }        }        void Form1_Resize(object sender, EventArgs e)
            {
               // throw new Exception("The method or operation is not implemented.");
                float  newx = (this.Width )/ X;
              //  float newy = (this.Height - this.statusStrip1.Height) / (Y - y);
                float newy = this.Height / Y;
                setControls(newx, newy, this);
                this.Text = this.Width.ToString() +" "+ this.Height.ToString();        }
    翻译的一篇BLOG,但是注意statusStrip1之类的,自己修改把.
    form_load中添加
                 this.Resize += new EventHandler(Form1_Resize);            X = this.Width;
                Y = this.Height;
                y = this.statusStrip1.Height;
                setTag (this);
      

  9.   

            public float   X;
            public float   Y;
            public float y;
      

  10.   

    为什么手动拉放窗口却不能用呢?
    Anchor不会缩放字体,而14楼的会缩放字体,不错...
      

  11.   

    建议把float  newx = (this.Width )/ X;
    写成 float newx = (float)(this.Width) / (float)X;
    否则在手工拉放时比例始终是1
    另外,如果手工拉申方法来缩小窗口,设置字体时会有异常.
      

  12.   

    如何实现控件location的变化呢?
      

  13.   

    public float X, Y,y;        private void setTag(Control cons)
            {
                foreach (Control con in cons.Controls)
                {
                    con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
                    if (con.Controls.Count > 0)
                        setTag(con);
                }
            }
            private void setControls(float newx, float newy, Control cons)
            {
                foreach (Control con in cons.Controls)
                {                string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
                    float a = Convert.ToSingle(mytag[0]) * newx;
                    con.Width = (int)a;
                    a = Convert.ToSingle(mytag[1]) * newy;
                    con.Height = (int)(a);
                    a = Convert.ToSingle(mytag[2]) * newx;
                    con.Left = (int)(a);
                    a = Convert.ToSingle(mytag[3]) * newy;
                    con.Top = (int)(a);
                    Single currentSize = Convert.ToSingle(mytag[4]) * newy;
                    con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        setControls(newx, newy, con);
                    }
                }        }private void FrmMain_Load(object sender, EventArgs e)
    {
                this.Resize += new EventHandler(FrmMain_Resize);
                X = this.Width;
                Y = this.Height;
                y = this.statusStrip1.Height;
                setTag(this);
    } private void FrmMain_Resize(object sender, EventArgs e)
            {
                // throw new Exception("The method or operation is not implemented.");
                float newx = (this.Width) / X;
                //  float newy = (this.Height - this.statusStrip1.Height) / (Y - y);
                float newy = this.Height / Y;
                setControls(newx, newy, this);
                this.Text = this.Width.ToString() + " " + this.Height.ToString();        }