本文介绍了如何使用WPF打开“颜色和字体”对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to show the color and font dialog box in WPF .net 4.5, how to can I do?Please help me anybody.

Thnx(高级)!

推荐答案

最好的现成解决方案是使用 FontDialog 表单 System.Windows.Forms 程序集,但是您将

The best out of the box solution is using FontDialog form System.Windows.Forms assembly, but you will have to convert it's output to apply it to WPF elements.

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字体属性,例如超粗字体

Notice that winforms dialog does not support many of WPF font properties like extra bold fonts.

颜色对话框要容易得多:

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));
}

不过它不支持alpha。

It does not support alpha though.

这篇关于如何使用WPF打开“颜色和字体”对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 08:42