第一种:推荐

在窗体中加上如下代码即可实现,但窗体点击放大按钮时却不能改变控件大小。


private Size beforeResizeSize = Size.Empty;protectedoverridevoid OnResizeBegin(EventArgs e){      base.OnResizeBegin(e);      beforeResizeSize =this.Size;}protectedoverridevoid OnResizeEnd(EventArgs e){      base.OnResizeEnd(e);      //窗口resize之后的大小      Size endResizeSize =this.Size;      //获得变化比例      float percentWidth = (float)endResizeSize.Width / beforeResizeSize.Width;      float percentHeight = (float)endResizeSize.Height / beforeResizeSize.Height;      foreach (Control control inthis.Controls)      {            if (control is DataGridView)                 continue;            //按比例改变控件大小            control.Width = (int)(control.Width * percentWidth);            control.Height = (int)(control.Height * percentHeight);            //为了不使控件之间覆盖 位置也要按比例变化            control.Left = (int)(control.Left * percentWidth);            control.Top = (int)(control.Top * percentHeight);     }}

第二种:效果很差

在加载事件中写 AutoScale(this);


//设置窗口控件随窗口大小改变而改变publicnewvoid AutoScale(Form frm){      frm.Tag = frm.Width.ToString() +","+ frm.Height.ToString();      frm.SizeChanged +=newEventHandler(frm_SizeChanged);}publicvoid frm_SizeChanged(object sender, EventArgs e){      string[] tmp = ((Form)sender).Tag.ToString().Split(',');      float width = (float)((Form)sender).Width / (float)Convert.ToInt32(tmp[0]);      float height = (float)((Form)sender).Height / (float)Convert.ToInt32(tmp[1]);      ((Form)sender).Tag = ((Form)sender).Width.ToString() +","+ ((Form)sender).Height;      string str = ((Form)sender).Tag.ToString();      // int font_size = Int32.Parse(str.Substring(0, str.IndexOf(','))) / 100;
      //也可使字体随之改变      float tempWidth=0F;      float tempHeight=0F;      foreach (Control control in ((Form)sender).Controls)      {           if (control is DataGridView) continue;           if (control is TextBox)           {                tempHeight = height;                tempWidth =width;           }            if (control is Button)           {                if (this.WindowState == FormWindowState.Maximized) tempHeight -=0.4F;                else tempHeight +=0.2F;                control.Scale(new SizeF(tempWidth, tempHeight));           }           else           {                control.Scale(new SizeF(width, height));           }      }}

第三种:http://www.cnblogs.com/kenkao/archive/2008/11/10/1330623.html

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

namespace WindowsApplication3
{
publicpartialclass Form1 : Form
{
/*******************设定程序中可能要用到的用以存储初始数据的动态数组及相关私有变量******************************/

private ArrayList InitialCrl =new ArrayList();//用以存储窗体中所有的控件名称
private ArrayList CrlLocationX =new ArrayList();//用以存储窗体中所有的控件原始位置
private ArrayList CrlLocationY =new ArrayList();//用以存储窗体中所有的控件原始位置
private ArrayList CrlSizeWidth =new ArrayList();//用以存储窗体中所有的控件原始的水平尺寸
private ArrayList CrlSizeHeight =new ArrayList();//用以存储窗体中所有的控件原始的垂直尺寸
privateint FormSizeWidth;//用以存储窗体原始的水平尺寸
privateint FormSizeHeight;//用以存储窗体原始的垂直尺寸
privatedouble FormSizeChangedX;//用以存储相关父窗体/容器的水平变化量
privatedouble FormSizeChangedY;//用以存储相关父窗体/容器的垂直变化量
privateint Wcounter =0;//为防止递归遍历控件时产生混乱,故专门设定一个全局计数器

public Form1()
{
InitializeComponent();
}

privatevoid Form1_Load(object sender, EventArgs e)
{
GetInitialFormSize();
//this.AutoScroll = true;
//this.SetAutoSizeMode(FormSizeWidth,FormSizeHeight);
//this.AutoScrollMinSize.Width = FormSizeWidth;
//this.AutoScrollMinSize.Height = FormSizeHeight;
GetAllCrlLocation(this);
GetAllCrlSize(this);
}
publicvoid GetAllCrlLocation(Control CrlContainer)//获得并存储窗体中各控件的初始位置
{
foreach (Control iCrl in CrlContainer.Controls)
{
if (iCrl.Controls.Count >0)
GetAllCrlLocation(iCrl);
InitialCrl.Add(iCrl);
CrlLocationX.Add(iCrl.Location.X);
CrlLocationY.Add(iCrl.Location.Y);
}
}

publicvoid GetAllCrlSize(Control CrlContainer)//获得并存储窗体中各控件的初始尺寸
{
foreach (Control iCrl in CrlContainer.Controls)
{
if (iCrl.Controls.Count >0)
GetAllCrlSize(iCrl);
CrlSizeWidth.Add(iCrl.Width);
CrlSizeHeight.Add(iCrl.Height);
}
}

publicvoid GetInitialFormSize()//获得并存储窗体的初始尺寸
{
FormSizeWidth =this.Size.Width;
FormSizeHeight =this.Size.Height;
}

privatevoid Form1_SizeChanged(object sender, EventArgs e)
{
// MessageBox.Show("窗体尺寸改变");
Wcounter =0;
int counter =0;
if (this.Size.Width < FormSizeWidth ||this.Size.Height < FormSizeHeight)
//如果窗体的大小在改变过程中小于窗体尺寸的初始值,则窗体中的各个控件自动重置为初始尺寸,且窗体自动添加滚动条
{
foreach (Control iniCrl in InitialCrl)
{
iniCrl.Width = (int)CrlSizeWidth[counter];
iniCrl.Height = (int)CrlSizeHeight[counter];
Point point =new Point();
point.X = (int)CrlLocationX[counter];
point.Y = (int)CrlLocationY[counter];
iniCrl.Bounds =new Rectangle(point, iniCrl.Size);
counter++;
}
this.AutoScroll =true;
}
else
//否则,重新设定窗体中所有控件的大小(窗体内所有控件的大小随窗体大小的变化而变化)
{
this.AutoScroll =false;
ResetAllCrlState(this);
}
}

publicvoid ResetAllCrlState(Control CrlContainer)//重新设定窗体中各控件的状态(在与原状态的对比中计算而来)
{
FormSizeChangedX = (double)this.Size.Width / (double)FormSizeWidth;
FormSizeChangedY = (double)this.Size.Height / (double)FormSizeHeight;

foreach (Control kCrl in CrlContainer.Controls)
{
/*string name = kCrl.Name.ToString();
MessageBox.Show(name);
MessageBox.Show(Wcounter.ToString());*/
if (kCrl.Controls.Count >0)
{
ResetAllCrlState(kCrl);
}
Point point =new Point();
point.X = (int)((int)CrlLocationX[Wcounter] * FormSizeChangedX);
point.Y = (int)((int)CrlLocationY[Wcounter] * FormSizeChangedY);
kCrl.Width = (int)((int)CrlSizeWidth[Wcounter] * FormSizeChangedX);
kCrl.Height = (int)((int)CrlSizeHeight[Wcounter] * FormSizeChangedY);
kCrl.Bounds =new Rectangle(point, kCrl.Size);
Wcounter++;
}
}
}
}
 
 
private float X;

private 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 )
{ string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
float a = Convert.ToSingle(mytag[0]) * newx;
con.Width = (int)a;
a=Convert.ToSingle(mytag[1]) * newy;
con.Height = (int)(a);
a=Convert.ToSingle(mytag[2]) * newx;
con.Left = (int)(a);
a=Convert.ToSingle(mytag[3]) * newy;
con.Top = (int)(a);
Single currentSize = Convert.ToSingle (mytag[4]) * Math.Min(newx,newy);
con .Font =new Font (con.Font .Name ,currentSize,con.Font .Style ,con.Font .Unit );
if(con.Controls .Count >0)
{
setControls (newx ,newy ,con );
}
} } void Form1_Resize(object sender, EventArgs e)
{
float newx = (this.Width )/ X;
float newy = this.Height / Y;
setControls(newx, newy, this);
this.Text = this.Width.ToString() +" "+ this.Height.ToString(); } 在Form_Load里面添加: this.Resize += new EventHandler(Form1_Resize); X = this.Width;
Y = this.Height; setTag (this);
Form1_Resize(new object(),new EventArgs());//x,y可在实例化时赋值,最后这句是新加的,在MDI时有用
分类: WinForm

