C#/WPF
WPF FontDialog
기공이
2017. 7. 18. 11:10
WPF에서 FontDialog를 사용하려면 꼭 System.Windows.Forms에 있는 FontDialog를 사용했어야 했습니다.
(이전 글에서 설명 링크)
그런데 윈도우 7에서 .otf형식의 확장자를 사용하다보니 중간에 문제가 발생했습니다. 윈도우 10에서는 문제가 없었는데 말이죠.
그래서 Font Dialog를 직접 만들게 되었습니다.
급하게 필요한 기능만을 모아 간단하게 만들었습니다.
코드는 git hub에 올려두었습니다. (링크)
.net framework 3.5 버전 이상이 설치되어 있으면 가능하며,
윈도우 7 부터는 3.5버전이 기본 설치되어 모두 사용 가능합니다.
버전 1의 코드를 여기에 올려놓겠습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <Window x:Class="Font.FontDialog" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Font" mc:Ignorable="d" Title="FontDialog" Height="300" Width="600" ResizeMode="NoResize" > <Grid> <TextBox x:Name="textFont" TextWrapping="Wrap" Text="" Margin="30,40,0,0" Height="23" TextAlignment="Left" VerticalAlignment="Top" HorizontalAlignment="Left" Width="170" TextChanged="textFont_TextChanged"/> <ListBox x:Name="lboxFont" HorizontalAlignment="Left" Height="165" VerticalAlignment="Top" Width="170" Margin="30,75,0,0" SelectionChanged="lboxFont_SelectionChanged"/> <ListBox x:Name="lboxFontStyle" HorizontalAlignment="Left" Height="200" VerticalAlignment="Top" Width="150" Margin="220,40,0,0"/> <TextBox x:Name="textFontSize" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" TextAlignment="Left" VerticalAlignment="Top" Width="90" Margin="385,40,0,0" PreviewKeyDown="textFontSize_PreviewKeyDown" TextChanged="textFontSize_TextChanged"/> <ListBox x:Name="lboxFontSize" HorizontalAlignment="Left" Height="165" VerticalAlignment="Top" Width="90" Margin="385,75,0,0" SelectionChanged="lboxFontSize_SelectionChanged"/> <Button x:Name="btnOK" Content="확인" IsDefault="True" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="490,40,0,0" Click="btnOK_Click"/> <Button x:Name="btnCancel" Content="취소" IsCancel="True" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="490,85,0,0"/> </Grid> </Window> | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; namespace Font { /// <summary> /// WPF Simple Font Dialog /// </summary> public partial class FontDialog : Window { #region Member List<string> listFont = new List<string>(); List<string> listFontStyle; List<FamilyTypeface> listFontTypeface; #endregion #region Property /// <summary> /// 사용자가 선택한 FontFamily /// </summary> public FontFamily ResultFontFamily { get; private set; } /// <summary> /// 사용자가 선택한 FontSize /// </summary> public double ResultFontSize { get; private set; } /// <summary> /// 사용자가 선택한 FontTypeFace /// </summary> public FamilyTypeface ResultTypeFace { get; private set; } #endregion #region Constructor /// <summary> /// FontDialog의 기본 생성자 /// </summary> public FontDialog() { InitializeComponent(); Control control = this; ResultFontFamily = control.FontFamily; ResultFontSize = control.FontSize; ResultTypeFace = new FamilyTypeface(); ResultTypeFace.Stretch = control.FontStretch; ResultTypeFace.Style = control.FontStyle; ResultTypeFace.Weight = control.FontWeight; var cond = System.Windows.Markup.XmlLanguage.GetLanguage(System.Globalization.CultureInfo.CurrentUICulture.Name); foreach (FontFamily item in Fonts.SystemFontFamilies) { if (item.FamilyNames.ContainsKey(cond)) listFont.Add(item.FamilyNames[cond]); else listFont.Add(item.ToString()); } listFont.Sort(); lboxFont.ItemsSource = listFont; lboxFont.SelectedItem = control.FontFamily.ToString(); lboxFont.ScrollIntoView(lboxFont.SelectedItem); textFont.Text = control.FontFamily.ToString(); double[] listSize = { 8, 9, 10, 10.5, 11, 12, 14, 16, 18, 20, 24, 28, 32, 36, 40, 44, 48, 54, 60, 66, 72, 80, 88, 96 }; lboxFontSize.ItemsSource = listSize; lboxFontSize.SelectedItem = FontSize; textFontSize.Text = control.FontSize.ToString(); } /// <summary> /// FontDialog 생성자 /// </summary> /// <param name="control">Dialog의 초기값으로 설정하게 할 Base Control</param> public FontDialog(Control control) { InitializeComponent(); ResultFontFamily = control.FontFamily; ResultFontSize = control.FontSize; ResultTypeFace = new FamilyTypeface(); ResultTypeFace.Stretch = control.FontStretch; ResultTypeFace.Style = control.FontStyle; ResultTypeFace.Weight = control.FontWeight; var cond = System.Windows.Markup.XmlLanguage.GetLanguage(System.Globalization.CultureInfo.CurrentUICulture.Name); foreach (FontFamily item in Fonts.SystemFontFamilies) { if (item.FamilyNames.ContainsKey(cond)) listFont.Add(item.FamilyNames[cond]); else listFont.Add(item.ToString()); } listFont.Sort(); lboxFont.ItemsSource = listFont; lboxFont.SelectedItem = control.FontFamily.ToString(); lboxFont.ScrollIntoView(lboxFont.SelectedItem); textFont.Text = control.FontFamily.ToString(); double[] listSize = { 8, 9, 10, 10.5, 11, 12, 14, 16, 18, 20, 24, 28, 32, 36, 40, 44, 48, 54, 60, 66, 72, 80, 88, 96 }; lboxFontSize.ItemsSource = listSize; lboxFontSize.SelectedItem = FontSize; textFontSize.Text = control.FontSize.ToString(); } #endregion #region Event Handler private void textFontSize_PreviewKeyDown(object sender, KeyEventArgs e) { if (!(((Key.D0 <= e.Key) && (e.Key <= Key.D9)) || ((Key.NumPad0 <= e.Key) && (e.Key <= Key.NumPad9)) || e.Key == Key.Back || e.Key == Key.OemPeriod || e.Key == Key.Delete)) { e.Handled = true; } else if (e.Key == Key.OemPeriod) { if ((sender as TextBox).Text.IndexOf('.') > -1) e.Handled = true; } } private void btnOK_Click(object sender, RoutedEventArgs e) { ResultFontFamily = new FontFamily(listFont[lboxFont.SelectedIndex]); ResultFontSize = double.Parse(textFontSize.Text); ResultTypeFace = listFontTypeface[lboxFontStyle.SelectedIndex]; DialogResult = true; } private void lboxFont_SelectionChanged(object sender, SelectionChangedEventArgs e) { listFontStyle = new List<string>(); FontFamily family = new FontFamily((sender as ListBox).SelectedItem as string); listFontTypeface = family.FamilyTypefaces.ToList(); int selectIndex = -1; var cond = System.Windows.Markup.XmlLanguage.GetLanguage(System.Globalization.CultureInfo.CurrentUICulture.Name); var list = family.GetTypefaces().ToList(); for(int i = 0; i < list.Count; i++) { var item = list[i]; if (item.FaceNames.ContainsKey(cond)) { listFontStyle.Add(item.FaceNames[cond]); } else { listFontStyle.Add(item.FaceNames[System.Windows.Markup.XmlLanguage.GetLanguage("en-us")]); } if (family.ToString() == ResultFontFamily.ToString()) { if (item.Stretch == ResultTypeFace.Stretch && item.Style == ResultTypeFace.Style && item.Weight == ResultTypeFace.Weight) selectIndex = i; } } lboxFontStyle.ItemsSource = listFontStyle; if (selectIndex > -1) lboxFontStyle.SelectedIndex = selectIndex; else lboxFontStyle.SelectedIndex = 0; } private void textFont_TextChanged(object sender, TextChangedEventArgs e) { string lower = textFont.Text.ToLower(); foreach (var item in listFont) { if (item.ToLower().StartsWith(lower)) { lboxFont.SelectedItem = item; lboxFont.ScrollIntoView(item); return; } } } private void lboxFontSize_SelectionChanged(object sender, SelectionChangedEventArgs e) { textFontSize.Text = (sender as ListBox).SelectedItem.ToString(); } private void textFontSize_TextChanged(object sender, TextChangedEventArgs e) { string num = textFontSize.Text; foreach (var item in lboxFontSize.Items) { if (item.ToString() == num) { lboxFontSize.SelectedItem = item; lboxFontSize.ScrollIntoView(item); return; } } } #endregion #region Function /// <summary> /// Apply Result Font to Control /// </summary> /// <param name="control">Control to Apply Result</param> public void ApplyToControl(Control control) { control.FontFamily = ResultFontFamily; control.FontSize = ResultFontSize; control.FontStretch = ResultTypeFace.Stretch; control.FontStyle = ResultTypeFace.Style; control.FontWeight = ResultTypeFace.Weight; } #endregion } } | cs |