gpt4 book ai didi

wpf - 如何使用 WPF 打开颜色和字体对话框?

转载 作者:行者123 更新时间:2023-12-04 23:03:28 25 4
gpt4 key购买 nike

我想在 WPF .net 4.5 中显示颜色和字体对话框,我该怎么做?
请帮助我任何人。

Thnx 进阶!

最佳答案

最好的开箱即用解决方案是使用 FontDialog表格 System.Windows.Forms程序集,但您必须转换它的输出才能将其应用于 WPF 元素。

FontDialog fd = new FontDialog();
var result = fd.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
Debug.WriteLine(fd.Font);

tbFonttest.FontFamily = new FontFamily(fd.Font.Name);
tbFonttest.FontSize = fd.Font.Size * 96.0 / 72.0;
tbFonttest.FontWeight = fd.Font.Bold ? FontWeights.Bold : FontWeights.Regular;
tbFonttest.FontStyle = fd.Font.Italic ? FontStyles.Italic : FontStyles.Normal;

TextDecorationCollection tdc = new TextDecorationCollection();
if (fd.Font.Underline) tdc.Add(TextDecorations.Underline);
if (fd.Font.Strikeout) tdc.Add(TextDecorations.Strikethrough);
tbFonttest.TextDecorations = tdc;
}

请注意,winforms 对话框不支持许多 WPF 字体属性,例如额外的粗体字体。

颜色对话框要容易得多:
ColorDialog cd = new ColorDialog();
var result = cd.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
tbFonttest.Foreground = new SolidColorBrush(Color.FromArgb(cd.Color.A, cd.Color.R, cd.Color.G, cd.Color.B));
}

虽然它不支持alpha。

关于wpf - 如何使用 WPF 打开颜色和字体对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17294236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com