[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?我心情还没那么好哈哈哈。
|