C#当窗体大小改变时,窗体中的控件大小也随之改变的更多相关文章

  1. C# 清除当前窗体中TextBox控件中的内容

    //当有多个窗体时,对顶层的窗口进行操作,例如:我们开发具有录入功能的界面的时候,为了防止提交后的二次(重复)录入,希望点击提交按钮并提示成功后,界面的所有文本框内容能够自动清空.NET Framew ...

  2. 【机房系统知识小结点系列】之遍历窗体中的控件,判断Text是否为空?

    做机房系统时,几乎每个窗体中都会用到判断界面中的控件是否为空的情景.我们曾经是这样走来的: 第一版: 好处:对窗体界面中的Text等控件,逐一做判断,当用户输入某一项为空的时候,会议弹出框的形式,告诉 ...

  3. C# 向程序新建的窗体中添加控件,控件需要先实例化,然后用controls.add添加到新的窗体中去

    C# 向程序新建的窗体中添加控件,控件需要先实例化,然后用controls.add添加到新的窗体中去 Form settingForm = new Form(); setForm deviceSet ...

  4. 五种情况下会刷新控件状态(刷新所有子FWinControls的显示)——从DFM读取数据时、新增加子控件时、重新创建当前控件的句柄时、设置父控件时、显示状态被改变时

    五种情况下会刷新控件状态(刷新控件状态才能刷新所有子FWinControls的显示): 在TWinControls.PaintControls中,对所有FWinControls只是重绘了边框,而没有整 ...

  5. .net dataGridView当鼠标经过时当前行背景色变色;然后【给GridView增加单击行事件,并获取单击行的数据填充到页面中的控件中】

    1.首先在前台dataGridview属性中增加onRowDataBound属性事件 2.然后在后台Observing_RowDataBound事件中增加代码 protected void Obser ...

  6. js中使用控件名和数组下标方式获取控件的值时失败

    在做界面展示时涉及到表单行项目的增加和删除时,我们一帮都使用js的脚本实现表单行的增加和删除,那么在进行表单的提交的时我们会再页面上进行提交数据的初步校验,进行数据的初步校验时,就要动态获取控件的值. ...

  7. 母版页改变被嵌套的页面中的控件ID的解决方法

    使用过模板页的朋友都会很纳闷,怎么页面的用js通过getElementById(“id”):找不到对象.查看了页面源代码才发现,原来控件的ID变了,这是母版页导致的.因为母版页怕母版页本身页面中的控件 ...

  8. Android 中常见控件的介绍和使用

    1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...

  9. C# Winform 界面中各控件随着窗口大小变化

    在做一项工程中,由于不确定目标平台的分辨率,而正常使用要求铺满整个屏幕,所以界面中的各个控件必须能够适应窗口的变化. 首先想到的就是控件的百分比布局,但是再尝试写了几个控件的Location和Size ...

  10. MFC中ComboBox控件用法

    MFC ComboBox 一.入门篇 ComboBox (组合框)控件很简单,可以节省空间.从用户角度来看,这个控件是由一个文本输入控件和一个下拉菜单组成的.用户可以从一个预先定义的列表里选择一个选项 ...

