かなりの時間を費やした後、この関数を検索します。
誰かが私にそうするための最良の方法を教えてくれたらいいのにと思いました。ウィンドウフォームコントロール(ラベル/ボタン)のサイズに基づいてフォントサイズを動的に調整できる機能はありますか?
これは私がオンラインで調査した後に持っているものですが、残念ながら、これらのコードは、コントロールのサイズが変更される実行時に多くの例外を与えます。
public void textAdjustment()
{
try
{
while (this.label.Width < System.Windows.Forms.TextRenderer.MeasureText(this.label.Text,
new Font(this.label.Font.FontFamily, this.label.Font.Size, this.label.Font.Style)).Width)
{
this.label.Font = new Font(this.label.Font.FontFamily, this.label.Font.Size - 1.0f, this.label.Font.Style);
}
if (this.label.Width > System.Windows.Forms.TextRenderer.MeasureText(this.label.Text, new Font(this.label.Font.FontFamily, this.label.Font.Size, this.label.Font.Style)).Width)
{
this.label.Font = new Font(this.label.Font.FontFamily, this.label.Font.Size + 0.1f, this.tableLabel.Font.Style);
}
if (this.label.Height < System.Windows.Forms.TextRenderer.MeasureText(this.label.Text, new Font(this.label.Font.FontFamily, this.label.Font.Size, this.label.Font.Style)).Height)
{
this.label.Font = new Font(this.label.Font.FontFamily, this.label.Font.Size - 0.6f, this.label.Font.Style);
}
}
catch (Exception e)
{
this.label.Font = Control.DefaultFont;
}
}
私はこれに取り組む方法が正しいとは思いませんが、実行時にコントロールサイズを増減しても、フォントサイズベースを調整する方法を知っている人はいますか?個人的には、この投稿は他の人にも参考になるとても良い投稿になると思います。