Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open Color and Font Dialog box using WPF?

Tags:

dialog

wpf

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 in Advanced!

like image 735
Ben Reitman Avatar asked Jun 25 '13 09:06

Ben Reitman


People also ask

Which dialog box can find font size and Colour?

You can access this dialog box by clicking Tools > Options, and then selecting Environment > Fonts and Colors.

What is the font dialogue box?

The Font dialog box lets the user choose attributes for a logical font, such as font family and associated font style, point size, effects (underline, strikeout, and text color), and a script (or character set).

What is FontDialog C#?

A FontDialog control in WinForms is used to select a font from available fonts installed on a system. A typical Font Dialog looks like Figure 1 where you can see there is a list of fonts, styles, size and other options.


2 Answers

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

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

It does not support alpha though.

like image 114
Dave_cz Avatar answered Nov 15 '22 11:11

Dave_cz


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.

Alternatively, you can implement your own dialogs or use third-party controls, see Free font and color chooser for WPF?.

like image 34
Athari Avatar answered Nov 15 '22 12:11

Athari