首页 / C# / C# winform根据屏幕大小自动缩放
C# winform根据屏幕大小自动缩放
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C# winform根据屏幕大小自动缩放,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3116字,纯文字阅读大概需要5分钟。
内容图文
1 Form基类
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.IO; using System.Reflection; /// <summary> /// 用法: /// 1 新建Form窗体,继承BaseForm /// 2 新Form添加panel, dock = fill; /// 3 新Form load事件添加 /// </summary> public class BaseForm : Form { #region 属性 double formWidth, formHeight;//初始化大小 double xScale, yScale;//缩?放比括例 //控?件t坐?标括,?大洙小?,?字?体? Dictionary<string, string> controlInfo = new Dictionary<string, string>(); #endregion #region 方?法 public void RemoveVirtualBorder(object obj) { MethodInfo methodinfo = obj.GetType().GetMethod("SetStyle", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod); methodinfo.Invoke(obj, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, new object[] { ControlStyles.Selectable, false }, Application.CurrentCulture); } /// <summary> /// 获?取?原-始?大洙小? /// </summary> /// <param name="CrlContainer"></param> protected void GetInitInfo(Control CrlContainer) { if (CrlContainer.Parent == this) { formWidth = Convert.ToDouble(CrlContainer.Width); formHeight = Convert.ToDouble(CrlContainer.Height); } foreach (Control c in CrlContainer.Controls) { if (c.Name.Trim() != "") controlInfo.Add(c.Name, (c.Left + c.Width / 2) + "," + (c.Top + c.Height / 2) + "," + c.Width + "," + c.Height + "," + c.Font.Size); if ((c as UserControl) == null && c.Controls.Count > 0) GetInitInfo(c); RemoveVirtualBorder(c); } } /// <summary> /// 获?取?缩?放?比括例 /// </summary> /// <param name="CrlContainer"></param> private void ControlsChangeInit(Control CrlContainer) { xScale = (Convert.ToDouble(CrlContainer.Width)) / formWidth; yScale = (Convert.ToDouble(CrlContainer.Height)) / formHeight; } /// <summary> /// 缩?放?控?件t /// </summary> /// <param name="CrlContainer"></param> private void ControlsChange(Control CrlContainer) { double[] pos = new double[5];//存?放?当獭前°控?件t关?键ü值μ。£ foreach (Control c in CrlContainer.Controls) { if (c.Name.Trim() != "") { if ((c as UserControl) == null && c.Controls.Count > 0) ControlsChange(c); string[] strTmp = controlInfo[c.Name].Split(','); for (int i = 0; i < 5; i++) { pos[i] = Convert.ToDouble(strTmp[i]); } double cWidth = pos[2] * xScale; double cHeigth = pos[3] * yScale; c.Left = Convert.ToInt32(pos[0] * xScale - cWidth / 2); c.Top = Convert.ToInt32(pos[1] * yScale - cHeigth / 2); c.Width = Convert.ToInt32(cWidth); c.Height = Convert.ToInt32(cHeigth); double tmp; if (xScale == 0 && yScale == 0) tmp = 1; else tmp = Math.Min(xScale, yScale); c.Font = new Font(c.Font.Name, float.Parse((pos[4] * tmp).ToString())); } } } protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); if (controlInfo.Count > 0) { ControlsChangeInit(this.Controls[0]); ControlsChange(this.Controls[0]); } } #endregion }
原文:http://blog.csdn.net/taoerit/article/details/51917058
内容总结
以上是互联网集市为您收集整理的C# winform根据屏幕大小自动缩放全部内容,希望文章能够帮你解决C# winform根据屏幕大小自动缩放所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。