首页 > 其他分享 >WInform 控件大小随窗体大小等比例缩放

WInform 控件大小随窗体大小等比例缩放

时间:2024-08-01 17:53:27浏览次数:12  
标签:控件 Convert 缩放 System newy 窗体 con

winform控件自动大小

using System;
using System.Drawing;
using System.Windows.Forms;
using UCControl = System.Windows.Forms.Control;


/// <summary>
/// 控件自动适应
/// 使用直接继承 FormAutoSize即可
/// 示例如下:
/// ***示例代码***:public Form1()
/// ***示例代码***:{
/// ***示例代码***:       InitializeComponent();
/// ***示例代码***:       SetAutoSize();
/// ***示例代码***: }
/// </summary>
public class FormAutoSize : Form
{
    public FormAutoSize() : base()
    {
        this.Resize += Form1_Resize;
    }
    /// <summary>
    /// 设置控件大小随窗体大小等比例缩放
    /// </summary>
    public void _SetAutoSize()
    {
        XClientRectangleWidth = this.ClientRectangle.Width; //this.Width; ClientRectangleWidth
        YClientRectangleHeight = this.ClientRectangle.Height;// this.Height;
        SetTag(this);
    }
    #region 控件大小随窗体大小等比例缩放
    private float XClientRectangleWidth;//定义当前窗体的工作区域宽度
    private float YClientRectangleHeight;//定义当前窗体的工作区域高度
    private void SetTag(UCControl cons)
    {
        foreach (UCControl 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, UCControl cons)
    {
        //遍历窗体中的控件,重新设置控件的值
        foreach (UCControl con in cons.Controls)
        {
            //获取控件的Tag属性值,并分割后存储字符串数组
            if (con.Tag != null)
            {
                string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
                //根据窗体缩放的比例确定控件的值
                con.Width = Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * newx);//宽度
                con.Height = Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * newy);//高度
                con.Left = Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * newx);//左边距
                con.Top = Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * newy);//顶边距
                Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;//字体大小
                con.Font = new Font(con.Font.Name, currentSize == 0 ? 1 : currentSize, con.Font.Style, con.Font.Unit);
                Console.WriteLine($"setControls={con.Name}");
                if (con.Controls.Count > 0)
                {
                    setControls(newx, newy, con);
                }
            }
        }
    }
    private void Form1_Resize(object sender, EventArgs e)
    {
        float newx = (this.ClientRectangle.Width) / XClientRectangleWidth;
        float newy = (this.ClientRectangle.Height) / YClientRectangleHeight;

        this.SuspendLayout();//临时挂起控件的布局逻辑。 
        setControls(newx, newy, this);
        this.ResumeLayout(false);//恢复正常的布局逻辑,可以选择强制对挂起的布局请求立即进行布局。
    }

    #endregion
}



/// <summary> 
/// 指定控件(panel)绽放
///  cas = new ControlAutoSize(panel1);
///  cas._SetAutoSize();
/// </summary>
public class ControlAutoSize
{
    private UCControl uccontrol;
    public ControlAutoSize(UCControl control)// : base()
    {
        uccontrol = control;
        control.Resize += Control1_Resize;
    }
    /// <summary>
    /// 设置控件大小随窗体大小等比例缩放
    /// </summary>
    public void _SetAutoSize()
    {
        XClientRectangleWidth = uccontrol.ClientRectangle.Width; //control.Width; ClientRectangleWidth
        YClientRectangleHeight = uccontrol.ClientRectangle.Height;// control.Height;
        SetTag(uccontrol);
    }
    #region 控件大小随窗体大小等比例缩放
    private float XClientRectangleWidth;//定义当前窗体的工作区域宽度
    private float YClientRectangleHeight;//定义当前窗体的工作区域高度
    private void SetTag(UCControl cons)
    {
        foreach (UCControl 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, UCControl cons)
    {
        //遍历窗体中的控件,重新设置控件的值
        foreach (UCControl con in cons.Controls)
        {
            //获取控件的Tag属性值,并分割后存储字符串数组
            if (con.Tag != null)
            {
                string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
                //根据窗体缩放的比例确定控件的值
                con.Width = Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * newx);//宽度
                con.Height = Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * newy);//高度
                con.Left = Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * newx);//左边距
                con.Top = Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * newy);//顶边距
                Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;//字体大小
                con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                Console.WriteLine($"setControls={con.Name}");
                if (con.Controls.Count > 0)
                {
                    setControls(newx, newy, con);
                }
            }
        }
    }
    private void Control1_Resize(object sender, EventArgs e)
    {
        float newx = (uccontrol.ClientRectangle.Width) / XClientRectangleWidth;
        float newy = (uccontrol.ClientRectangle.Height) / YClientRectangleHeight;

        uccontrol.SuspendLayout();//临时挂起控件的布局逻辑。 
        setControls(newx, newy, uccontrol);
        uccontrol.ResumeLayout(false);//恢复正常的布局逻辑,可以选择强制对挂起的布局请求立即进行布局。
    }

    #endregion
}

 

