Wpf, FontDialog使用及設定
//設定從Wpf中設定FontDialog的屬性FontDialog fd = new FontDialog();
fd.ShowColor = true;
fd.ShowEffects = true;
fd.FontMustExist = true;
System.Drawing.FontStyle style = new System.Drawing.FontStyle();
if (TBx_writeinfo.FontWeight.ToString() == "Bold")
{
if (TBx_writeinfo.FontStyle.ToString() == "Italic")
{
try
{
style = (System.Drawing.FontStyle)new FontStyleConverter().ConvertFromString("Bold, Italic");
}
catch (Exception)
{
////Console.WriteLine("ex:" + ex.Message);
}
}
else
{
////Console.WriteLine("not italic");
style = System.Drawing.FontStyle.Bold;
}
}
else
{
////Console.WriteLine("not bold");
if (TBx_writeinfo.FontStyle.ToString() == "Italic")
{
////Console.WriteLine("italic");
style = System.Drawing.FontStyle.Italic;
}
else
{
////Console.WriteLine("not italic");
}
}
fd.Color = (System.Drawing.Color)new System.Drawing.ColorConverter().ConvertFromString(TBx_writeinfo.Foreground.ToString());
//TODO:取得刪除線
Console.WriteLine("MyTextDecoration:" + MyTextDecoration);
if (MyTextDecoration != null)
{
string[] temp = MyTextDecoration.Split('-');
if (temp != null)
{
for (int i = 0; i < temp.Length; i++)
{
if (!String.IsNullOrEmpty(temp[i]))
{
if (temp[i] == "Strikethrough")
{
style |= System.Drawing.FontStyle.Strikeout;
}
else if (temp[i] == "Underline")
{
style |= System.Drawing.FontStyle.Underline;
}
}
}
}
}
//TODO:取得底線
fd.Font = new System.Drawing.Font(TBx_writeinfo.FontFamily.ToString(), (float)TBx_writeinfo.FontSize, style);
//從FontDialog屬性中,設定Wpf元件屬性(textblock)
DialogResult dr = fd.ShowDialog();
//Console.WriteLine("dr:" + dr.ToString());
if (dr.ToString() == "OK")
{
////Console.WriteLine("ok");
////Console.WriteLine("family:" + fd.Font.FontFamily);
////Console.WriteLine("style:" + fd.Font.Style);
////Console.WriteLine("size:" + fd.Font.Size);
////Console.WriteLine("Strikeout:" + fd.Font.Strikeout);
////Console.WriteLine("underline:" + fd.Font.Underline);
////Console.WriteLine("bold:" + fd.Font.Bold);
////Console.WriteLine("italic:" + fd.Font.Italic);
////Console.WriteLine("color:" + fd.Color.Name);
////Console.WriteLine("charset:" + fd.Font.GdiCharSet);
//TODO:設定變更的字型樣式
TBx_writeinfo.FontFamily = new FontFamily(fd.Font.FontFamily.Name);
MyFontFamily = new FontFamily(fd.Font.FontFamily.Name);
TBx_writeinfo.FontSize = fd.Font.Size;
MyFontSize = fd.Font.Size;
//TBx_writeinfo.FontWeight = fd.Font.Bold.
////Console.WriteLine("fontfamily:" + TBx_writeinfo.FontFamily);
////Console.WriteLine("style:" + TBx_writeinfo.FontStyle);
////Console.WriteLine("fontweight:" + TBx_writeinfo.FontWeight);
if (fd.Font.Bold)
{
TBx_writeinfo.FontWeight = FontWeights.Bold;
MyFontWeight = FontWeights.Bold;
}
else
{
TBx_writeinfo.FontWeight = FontWeights.Normal;
MyFontWeight = FontWeights.Normal;
}
if (fd.Font.Italic)
{
TBx_writeinfo.FontStyle = FontStyles.Italic;
MyFontStyle = FontStyles.Italic;
}
else
{
TBx_writeinfo.FontStyle = FontStyles.Normal;
MyFontStyle = FontStyles.Normal;
}
string sendTextDecoration = "";
MyTextDecoration = null;
TBx_writeinfo.TextDecorations = null;
if (fd.Font.Strikeout == true && fd.Font.Underline == true)
{
//設定underline
TextDecorationCollection myCollection = new TextDecorationCollection();
TextDecoration myUnderline = new TextDecoration();
myUnderline.Location = TextDecorationLocation.Underline;
// Set the solid color brush.
myUnderline.Pen = new Pen((Brush)new BrushConverter().ConvertFromString(fd.Color.Name), 1);
myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended;
// Set the underline decoration to the text block.
myCollection.Add(myUnderline);
//設定Strikethrough;
//TextDecorationCollection myCollection = new TextDecorationCollection();
TextDecoration myStrikeThrough = new TextDecoration();
myStrikeThrough.Location = TextDecorationLocation.Strikethrough;
// Set the solid color brush.
myStrikeThrough.Pen = new Pen((Brush)new BrushConverter().ConvertFromString(fd.Color.Name), 1);
myStrikeThrough.PenThicknessUnit = TextDecorationUnit.FontRecommended;
// Set the underline decoration to the text block.
myCollection.Add(myStrikeThrough);
//TBx_writeinfo.TextDecorations = TextDecorations.Underline || TextDecorations.Strikethrough;
TBx_writeinfo.TextDecorations = myCollection;
MyTextDecoration = "Strikethrough-Underline";
sendTextDecoration = "Strikethrough-Underline";
}
else
{
if (fd.Font.Strikeout)
{
TBx_writeinfo.TextDecorations = TextDecorations.Strikethrough;
MyTextDecoration = "Strikethrough";
sendTextDecoration = "Strikethrough";
}
if (fd.Font.Underline)
{
TBx_writeinfo.TextDecorations = TextDecorations.Underline;
MyTextDecoration = "Underline";
sendTextDecoration = "Underline";
}
}
TBx_writeinfo.Foreground = (Brush)new BrushConverter().ConvertFromString(fd.Color.Name);
MyColor = fd.Color.Name;