[System.Runtime.Serialization.DataContract]
class WindowFont
{
/// <summary>
/// WPF에서 Font를 조금 더 자유롭게 사용할 수 있도록 하기 위해서 만들었습니다.
/// 2016.12.29 by YoungWoo
/// blog: gigong.tistory.com
/// email: gigong@tistory.com
/// 계속해서 배워가는 중이라 미흡한 점이 많습니다.
/// </summary>
#region private member
///
[System.Runtime.Serialization.DataMember]
string fontFamily; // save fontfamily to string
[System.Runtime.Serialization.DataMember]
int fontWeight, fontStretch, fontStyle; // save weight, stretch, style to integer
// weight, stretch can convert to integer using ToOpenTypeWeight
// style is matched to integer // 0 : normal, 1 : oblique, 2 : italic
[System.Runtime.Serialization.DataMember]
double fontSize; // save fontsize to double
[System.Runtime.Serialization.DataMember]
Color color; // save Color
// font's foreground
#endregion
#region property
///<summary>
/// WindowFont To/From Font (System.Drawing.Font, in WinForms) Without Underline
/// </summary>
public System.Drawing.Font Font
{
get
{
System.Drawing.FontStyle fs = System.Drawing.FontStyle.Regular;
if (FontWeight.ToOpenTypeWeight() >= FontWeights.Bold.ToOpenTypeWeight())
fs |= System.Drawing.FontStyle.Bold;
if (FontStyle == FontStyles.Italic)
fs |= System.Drawing.FontStyle.Italic;
return new System.Drawing.Font(fontFamily, (float)fontSize, fs);
}
set
{
fontFamily = value.FontFamily.Name;
fontStretch = FontStretches.Normal.ToOpenTypeStretch(); // WinForms's Font does't have Stretch
fontSize = value.Size;
if (value.Bold == true)
fontWeight = FontWeights.Bold.ToOpenTypeWeight();
else
fontWeight = FontWeights.Normal.ToOpenTypeWeight();
if (value.Italic == true)
FontStyle = FontStyles.Italic;
else
FontStyle = FontStyles.Normal;
}
}
/// int fontWeight to/from FontWeight
public FontWeight FontWeight
{
get
{
return FontWeight.FromOpenTypeWeight(fontWeight);
}
set
{
fontWeight = value.ToOpenTypeWeight();
}
}
/// int fontStretch to/from FontStretch
public FontStretch FontStretch
{
get
{
return FontStretch.FromOpenTypeStretch(fontStretch);
}
set
{
fontStretch = value.ToOpenTypeStretch();
}
}
/// int fontStyle to/from FontStyle
public FontStyle FontStyle
{
get
{
switch (fontStyle)
{
case 1:
return FontStyles.Oblique;
case 2:
return FontStyles.Italic;
default:
return FontStyles.Normal; // Otherwise, normal
}
}
set
{
if (value == FontStyles.Oblique)
fontStyle = 1;
else if (value == FontStyles.Italic)
fontStyle = 2;
else
fontStyle = 0; // Otherwise, normal
}
}
/// just double FontSize property
public double FontSize
{
get { return fontSize; }
set { fontSize = value; }
}
/// string fontFamily to/from FontFamily
public FontFamily FontFamily
{
get
{
return new FontFamily(fontFamily);
}
set
{
fontFamily = value.Source;
}
}
/// just Color property
public Brush Foreground
{
get { return new SolidColorBrush(color); }
set { color = (value as SolidColorBrush).Color; }
}
#endregion
#region Generator
/// <summary>
/// Default Generator
/// </summary>
public WindowFont()
{
fontFamily = "Arial"; // Default Font
fontWeight = FontWeights.Normal.ToOpenTypeWeight(); // Weithgt is normal
fontStretch = FontStretches.Normal.ToOpenTypeStretch(); // Stretch is normal
fontStyle = 0; // Style is normal
fontSize = 12.0; // Size is 12.0
color = Brushes.Black.Color; // Color is Black
}
/// <summary>
/// Generator to Font (System.Drawing.Font, in WinForms)
/// </summary>
/// <param name="tempFont"></param>
/// <param name="tempColor"></param>
public WindowFont(System.Drawing.Font tempFont, Color tempColor)
{
Font = tempFont;
color = tempColor;
}
#endregion
#region Function
/// <summary>
/// Function for Applying Font to Label
/// </summary>
/// <param name="obj"></param>
public void ApplyToLabel(Label lbl)
{
lbl.FontFamily = FontFamily;
lbl.FontSize = FontSize;
lbl.FontStretch = FontStretch;
lbl.FontStyle = FontStyle;
lbl.FontWeight = FontWeight;
lbl.Foreground = Foreground;
}
// will be added...
#endregion
}