随机推荐

  1. ACE下载地址

    https://download.dre.vanderbilt.edu/previous_versions/ 在某n中找了大半天愣是没人贴出来

  2. window 0x00007b无法正常启动解决方法

    参考链接: https://pcedu.pconline.com.cn/1570/15702605.html directx修复工具下载: https://www.onlinedown.net/sof ...

  3. supervisor 安装及基本使用

    1.安装 yum install supervisor 2.检查版本 supervisord --version 3.设为开机启动 systemctl enable supervisord.servi ...

  4. RabbitMQ的使用介绍

    一.RabbitMQ是什么 RabbitMQ是一种常用的消息中间件,是基于AMQP协议,采用erlang语言开发的面向消息服务的中间件,是一个独立的系统应用程序,可以管理服务器计算资源和网络通信.一般 ...

  5. docker、Containerd ctr、crictl 区别

    简述 作为接替 Docker 运行时的 Containerd 在早在 Kubernetes1.7 时就能直接与 Kubelet 集成使用,只是大部分时候我们因熟悉 Docker,在部署集群时采用了默认 ...

  6. java猜数小游戏

    问题: 程序自动生成一个1~100之间的随机数字,使用程序猜出这个数字是多少: Random可以随机生成一个随机数 使用方法: 导包 创建对象 Random r = new Random(); //只 ...

  7. 主页面调取iframe子页面的子页面数据

    iframe = this.iframe.contentWindow; var rowsData = iframe.$("#sonList2")[0].contentWindow. ...

  8. 对于AF、RI、Safety from rep exposure、spec的归纳总结

    每次写实验时,在写代码之前都要进行AFRISafety from rep exposure spec的编写,过程十分繁琐,但是也非常有用.根据他们写代码,不仅可以找到切入点,而且思路更加清晰了,避免了 ...

  9. 简单的自动清理TIM/QQ聊天垃圾文件方案

    我平时喜欢在后台挂着Tim,时间一长,我发现数据文件夹会越来越大,即使我没有看过这些消息(多为群聊消息),为了不再惦记清理垃圾文件,我整理了以下方案,可以每天清理一次7天前的文件. 1. 在磁盘任意位 ...

  10. 快速确定execl 列数

    1.在最后的列输入公式=COLUMN(). 2.按回车