2013-06-25 22 views
3

我想顯示WPF .net 4.5中的顏色和字體對話框,我該怎麼做? 請幫助任何人。如何使用WPF打開顏色和字體對話框?

Thnx in Advanced!

+0

在這裏找到這些表單.http://blogs.msdn.com/b/wpfsdk/archive/2006/10/26/uncommon-dialogs--font-chooser-and-color-picker-dialogs.aspx – ridoy

回答

1

您可以使用System.Windows.Forms的類,使用它們沒有任何問題。你可能需要將值轉換爲特定於WPF的。

或者,您可以實施自己的對話框或使用第三方控件,請參閱Free font and color chooser for WPF?

5

最好的開箱即用的解決方案是使用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雖然。