You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# WinForms字体查看器问题:ComboBox无法修改字体样式与大小

解决C# WinForms字体查看器的样式/大小不更新问题

嘿,我来帮你搞定这个字体查看器的问题!你遇到的核心问题是选择字体或字号后,PictureBox里的内容没跟着变化,另外代码里还有几个小bug,咱们一个个修正:

问题分析与修复点

  • 缺少重绘触发逻辑:当你在ComboBox里选新字体/字号时,程序没通知PictureBox重新绘制内容,所以它一直显示初始状态。咱们需要给两个ComboBox添加SelectedIndexChanged事件,每次选中变化时让PictureBox重绘。
  • 字号循环的致命错误:你写的for( int s = 1; s <= 50; size++ )完全搞错了循环变量——你在递增ComboBox控件size,而不是变量s,这会导致循环根本跑不起来,还会搞坏控件状态。要改成用s来循环。
  • 空值与默认选中处理:程序启动时ComboBox没有选中项,直接用fontsm.Text和size.Text会导致创建Font时出错,所以得设置默认选中项,同时在绘制前做有效性判断。

修正后的完整代码

namespace CodeImage35
{
    class Visualizer : Form
    {
        Label choose;
        PictureBox picbox;
        ComboBox fontsm;
        ComboBox size;

        public Visualizer()
        {
            this.Width = 600;
            this.Height = 300;
            this.Text = "Visualizer of fonts";

            choose = new Label();
            choose.Size = new Size(100, 20);
            choose.Location = new Point(20, 20);
            choose.Text = "Choose Font:";

            picbox = new PictureBox();
            picbox.Size = new Size(300, 200);
            picbox.Location = new Point(100, 150);
            // 设置白色背景,避免黑色文字在透明背景上不可见
            picbox.BackColor = Color.White;

            fontsm = new ComboBox();
            fontsm.Location = new Point(110, 20);
            fontsm.Size = new Size(350, 20);
            // 字体选择变化时触发重绘
            fontsm.SelectedIndexChanged += (s, e) => picbox.Invalidate();

            size = new ComboBox();
            size.Location = new Point(480, 20);
            size.Size = new Size(100, 20);
            // 字号选择变化时触发重绘
            size.SelectedIndexChanged += (s, e) => picbox.Invalidate();

            // 加载系统安装的字体到ComboBox
            System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
            foreach (FontFamily style in fonts.Families)
            {
                fontsm.Items.Add(style.Name);
            }

            // 正确添加1到50的字号选项
            for (int s = 1; s <= 50; s++)
            {
                size.Items.Add(s);
            }

            // 设置默认选中项,避免初始为空导致报错
            if (fontsm.Items.Count > 0)
                fontsm.SelectedIndex = 0;
            if (size.Items.Count > 10)
                size.SelectedIndex = 10; // 默认选中11号字(索引从0开始)

            picbox.Paint += write_in_PictureBox;

            Controls.Add(fontsm);
            Controls.Add(choose);
            Controls.Add(size);
            Controls.Add(picbox);
        }

        void write_in_PictureBox(object sender, PaintEventArgs e)
        {
            // 先校验字体和字号的有效性
            if (string.IsNullOrEmpty(fontsm.Text) || !int.TryParse(size.Text, out int fontSize) || fontSize <= 0)
                return;

            // 使用using包裹Font对象,确保资源正确释放
            using (Font drawFont = new Font(fontsm.Text, fontSize))
            {
                e.Graphics.DrawString(fontsm.Text, drawFont, Brushes.Black, new Point(10, 10));
            }
        }
    }

    static class MainClass
    {
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Visualizer screen1 = new Visualizer();
            Application.Run(screen1);
        }
    }
}

额外优化说明

  • 给PictureBox设置了白色背景,解决了黑色文字在默认透明背景下可能看不见的问题;
  • 用using语句管理Font对象,确保GDI+资源能被正确释放;
  • 添加了Application.EnableVisualStyles()和Application.SetCompatibleTextRenderingDefault(false),让WinForms界面样式更贴合系统主题。

内容的提问来源于stack exchange,提问作者Bryan_L

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 12:53:09