您的位置:首页 > 编程语言 > C#

C#Winform控件随窗体缩放

2015-06-27 17:13 369 查看
解决方法一:(测试可用)

/// <summary>
/// 缩放比
/// </summary>
private double m_AllZoomScale = 1.0;
/// <summary>
/// 控件中心Left,Top,控件Width,控件Height,控件字体Size
/// </summary>
private Dictionary<string, string> m_DctControlInfo = new Dictionary<string, string>();

/// <summary>
/// 缩放控件
/// </summary>
private void Resize_Zoom()
{
if (m_DctControlInfo.Count > 0)
{
Resize_ControlsChange(this.panelPDF);
}
}

/// <summary>
/// 递归获取所有要缩放的控件
/// </summary>
private void Resize_GetAllInitInfo(Control CrlContainer)
{
foreach (Control item in CrlContainer.Controls)
{
if (item.Name.Trim() != string.Empty)
m_DctControlInfo.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)
Resize_GetAllInitInfo(item);
}
}

/// <summary>
/// 缩放存储的所有控件
/// </summary>
private void Resize_ControlsChange(Control CrlContainer)
{
// pos数组保存当前控件中心Left,Top,控件Width,控件Height,控件字体Size
double[] pos = new double[5];
foreach (Control item in CrlContainer.Controls)
{
if (item.Name.Trim() != string.Empty)
{
if ((item as UserControl) == null &&item.Controls.Count > 0)
Resize_ControlsChange(item);

string[] strs = m_DctControlInfo[item.Name].Split(',');
for (int j = 0; j < 5; j++)
{
pos[j] = Convert.ToDouble(strs[j]);
}

double itemWidth = pos[2] * m_AllZoomScale;//scaleX;
double itemHeight = pos[3] * m_AllZoomScale;//scaleY;
item.Left = Convert.ToInt32(pos[0] * m_AllZoomScale - itemWidth / 2);//scaleX
item.Top = Convert.ToInt32(pos[1] * m_AllZoomScale - itemHeight / 2);//scaleY
item.Width = Convert.ToInt32(itemWidth);
item.Height = Convert.ToInt32(itemHeight);
item.Font = new Font(item.Font.Name, float.Parse((pos[4] * m_AllZoomScale).ToString()));//scaleX, scaleY
}
}
}

Resize_GetAllInitInfo() 函数用于递归获取并保存所有要缩放的控件到m_DctControlInfo变量中。

Resize_Zoom() 函数中的“this.panelPDF”应该是所有缩放控件的顶层容器,应当和调用Resize_GetAllInitInfo() 函数时传递的值一样。

×此方法参考修改自:http://www.cnblogs.com/sydeveloper/archive/2013/01/29/2881521.html

解决方法二:(测试可用)

/// <summary>
/// 缩放比
/// </summary>
private float m_AllZoomScale = 1.0f;

/// <summary>
/// 缩放控件
/// </summary>
private void Resize_Zoom()
{
if (m_DctControlInfo.Count > 0)
{
this.panelPDF.Scale(new SizeF(m_AllZoomScale, m_AllZoomScale));
}
}


一般控件都继承自Control,而Control有个Scale函数,使用此函数也可进行缩放。

×但此方法相对于“解决方法一”存在控件中输入的字体不能放大,和连续点击放大缩小时似乎存在间隔(?)的问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# winform 缩放