您的位置:首页 > 博客中心 > 电脑问题 >

C# winform根据屏幕大小自动缩放

时间:2022-03-18 00:52

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
}






本类排行

今日推荐

热门手游