110,538
社区成员
发帖
与我相关
我的任务
分享
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);
}
//控件根据zoom重新缩放
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);
//对每一个控件重新生成坐标
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, 5); 实现缩放,1.0