■ 설치 폰트 샘플을 표시하는 방법을 보여준다.
▶ MainWindow.xaml
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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="설치 폰트 샘플 표시하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition /> <ColumnDefinition Width="30" /> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" Margin="5" Content="텍스트" /> <TextBox Name="sampleTextBox" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" Margin="5" VerticalContentAlignment="Center" Text="Sample Text" /> <Label Grid.Row="1" Grid.Column="0" Margin="5" Content="폰트 크기" /> <Slider Name="fontSizeSlider" Grid.Row="1" Grid.Column="1" Margin="5" Minimum="5" Maximum="100" Value="20" TickFrequency="5" TickPlacement="BottomRight" IsSnapToTickEnabled="True" /> <Label Grid.Row="1" Grid.Column="2" VerticalAlignment="Center" Content="{Binding ElementName=fontSizeSlider, Path=Value}" ContentStringFormat="{}{0:#}" /> <Button Grid.Row="2" Grid.ColumnSpan="3" Margin="5" Width="120" Height="30" IsDefault="True" Content="샘플 표시하기" Click="showSampleButton_Click" /> <ListBox Name="sampleFontListBox" Grid.Row="3" Grid.ColumnSpan="3" Margin="5" SelectionChanged="sampleFontListBox_SelectionChanged" /> <Label Name="fontNameLabel" Grid.Row="4" Grid.ColumnSpan="3" Margin="5 0 5 0" Content=" " /> </Grid> </Window> |
▶ MainWindow.xaml.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 |
using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 샘플 표시하기 버튼 클릭시 처리하기 - showSampleButton_Click(sender, e) /// <summary> /// 샘플 표시하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void showSampleButton_Click(object sender, RoutedEventArgs e) { this.fontNameLabel.Content = null; string sample = this.sampleTextBox.Text; this.sampleFontListBox.Items.Clear(); foreach(FontFamily fontFamily in Fonts.SystemFontFamilies) { Label label = new Label(); label.Content = sample; label.FontFamily = fontFamily; label.FontSize = this.fontSizeSlider.Value; this.sampleFontListBox.Items.Add(label); } } #endregion #region 샘플 폰트 리스트 박스 선택 변경시 처리하기 - sampleFontListBox_SelectionChanged(sender, e) /// <summary> /// 샘플 폰트 리스트 박스 선택 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void sampleFontListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { Label label = this.sampleFontListBox.SelectedItem as Label; if(label == null) { this.fontNameLabel.Content = null; } else { this.fontNameLabel.Content = label.FontFamily.ToString(); } } #endregion } } |