建议使用 
ControlAutoSize 这个类,

标签:控件,Convert,缩放,System,newy,窗体,con
From: https://www.cnblogs.com/orp1989/p/18337173

相关文章

  • QT之ui控件随窗口布局的大小而自适应大小
    QT之ui窗口自适应布局新建个工程说明,注意此处勾上Generateform根据开发电脑的系统选择套件点开widget.ui,如图鼠标随意托几个常用控件展示,如图三个控件,如图,水平布局sizePolicy策略:图中,控件的sizePolicy策略将决定上面这三个控件组在自适应成的控件组的分配策略。Fi......
  • ICSTCP控件
    delphi7代码:unitUnit1;interfaceusesWindows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,StdCtrls,ExtCtrls,OverbyteIcsWndControl,OverbyteIcsWSocket,OverbyteIcsWSocketS;typeTForm1=class(TForm)WSocketServer1:TWSock......
  • 5.为工作使用正确的控件
    在本章中,我们将首先考虑WindowsPresentationFoundation(WPF)为我们提供的现有控件,并了解如何使用它们来创建我们需要的布局。我们将研究修改这些控件的多种方法,以避免创建新控件。我们将检查现有控件中内置的各个级别的功能,然后发现如何在需要时最好地声明我们自己的......
  • 6.调整内置控件
    每个.NET控件都有许多方法,每个方法都以前缀On命名,例如OnInitialized或OnApplyTemplate。这些是受保护的方法,可以在任何扩展.NET控件的自定义类中重写。它们在控件生命周期中的某些点被调用,使我们能够更改每个控件的默认行为。它们使我们能够做一些简单的事情,例......
  • 界面控件DevExpress WinForms,支持HTML & CSS提升用户体验(一)
    DevExpressWinForms现在可以利用HTML/CSS强大的功能,帮助受DevExpress驱动的WinForms应用程序引入现代的UI元素和用户体验!P.S:DevExpressWinForms拥有180+组件和UI库,能为WindowsForms平台创建具有影响力的业务解决方案。DevExpressWinForms能完美构建流畅、美观且易于使用的应......
  • 删除不必要的 Seaborn Boxplot y 轴缩放
    我正在使用Seaborn生成许多箱线图。大多数都使用正确的线性y轴刻度生成良好,但是有些在图的左上角有一个值(例如1e-7+7.0334e-1),我认为这是一个缩放器,因此y轴标签看起来确实错误(显示〜5.8而不是〜0.7)。有没有办法可以在没有这个缩放因子的情况下强制显示y轴刻度?提前致谢......
  • 移动端页面字体缩放问题处理
    [移动端浏览器会缩放字体导致页面布局变化]ps:这个问题弄得我整个人都不好了,移动端浏览器可以设置字体缩放,而且不同浏览器不同设备默认缩放程度不同。仔细观察页面,就会发现也不是所有的字体都会被缩放。经过反复对比发现,提供了宽高数值,并且设置了flex布局的文字就不会被缩......
  • 如何通过前端表格控件实现自动化报表?
    背景最近伙伴客户的项目经理遇见一个问题,他们在给甲方做自动化报表工具,项目已经基本做好了,但拿给最终甲方,业务人员不太买账,项目经理为此也是天天抓狂,没有想到合适的应对方案。现阶段主要面临的问题是项目经理和甲方沟通好了需求,就布置了任务,让研发去调研。但由于现在市面上报表......
  • Android 四大控件
    一、Activity  在Android应用中,Activity是一个非常核心的组件,用于表示应用的一个单一屏幕,是用户与应用交互的主界面。每个Activity提供一个窗口,用于绘制界面和接收与用户的交互事件。理解Activity的创建、生命周期和其基本用法对于开发Android应用至关重要。 一个......
  • 报表控件DevExpress Reporting v24.1 - 全新升级报表查看器功能
    DevExpressReporting是.NETFramework下功能完善的报表平台,它附带了易于使用的VisualStudio报表设计器和丰富的报表控件集,包括数据透视表、图表,因此您可以构建无与伦比、信息清晰的报表。DevExpressReporting控件日前正式发布了v24.1,新版本重点针对报表查看器的一系列功能进行......