作者: 死胖子叔叔
查看: 738|回复: 3

[源码] 【等比缩放】窗体/控件 等比缩放子控件不偏移

[复制链接]
死胖子叔叔 发表于 2018-4-2 12:25:53 | 显示全部楼层 |阅读模式
查看: 738|回复: 3
[C#] 纯文本查看 复制代码
        /// <summary>
        /// Zoom Control(Form) Ex 1.0 By Xomix comic_729#sina.com
        /// </summary>
        /// <param name="c">Control(Form) Need Zoom .</param>
        /// <param name="Zoom">Zoom, 10:Nothing , less than 10:Zoom out , more than 10:Zoom in </param>
        private void ZoomEX(System.Windows.Forms.Control c, int Zoom)
        {
 
            //求出相对中心
            System.Drawing.Point p0 = new System.Drawing.Point(c.Width / 2, c.Height / 2);
            //获取控件列表,计算控件距离中心位置得出相对坐标。
            System.Collections.Generic.LinkedList<ControlEx> Controls
                = new System.Collections.Generic.LinkedList<ControlEx>();
            foreach (System.Windows.Forms.Control item in c.Controls)
            {
                //查看该子控件是否有子控件,如果有,自调用本方法
                if (item.HasChildren)
                {
                    ZoomEX(item, Zoom);
                }
                ControlEx itemEx = new ControlEx(item);
                itemEx.ExPoint = new ControlEx.exPoint(((float)item.Left - (float)p0.X) / (float)c.Width, ((float)item.Top - (float)p0.Y) / (float)c.Height);
                itemEx.ExSize = new ControlEx.exSize((float)item.Width / (float)c.Width, (float)item.Height / (float)c.Height);
                Controls.AddLast(itemEx);
            }
            //求出新的Size
            System.Drawing.Size newSize = new System.Drawing.Size(c.Width * Zoom / 10, c.Height * Zoom / 10);
            //求出新的相对中心
            System.Drawing.Point pNew = new System.Drawing.Point(newSize.Width / 2, newSize.Height / 2);
            //对每一个控件重新生成坐标和Size
            foreach (ControlEx item in Controls)
            {
                item.Control.Size = new System.Drawing.Size((int)(item.ExSize.Width * newSize.Width), (int)(item.ExSize.Height * newSize.Height));
                item.Control.Location = new System.Drawing.Point((int)(item.ExPoint.X * newSize.Width + pNew.X), (int)(item.ExPoint.Y * newSize.Height + pNew.Y));
            }
            c.Size = newSize;//调整控件本身大小
        }
 
        class ControlEx
        {
            public System.Windows.Forms.Control Control  { get; set; }
            public ControlEx(System.Windows.Forms.Control c)
            {
                Control = c;
            }
            public exPoint ExPoint { get; set; }
            public exSize ExSize { get; set; }
            public class exPoint
            {
                public float X;
                public float Y;
 
                public exPoint(float x, float y)
                {
                    // TODO: Complete member initialization
                    this.X = x;
                    this.Y = y;
                }
            }
            public class exSize {
                public exSize(float p1, float p2)
                {
                    // TODO: Complete member initialization
                    this.Width = p1;
                    this.Height = p2;
                }
                public float Width { get; set; }
                public float Height { get; set; }
            }
        }
 
//窗体使用   ZoomEX(this, 15);   实现缩放,1.5


前几天无聊做的窗口等比缩放,喜欢的拿走吧,主要解决已经设计好的窗体在分辨率变化后太小/太大的问题。
什么你们说Echarts?我心情还没那么好哈哈哈。
回复 论坛版权

使用道具 举报

boxxie 发表于 2018-4-3 09:57:31 | 显示全部楼层

多谢楼主分享哟
895090595 发表于 2018-4-21 00:39:44 | 显示全部楼层
嘿嘿,评论一下  以后不然找不到.(窗体/控件 等比缩放子控件不偏移)
mqzzzz 发表于 2018-5-17 14:08:18 | 显示全部楼层
动态添加的控件应该无效吧
您需要登录后才可以回帖 登录 | 加入CSkin论坛

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|CSkin ( 粤ICP备13070794号

Powered by Discuz! X3.2  © 2001-2013 Comsenz Inc.  Designed by ARTERY.cn
GMT+8, 2018-9-22 05:32, Processed in 0.808997 second(s), 32 queries , Gzip On.

快速回复 返回顶部 返回列表