winfrom 中 label 文字随着窗体大小变化
在进行winfrom 开发过程中,窗体中的文字需要随着窗体大小变化,否则会影响窗体的美观和客户的体验。
之前曾经试过几种方法效果都不满意,例如将label的Dock 属性设置为fill.这样的设置对解决问题没有帮助,文字并没有随着窗体变化。
也尝试获取窗体大小让label文字的大小按比例随着窗体变化,这种方法倒是能实现效果,但会影响窗体原始对label 文字大小的设置。
最后找到了下面的方法很好的解决了这个问题,现在分享出来。
首先用VS创建FORM工程,然后再窗体中添加label,按照下面的代码执行。
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
{
Single X;
Single Y; public Form1()
{
InitializeComponent();
this.Resize += new EventHandler(Form1_Resize); X = this.Width;
Y = this.Height;
// y = this.statusStrip1.Height;
setTag(this);
}
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 > )
setTag(con);
}
}
private void setControls(float newx, float newy, Control cons)
{
foreach (Control con in cons.Controls)
{
Single aa= label1.Font.Size;
string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
float a = Convert.ToSingle(mytag[]) * newx;
con.Width = (int)a;
a = Convert.ToSingle(mytag[]) * newy;
con.Height = (int)(a);
a = Convert.ToSingle(mytag[]) * newx;
con.Left = (int)(a);
a = Convert.ToSingle(mytag[]) * newy;
con.Top = (int)(a);
Single currentSize = Convert.ToSingle(mytag[]) * newy;
con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
if (con.Controls.Count > )
{
setControls(newx, newy, con);
}
} } void Form1_Resize(object sender, EventArgs e)
{
// throw new Exception("The method or operation is not implemented.");
float newx = (this.Width) / X;
// float newy = (this.Height - this.statusStrip1.Height) / (Y - y);
float newy = this.Height / Y;
setControls(newx, newy, this);
//this.Text = this.Width.ToString() +" "+ this.Height.ToString(); } }
}
效果不错:


winfrom 中 label 文字随着窗体大小变化的更多相关文章
- MFC 无边框窗体实现用鼠标拖动窗体边缘实现窗体大小变化
无边框窗体如何实现用鼠标拖动窗体边缘实现窗体大小变动呢?下面介绍一种方法,通过以下几个步骤即可实现: 1.实现WM_NCHITTEST消息,实现四条边框的模拟 2.实现WM_NCLBUTTONDOWN ...
- MFC窗体大小变化
对话框的大小变化后,假若对话框上的控件大小不变化,看起来会比较难看.下面就介绍怎么让对话框上的控件随着对话框的大小的变化自动调整. 首先明确的是Windows有一个WM_SIZE消息响应函数,这个函数 ...
- C#设置窗体中的窗体随主窗体大小变化而变化
form2 f=new form2(); f.Size=this.Size; f.Location=this.Location; f.showdialog(); 作者:耑新新,发布于 博客园 转载请 ...
- WinFrom中使用WPF的窗体
步骤 1.添加WindowsFormsIntegration.dll .System.Windows.Forms.和System.Xaml,PresentationCore.PresentationF ...
- winform 窗体大小变化时,如何设置使控件一起按照比例变大
public Form() { InitializeComponent(); + ; float[] factor = new float[count]; ; factor[i++] = Size.W ...
- C#控件大小随窗体大小等比例变化
相信很多博友在开发初次接触学习C# winForm时,当窗体大小变化时,窗体内的控件并没有随着窗体的变化而变化,最近因为一个项目工程的原因,也需要解决这个问题.通过查阅和学习,这个问题得到了解决,或许 ...
- WinForm中控件位置不随窗体大小的变化而改变
背景:在窗体开发中使控件的位置不随窗体的大小改变而变化的问题?这是一个同事在开发时遇到的问题,在思考试错之后,写出如下代码. 声明成员: Point m_InitLocation; Size m_In ...
- iOS开发小技巧 - label中的文字添加点击事件
Label中的文字添加点击事件 GitHub地址:https://github.com/lyb5834/YBAttributeTextTapAction 以前老师讲过类似的功能,自己懒得回头看了,找了 ...
- [Xcode 实际操作]九、实用进阶-(26)对Storyboard(故事版)中的文字标签(Label)进行本地化处理
目录:[Swift]Xcode实际操作 对Storyboard(故事版)中的文字标签(Label)进行本地化处理. 点击项目名称[DemoApp]进入项目信息面板. [Build Setting]-& ...
随机推荐
- 原生js增加,移除类名
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HDU 4584
//也是简单题,因为n太小,故暴力之! #include<stdio.h> #include<math.h> #include<string.h> #define ...
- poj 2318 TOYS(计算几何 点与线段的关系)
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12015 Accepted: 5792 Description ...
- HTML 语法简要总结
HTML基本语法 认识网页 网页主要由文字.图像和超链接等元素构成.当然,除了这些元素,网页中还可以包含音频.视频以及Flash等. 常见浏览器内核介绍 浏览器是网页运行的平台,常用的浏览器有IE.火 ...
- html文件中script标签放在哪里?
- jQuery控制导航条样式
原理:点击当前元素时,当前元素添加(样式类),父辈的兄弟姐妹的孩子('a')去掉此样式类. 代码如下: /*次要导航*/ $(".subnav li a").click(funct ...
- javax.el.PropertyNotFoundException: Property 'studentAge' not found on type com.hs.model.StudentModel
mi明明已经把这个字段重构了,为啥还这样提示呢?整个工程全部 查找也找不到这个字段了啊
- <a>标签操作
1.点击后onclick事件失效,变灰,不可用 onclick(this); //事件传递this对象 function viewMm(obj) { $(obj).removeAttr("o ...
- wordpress主题之后台菜单编辑,小工具
1一:菜单编辑 在functions.php 文件加入 if (function_exists('register_nav_menus')) { register_nav_menus(array( / ...
- Node.js概述1
为什么我们要学习Node.js? 认为: Node.js就学习一周,时间比较短,不重要 将来工作我后端又不用Node.js做,我们又java/python/php/c,为什么要在意它 Node.js接 ...