欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# 控件随窗体自动变化

程序员文章站 2022-06-10 13:17:04
...
   #region 控件随窗体自动变化
    /// <summary>
    ///  自动调整空间大小;
    /// </summary>
    private Size m_szInit;//初始窗体大小;
    private Dictionary<Control, System.Drawing.Rectangle> m_dicSize = new Dictionary<Control, System.Drawing.Rectangle>();
    //private bool Manal_Start_left, Manal_Start_Right;
    //private bool threadIs_STart;
    protected override void OnLoad(EventArgs e)
    {
        m_szInit = this.Size;//获取初始大小;
        this.GetInitSize(this);
        base.OnLoad(e);
    }
    private void GetInitSize(Control ctrl)
    {
        foreach (Control c in ctrl.Controls)
        {
            m_dicSize.Add(c, new System.Drawing.Rectangle(c.Location, c.Size));
            this.GetInitSize(c);
        }
    }
    protected override void OnResize(EventArgs e)
    {
        // runOne = true;
        //计算当前大小和初始大小的比例;
        float fx = (float)this.Width / m_szInit.Width;
        float fy = (float)this.Height / m_szInit.Height;
        foreach (var v in m_dicSize)
        {
            v.Key.Left = (int)(v.Value.Left * fx);
            v.Key.Top = (int)(v.Value.Top * fy);
            v.Key.Width = (int)(v.Value.Width * fx);
            v.Key.Height = (int)(v.Value.Height * fy);
        }
        base.OnResize(e);
    }
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;

            cp.ExStyle |= 0x02000000;

            return cp;

        }
    }



    #endregion
相关标签: C# c#