첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ 설치 폰트 샘플 표시하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainWindow.xaml

 

 

<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

 

 

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

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요