c# winform自适应设置全部控件的大小位置字体大小,自适应屏幕大小分辨率问题。


using ShowScreen;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
 
namespace SmartMouse
{
    public partial class frmAutoSize  
    {
        public void Init(Control   control)
        {

            double width2 = SystemParameters.PrimaryScreenWidth;
            double width3 = Screen.PrimaryScreen.Bounds.Width;
            double height2 = SystemParameters.PrimaryScreenHeight;
            double height3 = Screen.PrimaryScreen.Bounds.Height;

            x = control.Size.Width;
            y = control.Size.Height;
            AddDic(control);
            control.SizeChanged += frmAutoSize_SizeChanged;
            control.Resize += frmAutoSize_SizeChanged;
        } 

        private void Control_Resize(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        Dictionary<string, OneCon> dic = new Dictionary<string, OneCon>();//原始控件大小
        float x = 0;//原始大小
        float y = 0;

        /// <summary>
        /// /添加数据
        /// </summary>
        /// <param name="con"></param>
        void AddDic(Control con)
        {
            foreach (Control item in con.Controls)
            {
                if (item.Controls.Count != 0)
                {
                    //if (item is uOneScreen == false)
                    {
                        AddDic(item);
                    }
                }
                SetColor(item);
                OneCon oneCon = new OneCon();
                oneCon.X = item.Location.X;
                oneCon.Y = item.Location.Y;
                oneCon.Width = item.Width;
                oneCon.Height = item.Height;
                oneCon.fontSize = item.Font.Size;
                string name = getAllName(item);
                if (dic.Keys.Contains(name))
                {
                    continue;
                }
                dic.Add(name, oneCon);
            }
            SetColor(con);
        }
        Random random = new Random();
        void SetColor(Control item)
        {
            //return;
            item.ForeColor = Color.White;
            //item.BackColor = Color.FromArgb(14, 33, 71);
            int x2 = (int)(item.Width / x * 20)-10;
            item.BackColor = Color.FromArgb(14 + x2, 33 + x2, 71 + x2);
            //item.BackColor = Color.FromArgb(random.Next(0, 30), random.Next(10, 50), random.Next (60,80));
        }
        string getAllName(Control item)
        {
            string s1 = "";
            while (true)
            {
                s1 += item.Name+"-";
                if (item.Parent == null)
                {
                    break;
                }
                item = item.Parent;
            }
            return s1;
        }
        /// <summary>
        /// 窗体变化大小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmAutoSize_SizeChanged(object sender, EventArgs e)
        {
            //return;
            Control control = (Control)sender;
            float x1 = (float )control.Width / x;
            float y1 = (float)control.Height / y;
            SetSize(control, x1, y1);
        }

        /// <summary>
        /// /设置全部的控件大小位置字体大小
        /// </summary>
        /// <param name="con"></param>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        void SetSize(Control con, float x1, float y1)
        {
            foreach (Control item in con.Controls)
            {
                if (item.Controls.Count != 0)
                {
                    SetSize(item, x1, y1);
                }
                string name = getAllName(item);
                if (dic.Keys.Contains(name) == false)
                {
                    continue;
                }
                OneCon rec = dic[name];
                item.Location = new Point((int)(rec.X * x1), (int)(rec.Y * y1));
                item.Size = new Size((int)(rec.Width * x1), (int)(rec.Height * y1));
                //item.Font.Size = (int)(9 * x1);
                double  xy = rec.fontSize  * x1;//使用最小的字体变化,默认字体9号
                if (x < y)
                {
                    xy = rec.fontSize * y1;
                }
                xy /= 1.5;
                item.Font = new System.Drawing.Font("宋体", (float )xy, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                //item.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            }
        }
    }
    public class OneCon
    {
        public int X = 0;
        public int Y = 0;
        public int Width = 10;
        public int Height = 10;
        public float  fontSize = 9;
    }
}

上面是一个类

   只需要在新的界面的调用 

new frmAutoSize().Init(this );

就好了

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值