C#Winform控件随窗体缩放
2015-06-27 17:13
369 查看
解决方法一:(测试可用)
Resize_GetAllInitInfo() 函数用于递归获取并保存所有要缩放的控件到m_DctControlInfo变量中。
Resize_Zoom() 函数中的“this.panelPDF”应该是所有缩放控件的顶层容器,应当和调用Resize_GetAllInitInfo() 函数时传递的值一样。
×此方法参考修改自:http://www.cnblogs.com/sydeveloper/archive/2013/01/29/2881521.html
解决方法二:(测试可用)
一般控件都继承自Control,而Control有个Scale函数,使用此函数也可进行缩放。
×但此方法相对于“解决方法一”存在控件中输入的字体不能放大,和连续点击放大缩小时似乎存在间隔(?)的问题。
/// <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#调用COM组件
- C#实现把指定数据写入串口
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- winform绑定快捷键的方法
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#通用邮件发送类分享
- C#中this的用法集锦
- C#.NET获取拨号连接的宽带连接方法
- C#中Equality和Identity浅析
- C#生成饼形图及添加文字说明实例代码
- C#判等对象是否相等的方法汇总
- C#简单的向量用法实例教程
- C#托管堆对象实例包含内容分析
- C#实现按照指定长度在数字前补0方法小结
- C#虚方法的声明和使用实例教程
- C#获取文件夹及文件的大小与占用空间的方法
- C#定义简单的反射工厂实例分析
- C#数字图象处理之肤色检测的方法