在进行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 文字随着窗体大小变化的更多相关文章

  1. MFC 无边框窗体实现用鼠标拖动窗体边缘实现窗体大小变化

    无边框窗体如何实现用鼠标拖动窗体边缘实现窗体大小变动呢?下面介绍一种方法,通过以下几个步骤即可实现: 1.实现WM_NCHITTEST消息,实现四条边框的模拟 2.实现WM_NCLBUTTONDOWN ...

  2. MFC窗体大小变化

    对话框的大小变化后,假若对话框上的控件大小不变化,看起来会比较难看.下面就介绍怎么让对话框上的控件随着对话框的大小的变化自动调整. 首先明确的是Windows有一个WM_SIZE消息响应函数,这个函数 ...

  3. C#设置窗体中的窗体随主窗体大小变化而变化

    form2 f=new form2(); f.Size=this.Size; f.Location=this.Location; f.showdialog(); 作者:耑新新,发布于  博客园 转载请 ...

  4. WinFrom中使用WPF的窗体

    步骤 1.添加WindowsFormsIntegration.dll .System.Windows.Forms.和System.Xaml,PresentationCore.PresentationF ...

  5. winform 窗体大小变化时,如何设置使控件一起按照比例变大

    public Form() { InitializeComponent(); + ; float[] factor = new float[count]; ; factor[i++] = Size.W ...

  6. C#控件大小随窗体大小等比例变化

    相信很多博友在开发初次接触学习C# winForm时,当窗体大小变化时,窗体内的控件并没有随着窗体的变化而变化,最近因为一个项目工程的原因,也需要解决这个问题.通过查阅和学习,这个问题得到了解决,或许 ...

  7. WinForm中控件位置不随窗体大小的变化而改变

    背景:在窗体开发中使控件的位置不随窗体的大小改变而变化的问题?这是一个同事在开发时遇到的问题,在思考试错之后,写出如下代码. 声明成员: Point m_InitLocation; Size m_In ...

  8. iOS开发小技巧 - label中的文字添加点击事件

    Label中的文字添加点击事件 GitHub地址:https://github.com/lyb5834/YBAttributeTextTapAction 以前老师讲过类似的功能,自己懒得回头看了,找了 ...

  9. [Xcode 实际操作]九、实用进阶-(26)对Storyboard(故事版)中的文字标签(Label)进行本地化处理

    目录:[Swift]Xcode实际操作 对Storyboard(故事版)中的文字标签(Label)进行本地化处理. 点击项目名称[DemoApp]进入项目信息面板. [Build Setting]-& ...

随机推荐

  1. leetcode 492-543 easy

    492. Construct the Rectangle Input: 4 Output: [2, 2] Explanation: The target area is 4, and all the ...

  2. Boost.Asio基础

    http://www.voidcn.com/article/p-exkmmuyn-po.html http://www.voidcn.com/article/p-xnxiwkrf-po.html ht ...

  3. 洛谷 P3742 umi的函数【构造】

    题目背景(https://www.luogu.org/problemnew/show/P3742) umi 找到了一个神秘的函数 f. 题目描述 这个函数接受两个字符串 s1,s2.这些字符串只能由小 ...

  4. NIOP模拟17.10.13

    太水,简述一下题意 T1 让你计算一个形如Σai * bi^ki 快速幂即可 #include <iostream> #include <cstdio> #include &l ...

  5. 【洛谷】P1888 三角函数

    P1888 三角函数 题目描述 输入一组勾股数a,b,c(a≠b≠c),用分数格式输出其较小锐角的正弦值.(要求约分.) 输入输出格式 输入格式: 一行,包含三个数,即勾股数a,b,c(无大小顺序). ...

  6. webpack学习之——npm的安装依赖情况

    这几天一直在研究webpack模块话打包工具,在网上的资源还是蛮丰富的,现在总结下这块的内容,需要好好的研究下,如果有问题,还请指正. 先是第一个为问题,就是npm-install --save 和n ...

  7. 全球城市群Megalopolis

    Megalopolis From Wikipedia, the free encyclopedia   (Redirected from Megalopolis (city type))   &quo ...

  8. WPF ScrollViewer嵌套Listbox无法滚动

    最近在做项目的时候,发现listBoxzi自带的垂直滚动条有问题,经常在Add(item)的时候下面会多出一些空白的部分,而且滚动条的长度也是无规则的,一会大一会小,而且无法控制横竖滚动条的显隐藏,并 ...

  9. PHP队列类

    /** * Created by PhpStorm. * User: LAMP-Q哥 * Date: 2017/8/3 * Time: 12:58 */ class Queue { private $ ...

  10. 使MySQL支持emoji

    1. 修改数据库的字符集 和 排序规则为: ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode ...