首页 > 编程语言 >C#Winform怎么让控件随着主界面大小变化

C#Winform怎么让控件随着主界面大小变化

时间:2023-09-04 18:45:39浏览次数:41  
标签:控件 Convert C# System newy using Winform con

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

x = Width;
y = Height;
setTag(this);
}

private readonly float x; //定义当前窗体的宽度
private readonly float y; //定义当前窗体的高度

private void setTag(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) setTag(con);
}
}

private void setControls(float newx, float newy, Control cons)
{
//遍历窗体中的控件,重新设置控件的值
foreach (Control con in cons.Controls)
//获取控件的Tag属性值,并分割后存储字符串数组
if (con.Tag != null)
{
var mytag = con.Tag.ToString().Split(';');
//根据窗体缩放的比例确定控件的值
con.Width = Convert.ToInt32(Convert.ToSingle(mytag[0]) * newx); //宽度
con.Height = Convert.ToInt32(Convert.ToSingle(mytag[1]) * newy); //高度
con.Left = Convert.ToInt32(Convert.ToSingle(mytag[2]) * newx); //左边距
con.Top = Convert.ToInt32(Convert.ToSingle(mytag[3]) * newy); //顶边距
var currentSize = Convert.ToSingle(mytag[4]) * newy; //字体大小
if (currentSize > 0) con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
con.Focus();
if (con.Controls.Count > 0) setControls(newx, newy, con);

}
}

/// <summary>
/// 重置窗体布局
/// </summary>
private void ReWinformLayout()
{
var newx = Width / x;
var newy = Height / y;
setControls(newx, newy, this);

}

private void Form1_Resize(object sender, EventArgs e)
{
ReWinformLayout();
}
}
}

 

标签:控件,Convert,C#,System,newy,using,Winform,con
From: https://www.cnblogs.com/linnew/p/17677811.html

相关文章

  • C#下log4net日志记录使用实例完整教程
    C#下log4net日志记录使用实例完整教程1log4net1.1log4net简介1.2log4net源码下载1.3log4net源码编译1.3.1编译工具1.3.2编译步骤1.3.2.1解压log4net源码apache-log4net-source-2.0.10.zip1.3.2.2找到文件“……\log4......
  • 神策数据 CJO 系列丨解密 CJO:连接体验的下一个前沿趋势
    10余年前,市场营销的焦点聚集在增长黑客如何利用AARRR模型(获取Acquisition、激活Activation、留存Retention、收入Revenue、传播Referral)来推动并加速企业的生长发展。我们曾相信,在AARRR漏斗中,只要我们吸引了足够的目光,就能实现令人满意的转化。然而,如今我们身处一个触点......
  • ChatGLM2 源码解析:`ChatGLMModel`
    #完整的GLM模型,包括嵌入层、编码器、输出层classChatGLMModel(ChatGLMPreTrainedModel):def__init__(self,config:ChatGLMConfig,device=None,empty_init=True):super().__init__(config)#如果设置了`empty_init`,创建任何PyTorch模块时,不初......
  • ChatGLM2 源码解析:`ChatGLMForConditionalGeneration.forward`
    classChatGLMForConditionalGeneration(ChatGLMPreTrainedModel):def__init__(self,config:ChatGLMConfig,empty_init=True,device=None):super().__init__(config)self.max_sequence_length=config.max_lengthself.transformer=C......
  • 无涯教程-JavaScript - DCOUNTA函数
    描述DCOUNTA函数返回列表或数据库中符合您指定条件的列中非空白单元格的计数。此函数与DCOUNT函数相似,不同之处在于DCOUNTA函数对所有非空白单元进行计数。DCOUNT函数仅计算包含数值的单元格。语法DCOUNTA(database,field,criteria)争论Argument描述Required/Opti......
  • The colossus
    BYSYLVIAPLATHIshallnevergetyouputtogetherentirely,Pieced,glued,andproperlyjointed.Mule-bray,pig-gruntandbawdycacklesProtectedfromyourgreatlips.It'sworsethanabarnyardPehapsyouconsideryourselfanoracle,Mouthpiecep......
  • 关于synchronized
    关于synchronizedsynchronized是java中的关键字,可以在需要线程安全的业务场景中进行使用,保证线程安全,它是利用锁机制来实现同步的。synchronized锁对象和锁类对象锁:每个实例都会有一个monitor对象,即Java对象的锁,类的对象可以有多个,所以每个对象有其独立的对象锁,互不干扰......
  • [个人笔记][C#]反射和特性学习笔记
    反射通过Type类型的实例来访问类型的元数据通过obj.GetType()或typeof()获取一个Type类型的实例在Type类型的实例上通过GetProperties(),GetMethods(),GetCustomAttributes()访问类型的各种东西GetType()只能在实例上调用,typeof()给一个类型名就行,它是在编译时求值的验证一个......
  • win2016系统php7.4安装oracle oci8扩展
    查看php版本,判断操作系统是否64位;phpinfo();判断PHP是否TS查看ThreadSafety的值,如果是disabled就是NTS,否则是TS,下载的时候要区分;下载扩展oci82.2.0forWindows:https://pecl.php.net/package/oci8/2.2.0/windows下载并解压,把php_oci8.dll,php_oci8_11g.dll,php_oci8_12c......
  • 配置iSCSI存储
    【更新】【WindowsServer2019】存储服务器的配置和管理——iSCSI的安装和配置(上)_windows搭建iscsi服务器_NOWSHUT的博客-CSDN博客【WindowsServer2019】存储服务器的配置和管理——iSCSI的安装和配置(下)_win10搭建iscsi服务端_NOWSHUT的博客-CSDN博客......