c# - 我可以使用 Combobox 在字体查看器中更改大小和样式吗?
问题描述
#大家好,我在 C# 中遇到了 Forms 的问题,我想制作一个字体查看器,实际上我想制作两个 Combobox 来更改“图形”并以其样式和特定的字体显示相同的字体名称第二个组合框的大小。主要问题是样式和大小不会改变。告诉我任何错误都会很酷:)这是我的代码。#
namespace CodeImage35
{
class Visualizer : Form
{
Label choose;
PictureBox picbox; //this is for the text
Graphics textm; //the text in PictureBox
ComboBox fontsm; //font of the text
ComboBox size; //size of the font
String fonts; //I used this for get the fonts,maybe never is used
public Visualizer() //Here is the UI
{
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);
fontsm = new ComboBox();
fontsm.Location = new Point(110, 20);
fontsm.Size = new Size(350, 20);
size = new ComboBox();
size.Location = new Point(480, 20);
size.Size = new Size(100, 20);
System.Drawing.Text.InstalledFontCollection fonts = new InstalledFontCollection(); //this
//is for the fonts
foreach ( FontFamily style in fonts.Families )//I add them in the combobox here
{
fontsm.Items.Add(style.Name);
}
for( int s = 1; s <= 50; size++ )
{
size.Items.Add(s); //I add here the sizes from 1 to 50
}
picbox.Paint += new PaintEventHandler(write_in_PictureBox);
Controls.Add(fontsm);
Controls.Add(choose);
Controls.Add(size);
Controls.Add(picbox);
}
void write_in_PictureBox(object sender, PaintEventArgs e)
{
String text2 = fontsm.Text;
textm = e.Graphics;
int x = 0;
Int32.TryParse(size.Text, out x); //I tried with this function to make string to int for
//the parameter of "DrawString
textm.DrawString( text2 , new Font( text2, x ), Brushes.Black, new Point(10, 10) );
}
}
static class MainClass
{
static void Main()
{
Visualizer screen1 = new Visualizer();
Application.Run(screen1);
}
}
}
解决方案
要开始修复此编译器错误:
for( int s = 1; s <= 50; size++ )
{
size.Items.Add(s); //I add here the sizes from 1 to 50
}
要增加的正确变量是s。你也避免从 1 开始,这个值真的太小而无法渲染字体并欣赏结果。从6或8开始应该没问题。除此之外...
为了绘制一个字符串,基于两个组合框的输入,您必须处理SelectedValueChanged事件(或 SelectedIndexChanged)。当用户更改(选择)组合框中的值时,就会发生这些事件。
相反,PaintEventHandler 在绘制控件时发生。请参阅绘画事件。
按照您的示例,您可以将 SelectedValueChanged 事件附加到绘制字符串的方法,就像您在write_in_PictureBox方法中所做的那样。
//put this at the end of the constructor method.
//The event will be triggered on both combobox, allowing the user to see the result of it's selection whatever combobox is edited
fontsm.SelectedValueChanged += (sender, args) => DrawFont();
size.SelectedValueChanged += (sender, args) => DrawFont();
//that's just a method to handle the event
private void DrawFont()
{
using (var g = picbox.CreateGraphics()) //remember to always dispose a disposable resource
{
//clear the area before draw the new string
g.Clear(DefaultBackColor);
if (!Int32.TryParse(size.Text, out int x)) x = 12; //if no value is selected for size combobox, use 12 as default
string text = fontsm.Text;
g.DrawString(text, new Font(text, x), Brushes.Black, new Point(10, 10));
}
}
顺便说一句...如果您只想显示字体预览...您只需放置一个标签并更改它的文本和字体属性会更容易:)。
推荐阅读
- sql - 在 VBA Access 中向 Excel 显示查询
- python - 重新加载其他模块中导入的子模块
- c - 如何在 OpenMP 中的线程之间分配数据?
- swift - AVAudioPlayer 不播放音频文件 - swift
- office365 - 根据 Infopath 中的下拉列表值加载查找字段
- java - 使用 for 循环对字符串进行排序
- powershell - 从 CSV 文件创建变量
- ruby-on-rails - ActionCable broadcast_to 未到达用户
- c# - 使用 RX.Net 的移动键盘“Shift”切换类似功能
- xcode - 位置模拟在 Xcode 10 和 iOS 12 中不起作用