C# 字体大小的相关问题

设置字体大小无法这么写,

button1.Font.Size = 20;

这个是只读属性;

把字体大小改为16,

button2.Font = new Font(button2.Font.Name, 16);

程序运行的时候先看一下窗体和控件的默认字体尺寸,都是9;然后点button4把button2的字体调为16,结果如下;

然后点button3,把窗体的字体大小改为16,再输出窗体和控件的字体大小,结果如下;

控件的字体也会同时变为16;如果把窗体的字体调大,窗体尺寸会变大;

cs 复制代码
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 fontdemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = this.Font.Size.ToString();
            textBox2.Text = button1.Font.Size.ToString();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            button2.Font = new Font(button2.Font.Name, 16);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Font = new Font(this.Font.Name, 16);

            textBox1.Text = this.Font.Size.ToString();
            textBox2.Text = button1.Font.Size.ToString();
        }
    }
}
相关推荐
Geek-Chow11 分钟前
Mobile App Certificate Pinning: Underlying Principle and a Swift Example
开发语言·ios·swift·安全架构
阿豪只会阿巴20 分钟前
两小时快速入门 FastAPI--第一回
开发语言·python·fastapi
你怎么知道我是队长35 分钟前
JavaScript 事件介绍
开发语言·前端·javascript
雪靡39 分钟前
WPF PerMonitorV2 下的跨窗口问题
c#·wpf
CodeStats40 分钟前
【Linux IO】从文件描述符到硬件中断:Linux IO 系统底层完全拆解
java·linux·开发语言·io·socket
苍狼唤43 分钟前
SQL+C# 增删改查(练习)
数据库·sql·c#
着迷不白1 小时前
Linux系统故障修复、日志管理与安全加固实战指南
开发语言·php
贺国亚1 小时前
A2A协议与Agent互操作-Task生命周期
开发语言·qt
炸薯条!1 小时前
从零开始学C++(4) --类和对象
开发语言·c++·算法
渡我白衣1 小时前
打印宏与socket模块设计
java·linux·开发语言·c++·ide·人工智能·eclipse