C#窗体自适应

来源:互联网 发布:淘宝女装,连衣裙 编辑:程序博客网 时间:2024/04/30 16:58
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.Data.SqlClient;using System.Configuration;using System.IO;using System.Net;using System.Text.RegularExpressions;namespace com{    public partial class FrmDemo1 : Form    {        private float X; //全局变量:当前窗体的宽度        private float Y; //全局变量:当前窗体的高度        public FrmDemo1()        {            InitializeComponent();            this.Init();        }        protected void Init()        {            this.Text = "控件大小随窗体大小等比例变化";            this.txtUrl.Text = "http://www.31164.net/001.htm?gzid=P95090";            this.labIntr.Text = "请对窗体进行缩放操作,观察窗体上控件的变化";        }        #region “加载” 按钮事件        private void btnSubmit_Click_1(object sender, EventArgs e)        {            string url = this.txtUrl.Text.ToString();            bool flag = this.Validate(url);            if (flag == false)                return;            this.webBrowser1.Navigate(url);        }        #endregion        #region 验证输入的URL地址是否有效        private bool Validate(string url)        {            string print = "";            bool flag = true;            //判断网址是否合法            string pattern = @"^(http|https)://.*";            if (Regex.IsMatch(url, pattern) == false)            {                print += "网址不合法,请输入正确的网址!\r\n";                flag = false;            }            if (flag == false)            {                MessageBox.Show(print);            }            return flag;        }        #endregion        #region SaveParam 保存控件的各个属性值        /// <summary>        /// 将控件的宽,高,左边距,顶边距和字体大小暂存到tag属性中        /// </summary>        /// <param name="cons">递归控件中的控件</param>        private void SaveParam(Control cons)        {            foreach (Control con in cons.Controls)            {                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;                                if (con.Controls.Count > 0)                    SaveParam(con);            }        }        #endregion        #region 根据窗体大小调整控件大小        //根据窗体大小调整控件大小        private void ResizeControls(float newx, float newy, Control cons)        {            //遍历窗体中的控件,重新设置控件的值            foreach (Control con in cons.Controls)            {                string[] mytag = con.Tag.ToString().Split(new char[] { ';' });//获取控件的Tag属性值,并分割后存储字符串数组                                float a = System.Convert.ToSingle(mytag[0]) * newx;//根据窗体缩放比例确定控件的值                con.Width = (int)a;//宽度                                a = System.Convert.ToSingle(mytag[1]) * newy;//高度                con.Height = (int)(a);                                a = System.Convert.ToSingle(mytag[2]) * newx;//左边距离                con.Left = (int)(a);                                a = System.Convert.ToSingle(mytag[3]) * newy;//上边缘距离                con.Top = (int)(a);                                Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;//字体大小                con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);                                if (con.Controls.Count > 0)                {                    ResizeControls(newx, newy, con);                }            }        }        #endregion        private void FrmDemo1_Load(object sender, EventArgs e)        {            X = this.Width;            Y = this.Height;            this.SaveParam(this);        }        private void FrmDemo1_Resize(object sender, EventArgs e)        {            float newx = this.Width / X;  //获取比例,            float newy = this.Height / Y; //获取比例            this.ResizeControls(newx, newy, this);                    }    }}

0 0
原创粉丝点击