主页 >
新闻资讯 >
软件开发资讯 >
C++编程C++软件开发 >
C# winform根据屏幕大小自动缩放
更新时间:2016-07-15 14:12
发布者:周老师
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;
-
-
-
-
-
-
-
-
public class BaseForm : Form
-
{
-
#region 属性
-
double formWidth, formHeight;
-
double xScale, yScale;
-
-
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);
-
-
}
-
-
-
-
-
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);
-
-
}
-
}
-
-
-
-
-
-
private void ControlsChangeInit(Control CrlContainer)
-
{
-
xScale = (Convert.ToDouble(CrlContainer.Width)) / formWidth;
-
yScale = (Convert.ToDouble(CrlContainer.Height)) / formHeight;
-
}
-
-
-
-
-
-
private void ControlsChange(Control CrlContainer)
-
{
-
double[] pos = new double[5];
-
-
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
-
}
上一篇:
C++内存模型 下一篇:
C++中std::map的使用