C#实现窗体中的各个控件同比自动放缩大小

发布时间:2022-04-21 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了C#实现窗体中的各个控件同比自动放缩大小脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

实现方式主要是利用panel控件为主题,对于每个控件的大小位置和字体这几个属性进行记录,然后根据窗体改变的大小同时放缩。

简要步骤如下:

1、创建C#窗体程序项目。
2、Panel放置到窗体。
3、设置属性dock为fill。
4、注意MinnumSize不能设置为0, 改成大于0都行。

复制代码 代码如下:

public partial class FrmDemo : Form 
    { 
        double DFrmWidth; 
        double dFrmHeight; 
        double dZoomHorizon; 
        double dZoomVerticalITy; 
        Dictionary<string, string> dicControlsAttribute = new Dictionary<string, string>(); 
 
        PRotected void GetAllInitiateContrlInfo(Control CrlContainer) 
        { 
            if (CrlContainer.Parent == this) 
            { 
                dFrmWidth = Convert.ToDouble(CrlContainer.Width); 
                dFrmHeight = Convert.ToDouble(CrlContainer.Height); 
            } 
            foreach (Control item in CrlContainer.Controls) 
            { 
                if (item.Name.Trim() != "") 
                    dicControlsAttribute.Add(item.Name, (item.Left + item.Width / 2) + "," + (item.Top + item.Height / 2)  
                                             + "," + item.Width + "," + item.Height + "," + item.Font.Size); 
                if ((item as UserControl) == null && item.Controls.Count > 0) 
                    GetAllInitiateContrlInfo(item); 
            } 
        } 
 
        private void ChangeControlsInitiate(Control CrlContainer) 
        { 
            dZoomHorizon = (Convert.ToDouble(CrlContainer.Width) / dFrmWidth); 
            dZoomVerticality = (Convert.ToDouble(CrlContainer.Height) / dFrmHeight); 
        } 
         
        private void ChangecurrentControlAttr(Control CrlContainer) 
        { 
            double[] dPosition = new double[5]; 
            foreach (Control item in CrlContainer.Controls) 
            { 
                if (item.Name.Trim() != "") 
                { 
                    if ((item as UserControl) == null && item.Controls.Count > 0) 
                        ChangeCurrentControlAttr(item); 
                    string[] strs = dicControlsAttribute[item.Name].Split(','); 
                    for (int j = 0; j < 5; j++) 
                    { 
                        dPosition[j] = Convert.ToDouble(strs[j]); 
                    } 
                    double itemWidth = dPosition[2] * dZoomHorizon; 
                    double itemHeight = dPosition[3] * dZoomVerticality; 
                    item.Left = Convert.ToInt32(dPosition[0] * dZoomHorizon - itemWidth / 2); 
                    item.Top = Convert.ToInt32(dPosition[1] * dZoomVerticality - itemHeight / 2); 
                    item.Width = Convert.ToInt32(itemWidth); 
                    item.Height = Convert.ToInt32(itemHeight); 
                    //item.Font = new Font(item.Font.Name, float.Parse 
                    //((dPosition[4] * Math.Min(dZoomHorizon, dZoomVerticality)).ToString())); 
                    //字体也可以实现同比放缩。 
                     } 
            } 
        } 
        protected override void OnSizeChanged(Eventargs e) 
        { 
            base.OnSizeChanged(e); 
            if (dicControlsAttribute.Count > 0) 
            { 
                ChangeControlsInitiate(this.Controls[0]); 
                ChangeCurrentControlAttr(this.Controls[0]); 
            } 
        }  
 
        public FrmDemo() 
        { 
            Initializecomponent(); 
            GetAllInitiateContrlInfo(this.Controls[0]);//构造函数里面调用即可。 
        } 
}

5、效果测试

脚本宝典总结

以上是脚本宝典为你收集整理的C#实现窗体中的各个控件同比自动放缩大小全部内容,希望文章能够帮你解决C#实现窗体中的各个控件同比自动放缩大小所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。