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
- C# WinForm 窗体 自适应
- C#窗体自适应
- C#窗体自适应
- C#窗体自适应显示器大小
- C#实现控件与窗体大小自适应
- C# WinForm窗体及其控件的自适应
- C# WinForm窗体及其控件的自适应
- C# WinForm窗体及其控件的自适应
- C# WinForm窗体及其控件的自适应
- c# 窗体设计的自适应调节
- 关于“C# 窗体程序自适应分辨率” 的一些探索
- C# WinForm窗体及其控件自适应各种屏幕分辨率
- C# WinForm窗体及其控件自适应各种屏幕分辨率
- C# WinForm窗体及其控件自适应各种屏幕分辨率
- C# WinForm窗体及其控件自适应各种屏幕分辨率
- C# WinForm窗体及其控件自适应各种屏幕分辨率
- 改进C# WinForm窗体及其控件自适应各种屏幕分辨率
- 【转】C# WinForm窗体及其控件的自适应
- FZUOJ 2150 Problem 2150 Fire Game (双起点BFS)
- Android-Uiautomator:[6]运行中传入参数
- Scala-01:函数定义、流程控制、异常处理
- Android笔记--对同步与异步的概念上的理解和对LayoutParams的理解
- 一起学CC3200之开发环境简介(2)烧录程序
- C#窗体自适应
- 应用程序之间的跳转设置info.plist
- 数列排序
- 算法导论 Exercises 9.3-9
- PHP标准 —— 框架的互操作性
- 用OpenCV创建第一个图像
- php实现冒泡排序
- VS2013写window服务,实现定时短信功能
- handler相关