zoukankan      html  css  js  c++  java
  • 控件缩放

     #region zhj-2018-11-19
    
            #region 控件缩放
            double formWidth;//窗体原始宽度
            double formHeight;//窗体原始高度
            double scaleX;//水平缩放比例
            double scaleY;//垂直缩放比例
            Dictionary<string, string> controlInfo = new Dictionary<string, string>();//控件中心Left,Top,控件Width,控件Height,控件字体Size
            /// <summary>
            /// 获取所有原始数据
            /// </summary>
            protected void GetAllInitInfo(Control CrlContainer)
            {
                if (CrlContainer.Parent == this)
                {
                    formWidth = Convert.ToDouble(CrlContainer.Width);
                    formHeight = Convert.ToDouble(CrlContainer.Height);
                }
                foreach (Control item in CrlContainer.Controls)
                {
                    if (item.Name.Trim() != "")
                        controlInfo.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)
                        GetAllInitInfo(item);
                }
            }
            private void ControlsChangeInit(Control CrlContainer)
            {
                scaleX = (Convert.ToDouble(CrlContainer.Width) / formWidth);
                scaleY = (Convert.ToDouble(CrlContainer.Height) / formHeight);
            }
            private void ControlsChange(Control CrlContainer)
            {
                double[] pos = new double[5];//pos数组保存当前控件中心Left,Top,控件Width,控件Height,控件字体Size
                foreach (Control item in CrlContainer.Controls)
                {
                    if (item.Name.Trim() != "")
                    {
                        if ((item as UserControl) == null && item.Controls.Count > 0)
                            ControlsChange(item);
                        string[] strs = controlInfo[item.Name].Split(',');
                        for (int j = 0; j < 5; j++)
                        {
                            pos[j] = Convert.ToDouble(strs[j]);
                        }
                        double itemWidth = pos[2] * scaleX;
                        double itemHeight = pos[3] * scaleY;
                        item.Left = Convert.ToInt32(pos[0] * scaleX - itemWidth / 2);
                        item.Top = Convert.ToInt32(pos[1] * scaleY - itemHeight / 2);
                        item.Width = Convert.ToInt32(itemWidth);
                        item.Height = Convert.ToInt32(itemHeight);
                        item.Font = new Font(item.Font.Name, float.Parse((pos[4] * Math.Min(scaleX, scaleY)).ToString()));
                    }
                }
            }
    
            protected override void OnSizeChanged(EventArgs e)
            {
                base.OnSizeChanged(e);
                if (controlInfo.Count > 0)
                {
                    ControlsChangeInit(this.Controls[0]);
                    ControlsChange(this.Controls[0]);
                }
            }
            #endregion 
    
            //private void FormSCAB_MouseDown(object sender, MouseEventArgs e)
            //{
            //    int x = e.X; //相对form窗口的坐标,客户区坐标  
            //    int y = e.Y;
            //    int x1 = Control.MousePosition.X;//相对显示器,屏幕的坐标  
            //    int y1 = Control.MousePosition.Y;    
            //}
    
            #endregion
  • 相关阅读:
    《面试题 03.05. 栈排序》——惰性更新
    CTF<密码学> writeup 传统知识+古典密码
    有趣的数学(二)
    【】Dedecms友情链接去掉LI的方法介绍
    【】【】打工子弟学校学生们的中国梦何时圆?
    Discuz! 7.2 防注册机注册+发垃圾帖的解决方法
    Windows 8 常用快捷键
    Win7 64位系统下Auto CAD 2010注册激活,出现警告:Make sure you can write to current directory...
    怎么从EXCEL或WORD里提取图片?
    如何将windows xp系统下的outlook express6.0的邮件,帐号及通迅录导入Office Outlook xp/2003/2007中...
  • 原文地址:https://www.cnblogs.com/aijiao/p/10142457.html
Copyright © 2011-2022 走看看