如何使用 WPF 打开颜色和字体对话框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17294236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open Color and Font Dialog box using WPF?
提问by Ben Reitman
I want to show the color and font dialog box in WPF .net 4.5, how to can I do? Please help me anybody.
我想在 WPF .net 4.5 中显示颜色和字体对话框,我该怎么做?请帮助我任何人。
Thnx in Advanced!
Thnx 进阶!
回答by Dave_cz
The best out of the box solution is using FontDialogform System.Windows.Formsassembly, but you will have to convert it's output to apply it to WPF elements.
最好的开箱即用解决方案是使用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;
}
Notice that winforms dialog does not support many of WPF font properties like extra bold fonts.
请注意,winforms 对话框不支持许多 WPF 字体属性,例如额外的粗体字体。
Color dialog is much easier:
颜色对话框要容易得多:
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));
}
It does not support alpha though.
虽然它不支持 alpha。
回答by Athari
You can use classes from System.Windows.Forms, there is nothing wrong with using them. You'll probably need to convert values to WPF-specific though.
您可以使用来自 的类System.Windows.Forms,使用它们没有任何问题。不过,您可能需要将值转换为 WPF 特定的值。
Alternatively, you can implement your own dialogs or use third-party controls, see Free font and color chooser for WPF?.
或者,您可以实现自己的对话框或使用第三方控件,请参阅WPF 的免费字体和颜色选择器?.

