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

■ 폰트 대화 상자 만들기

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


TestProject.zip


ListControl.cs

 

 

using System;

using System.Collections;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Input;

 

namespace TestProject

{

    /// <summary>

    /// 리스트 컨트롤

    /// </summary>

    public class ListControl : ContentControl

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Declaration

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region Event

 

        /// <summary>

        /// 선택 변경시

        /// </summary>

        public event EventHandler SelectionChanged;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 스크롤 뷰어

        /// </summary>

        private ScrollViewer scrollViewer;

 

        /// <summary>

        /// 스택 패널

        /// </summary>

        private StackPanel stackPanel;

 

        /// <summary>

        /// 배열 리스트

        /// </summary>

        private ArrayList arrayList = new ArrayList();

 

        /// <summary>

        /// 선택 인덱스

        /// </summary>

        private int selectedIndex = -1;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 수 - Count

 

        /// <summary>

        ///

        /// </summary>

        public int Count

        {

            get

            {

                return this.arrayList.Count;

            }

        }

 

        #endregion

        #region 선택 인덱스 - SelectedIndex

 

        /// <summary>

        /// 선택 인덱스

        /// </summary>

        public int SelectedIndex

        {

            set

            {

                if(value < -1 || value >= Count)

                {

                    throw new ArgumentOutOfRangeException("SelectedIndex");

                }

 

                if(value == this.selectedIndex)

                {

                    return;

                }

 

                if(this.selectedIndex != -1)

                {

                    TextBlock textBlock = this.stackPanel.Children[this.selectedIndex] as TextBlock;

 

                    textBlock.Background = SystemColors.WindowBrush;

                    textBlock.Foreground = SystemColors.WindowTextBrush;

                }

 

                this.selectedIndex = value;

 

                if(this.selectedIndex > -1)

                {

                    TextBlock textBlock = this.stackPanel.Children[this.selectedIndex] as TextBlock;

 

                    textBlock.Background = SystemColors.HighlightBrush;

                    textBlock.Foreground = SystemColors.HighlightTextBrush;

                }

 

                ScrollIntoView();

 

                OnSelectionChanged(EventArgs.Empty);

            }

            get

            {

                return this.selectedIndex;

            }

        }

 

        #endregion

        #region 선택 항목 - SelectedItem

 

        /// <summary>

        /// 선택 항목

        /// </summary>

        public object SelectedItem

        {

            set

            {

                SelectedIndex = this.arrayList.IndexOf(value);

            }

            get

            {

                if(SelectedIndex > -1)

                {

                    return this.arrayList[SelectedIndex];

                }

 

                return null;

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - ListControl()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public ListControl()

        {

            Focusable = false;

 

            Border border = new Border();

 

            border.BorderThickness = new Thickness(1);

            border.BorderBrush     = SystemColors.ActiveBorderBrush;

            border.Background      = SystemColors.WindowBrush;

 

            Content = border;

 

            this.scrollViewer = new ScrollViewer();

 

            this.scrollViewer.Focusable = false;

            this.scrollViewer.Padding   = new Thickness(2, 0, 0, 0);

 

            border.Child = this.scrollViewer;

 

            this.stackPanel = new StackPanel();

 

            this.scrollViewer.Content = this.stackPanel;

 

            AddHandler

            (

                TextBlock.MouseLeftButtonDownEvent,

                new MouseButtonEventHandler(textBlock_MouseLeftButtonDown)

            );

 

            Loaded += ListControl_Loaded;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Public

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 지우기 - Clear()

 

        /// <summary>

        /// 지우기

        /// </summary>

        public void Clear()

        {

            SelectedIndex = -1;

 

            this.stackPanel.Children.Clear();

 

            this.arrayList.Clear();

        }

 

        #endregion

        #region 추가하기 - Add(value)

 

        /// <summary>

        /// 추가하기

        /// </summary>

        /// <param name="value"></param>

        public void Add(object value)

        {

            this.arrayList.Add(value);

 

            TextBlock textBlock = new TextBlock();

 

            textBlock.Text = value.ToString();

 

            this.stackPanel.Children.Add(textBlock);

        }

 

        #endregion

        #region 삽입하기 - Insert(index, value)

 

        /// <summary>

        /// 삽입하기

        /// </summary>

        /// <param name="index">인덱스</param>

        /// <param name="value"></param>

        public void Insert(int index, object value)

        {

            this.arrayList.Insert(index, value);

 

            TextBlock textBlock = new TextBlock();

 

            textBlock.Text = value.ToString();

 

            this.stackPanel.Children.Insert(index, textBlock);

        }

 

        #endregion

        #region 포함 여부 조사하기 - Contains(value)

 

        /// <summary>

        /// 포함 여부 조사하기

        /// </summary>

        /// <param name="value"></param>

        /// <returns>포함 여부</returns>

        public bool Contains(object value)

        {

            return this.arrayList.Contains(value);

        }

 

        #endregion

        #region 글자로 이동하기 - GoToLetter(character)

 

        /// <summary>

        /// 글자로 이동하기

        /// </summary>

        /// <param name="character">문자</param>

        public void GoToLetter(char character)

        {

            int offset = SelectedIndex + 1;

 

            for(int i = 0; i < Count; i++)

            {

                int index = (i + offset) % Count;

 

                if(Char.ToUpper(character) == Char.ToUpper(this.arrayList[index].ToString()[0]))

                {

                    SelectedIndex = index;

 

                    break;

                }

            }

        }

 

        #endregion

        #region 페이지 위로 이동하기 - PageUp()

 

        /// <summary>

        /// 페이지 위로 이동하기

        /// </summary>

        public void PageUp()

        {

            if(SelectedIndex == -1 || Count == 0)

            {

                return;

            }

 

            int index = SelectedIndex - (int)(Count * this.scrollViewer.ViewportHeight / this.scrollViewer.ExtentHeight);

 

            if(index < 0)

            {

                index = 0;

            }

 

            SelectedIndex = index;

        }

 

        #endregion

        #region 페이지 아래로 이동하기 - PageDown()

 

        /// <summary>

        /// 페이지 아래로 이동하기

        /// </summary>

        public void PageDown()

        {

            if(SelectedIndex == -1 || Count == 0)

            {

                return;

            }

 

            int index = SelectedIndex + (int)(Count * this.scrollViewer.ViewportHeight / this.scrollViewer.ExtentHeight);

 

            if(index > Count - 1)

            {

                index = Count - 1;

            }

 

            SelectedIndex = index;

        }

 

        #endregion

        #region 선택 항목 보이기 - ScrollIntoView()

 

        /// <summary>

        /// 선택 항목 보이기

        /// </summary>

        public void ScrollIntoView()

        {

            if(Count == 0 || SelectedIndex == -1 || this.scrollViewer.ViewportHeight > this.scrollViewer.ExtentHeight)

            {

                return;

            }

 

            double itemHeight       = this.scrollViewer.ExtentHeight / Count;

            double itemTopOffset    = SelectedIndex * itemHeight;

            double itemBottomOffset = (SelectedIndex + 1) * itemHeight;

 

            if(itemTopOffset < this.scrollViewer.VerticalOffset)

            {

                this.scrollViewer.ScrollToVerticalOffset(itemTopOffset);

            }

            else if(itemBottomOffset > this.scrollViewer.VerticalOffset + this.scrollViewer.ViewportHeight)

            {

                this.scrollViewer.ScrollToVerticalOffset

                (

                    this.scrollViewer.VerticalOffset + itemBottomOffset - this.scrollViewer.VerticalOffset - this.scrollViewer.ViewportHeight

                );

            }

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 선택 변경시 처리하기 - OnSelectionChanged(e)

 

        /// <summary>

        /// 선택 변경시 처리하기

        /// </summary>

        /// <param name="e">이벤트 인자</param>

        protected virtual void OnSelectionChanged(EventArgs e)

        {

            if(SelectionChanged != null)

            {

                SelectionChanged(this, e);

            }

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 리스트 컨트롤 로드시 처리하기 - ListControl_Loaded(sender, e)

 

        /// <summary>

        /// 리스트 컨트롤 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void ListControl_Loaded(object sender, RoutedEventArgs e)

        {

            ScrollIntoView();

        }

 

        #endregion

        #region 텍스트 블럭 마우스 왼쪽 버튼 DOWN 처리하기 - textBlock_MouseLeftButtonDown(sender, e)

 

        /// <summary>

        /// 텍스트 블럭 마우스 왼쪽 버튼 DOWN 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void textBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

        {

            if(e.Source is TextBlock)

            {

                SelectedIndex = this.stackPanel.Children.IndexOf(e.Source as TextBlock);

            }

        }

 

        #endregion

    }

}

 

 

TextListControl.cs

 

 

using System;

using System.Windows.Controls;

using System.Windows.Input;

 

namespace TestProject

{

    /// <summary>

    /// 텍스트 리스트 컨트롤

    /// </summary>

    public class TextListControl : ContentControl

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Declaration

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region Event

 

        /// <summary>

        /// 텍스트 변경시

        /// </summary>

        public event TextChangedEventHandler TextChanged;

 

        /// <summary>

        /// 선택 변경시

        /// </summary>

        public event EventHandler SelectionChanged;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 텍스트 상자

        /// </summary>

        private TextBox textBox;

 

        /// <summary>

        /// 리스트 컨트롤

        /// </summary>

        private ListControl listControl;

 

        /// <summary>

        /// 읽기 전용 여부

        /// </summary>

        private bool isReadOnly;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 텍스트 - Text

 

        /// <summary>

        /// 텍스트

        /// </summary>

        public string Text

        {

            get

            {

                return this.textBox.Text;

            }

            set

            {

                this.textBox.Text = value;

            }

        }

 

        #endregion

        #region 읽기 전용 여부 - IsReadOnly

 

        /// <summary>

        /// 읽기 전용 여부

        /// </summary>

        public bool IsReadOnly

        {

            get

            {

                return this.isReadOnly;

            }

            set

            {

                this.isReadOnly = value;

 

                this.textBox.IsReadOnly = this.isReadOnly;

            }

        }

 

        #endregion

        #region 선택 인덱스 - SelectedIndex

 

        /// <summary>

        /// 선택 인덱스

        /// </summary>

        public int SelectedIndex

        {

            get

            {

                return this.listControl.SelectedIndex;

            }

            set

            {

                this.listControl.SelectedIndex = value;

 

                if(this.listControl.SelectedIndex == -1)

                {

                    this.textBox.Text = string.Empty;

                }

                else

                {

                    this.textBox.Text = this.listControl.SelectedItem.ToString();

                }

            }

        }

 

        #endregion

        #region 선택 항목 - SelectedItem

 

        /// <summary>

        /// 선택 항목

        /// </summary>

        public object SelectedItem

        {

            get

            {

                return this.listControl.SelectedItem;

            }

            set

            {

                this.listControl.SelectedItem = value;

 

                if(this.listControl.SelectedItem != null)

                {

                    this.textBox.Text = this.listControl.SelectedItem.ToString();

                }

                else

                {

                    this.textBox.Text = string.Empty;

                }

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - TextListControl()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public TextListControl()

        {

            DockPanel dockPanel = new DockPanel();

 

            Content = dockPanel;

 

            this.textBox = new TextBox();

 

            this.textBox.TextChanged += this.textBox_TextChanged;

 

            dockPanel.Children.Add(this.textBox);

 

            DockPanel.SetDock(this.textBox, Dock.Top);

 

            this.listControl = new ListControl();

 

            this.listControl.SelectionChanged += this.listControl_SelectionChanged;

 

            dockPanel.Children.Add(this.listControl);

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Public

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 지우기 - Clear()

 

        /// <summary>

        /// 지우기

        /// </summary>

        public void Clear()

        {

            this.listControl.Clear();

        }

 

        #endregion

        #region 추가하기 - Add(value)

 

        /// <summary>

        /// 추가하기

        /// </summary>

        /// <param name="value"></param>

        public void Add(object value)

        {

            this.listControl.Add(value);

        }

 

        #endregion

        #region 삽입하기 - Insert(index, value)

 

        /// <summary>

        /// 삽입하기

        /// </summary>

        /// <param name="index">인덱스</param>

        /// <param name="value"></param>

        public void Insert(int index, object value)

        {

            this.listControl.Insert(index, value);

        }

 

        #endregion

        #region 포함 여부 조사하기 - Contains(value)

 

        /// <summary>

        /// 포함 여부 조사하기

        /// </summary>

        /// <param name="value"></param>

        /// <returns>포함 여부</returns>

        public bool Contains(object value)

        {

            return this.listControl.Contains(value);

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Protected

 

        #region 마우스 DOWN 처리하기 - OnMouseDown(e)

 

        /// <summary>

        /// 마우스 DOWN 처리하기

        /// </summary>

        /// <param name="e">이벤트 인자</param>

        protected override void OnMouseDown(MouseButtonEventArgs e)

        {

            base.OnMouseDown(e);

 

            Focus();

        }

 

        #endregion

        #region 키보드 포커스 획득시 처리하기 - OnGotKeyboardFocus(e)

 

        /// <summary>

        /// 키보드 포커스 획득시 처리하기

        /// </summary>

        /// <param name="e">이벤트 인자</param>

        protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)

        {

            base.OnGotKeyboardFocus(e);

 

            if(e.NewFocus == this)

            {

                this.textBox.Focus();

 

                if(SelectedIndex == -1 && this.listControl.Count > 0)

                {

                    SelectedIndex = 0;

                }

            }

        }

 

        #endregion

        #region 텍스트 입력시 사전 처리하기 - OnPreviewTextInput(e)

 

        /// <summary>

        /// 텍스트 입력시 사전 처리하기

        /// </summary>

        /// <param name="e">이벤트 인자</param>

        protected override void OnPreviewTextInput(TextCompositionEventArgs e)

        {

            base.OnPreviewTextInput(e);

 

            if(IsReadOnly)

            {

                if(e.Text.Length > 0)

                {

                    this.listControl.GoToLetter(e.Text[0]);

 

                    e.Handled = true;

                }

            }

        }

 

        #endregion

        #region 키 DOWN 사전 처리하기 - OnPreviewKeyDown(e)

 

        /// <summary>

        /// 키 DOWN 사전 처리하기

        /// </summary>

        /// <param name="e">이벤트 인자</param>

        protected override void OnPreviewKeyDown(KeyEventArgs e)

        {

            base.OnPreviewKeyDown(e);

 

            if(SelectedIndex == -1)

            {

                return;

            }

 

            switch(e.Key)

            {

                case Key.Home :

 

                    if(this.listControl.Count > 0)

                    {

                        SelectedIndex = 0;

                    }

 

                    break;

 

                case Key.End :

 

                    if(this.listControl.Count > 0)

                    {

                        SelectedIndex = this.listControl.Count - 1;

                    }

 

                    break;

 

                case Key.Up :

 

                    if(SelectedIndex > 0)

                    {

                        SelectedIndex--;

                    }

 

                    break;

 

                case Key.Down :

 

                    if(SelectedIndex < this.listControl.Count - 1)

                    {

                        SelectedIndex++;

                    }

 

                    break;

 

                case Key.PageUp :

 

                    this.listControl.PageUp();

 

                    break;

 

                case Key.PageDown :

 

                    this.listControl.PageDown();

 

                    break;

 

                default :

 

                    return;

            }

 

            e.Handled = true;

        }

 

        #endregion

        #region 선택 변경시 처리하기 - OnSelectionChanged(e)

 

        /// <summary>

        /// 선택 변경시 처리하기

        /// </summary>

        /// <param name="e">이벤트 인자</param>

        protected virtual void OnSelectionChanged(EventArgs e)

        {

            if(SelectionChanged != null)

            {

                SelectionChanged(this, e);

            }

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 텍스트 상자 텍스트 변경시 처리하기 - textBox_TextChanged(sender, e)

 

        /// <summary>

        /// 텍스트 상자 텍스트 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void textBox_TextChanged(object sender, TextChangedEventArgs e)

        {

            if(TextChanged != null)

            {

                TextChanged(this, e);

            }

        }

 

        #endregion

        #region 리스트 컨트롤 선택 변경시 처리하기 - listControl_SelectionChanged(sender, e)

 

        /// <summary>

        /// 리스트 컨트롤 선택 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void listControl_SelectionChanged(object sender, EventArgs e)

        {

            if(SelectedIndex == -1)

            {

                this.textBox.Text = string.Empty;

            }

            else

            {

                this.textBox.Text = this.listControl.SelectedItem.ToString();

            }

 

            OnSelectionChanged(e);

        }

 

        #endregion

    }

}

 

 

FontDialog.cs

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Markup;

using System.Windows.Media;

 

namespace TestProject

{

    /// <summary>

    /// 폰트 대화 상자

    /// </summary>

    public class FontDialog : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 폰트 패밀리 텍스트 리스트 컨트롤

        /// </summary>

        private TextListControl fontFamilyTextListControl;

 

        /// <summary>

        /// 폰트 스타일 텍스트 리스트 컨트롤

        /// </summary>

        private TextListControl fontStyleTextListControl;

 

        /// <summary>

        /// 폰트 웨이트 텍스트 리스트 컨트롤

        /// </summary>

        private TextListControl fontWeightTextListControl;

 

        /// <summary>

        /// 폰트 스트레치 텍스트 리스트 컨트롤

        /// </summary>

        private TextListControl fontStretchTextListControl;

 

        /// <summary>

        /// 폰트 크기 텍스트 리스트 컨트롤

        /// </summary>

        private TextListControl fontSizeTextListControl;

 

        /// <summary>

        /// 디스플레이 레이블

        /// </summary>

        private Label displayLabel;

 

        /// <summary>

        /// 업데이트 금지 여부

        /// </summary>

        private bool isUpdateSuppressed = true;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 타입 페이스 - Typeface

 

        /// <summary>

        /// 타입 페이스

        /// </summary>

        public Typeface Typeface

        {

            get

            {

                return new Typeface

                (

                    (FontFamily)this.fontFamilyTextListControl.SelectedItem,

                    (FontStyle)this.fontStyleTextListControl.SelectedItem,

                    (FontWeight)this.fontWeightTextListControl.SelectedItem,

                    (FontStretch)this.fontStretchTextListControl.SelectedItem

                );

            }

            set

            {

                if(this.fontFamilyTextListControl.Contains(value.FontFamily))

                {

                    this.fontFamilyTextListControl.SelectedItem  = value.FontFamily;

                }

                else

                {

                    this.fontFamilyTextListControl.SelectedIndex = 0;

                }

 

                if(this.fontStyleTextListControl.Contains(value.Style))

                {

                    this.fontStyleTextListControl.SelectedItem  = value.Style;

                }

                else

                {

                    this.fontStyleTextListControl.SelectedIndex = 0;

                }

 

                if(this.fontWeightTextListControl.Contains(value.Weight))

                {

                    this.fontWeightTextListControl.SelectedItem  = value.Weight;

                }

                else

                {

                    this.fontWeightTextListControl.SelectedIndex = 0;

                }

 

                if(this.fontStretchTextListControl.Contains(value.Stretch))

                {

                    this.fontStretchTextListControl.SelectedItem  = value.Stretch;

                }

                else

                {

                    this.fontStretchTextListControl.SelectedIndex = 0;

                }

            }

        }

 

        #endregion

        #region 폰트 크기 - FaceSize

 

        /// <summary>

        /// 폰트 크기

        /// </summary>

        public double FaceSize

        {

            get

            {

                double size;

 

                if(!Double.TryParse(this.fontSizeTextListControl.Text, out size))

                {

                    size = 8.25d;

                }

 

                return size / 0.75d;

            }

            set

            {

                double size = 0.75d * value;

 

                this.fontSizeTextListControl.Text = size.ToString();

 

                if(!this.fontSizeTextListControl.Contains(size))

                {

                    this.fontSizeTextListControl.Insert(0, size);

                }

 

                this.fontSizeTextListControl.SelectedItem = size;

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - FontDialog()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public FontDialog()

        {

            ShowInTaskbar         = false;

            WindowStyle           = WindowStyle.SingleBorderWindow;

            WindowStartupLocation = WindowStartupLocation.CenterOwner;

            SizeToContent         = SizeToContent.WidthAndHeight;

            ResizeMode            = ResizeMode.NoResize;

            FontFamily            = new FontFamily("나눔고딕코딩");

            FontSize              = 16d;

            Title                 = "폰트";

 

            Grid layoutGrid = new Grid();

 

            layoutGrid.Margin = new Thickness(10);

 

            Content = layoutGrid;

 

            #region 레이아웃 행 1 정의

 

            RowDefinition layourRowDefinition1 = new RowDefinition();

 

            layourRowDefinition1.Height = new GridLength(200, GridUnitType.Pixel);

 

            layoutGrid.RowDefinitions.Add(layourRowDefinition1);

 

            #endregion

            #region 레이아웃 행 2 정의

 

            RowDefinition layourRowDefinition2 = new RowDefinition();

 

            layourRowDefinition2.Height = new GridLength(10, GridUnitType.Pixel);

 

            layoutGrid.RowDefinitions.Add(layourRowDefinition2);

 

            #endregion

            #region 레이아웃 행 3 정의

 

            RowDefinition layourRowDefinition3 = new RowDefinition();

 

            layourRowDefinition3.Height = new GridLength(150, GridUnitType.Pixel);

 

            layoutGrid.RowDefinitions.Add(layourRowDefinition3);

 

            #endregion

            #region 레이아웃 행 4 정의

 

            RowDefinition layourRowDefinition4 = new RowDefinition();

 

            layourRowDefinition4.Height = new GridLength(10, GridUnitType.Pixel);

 

            layoutGrid.RowDefinitions.Add(layourRowDefinition4);

 

            #endregion

            #region 레이아웃 행 5 정의

 

            RowDefinition layourRowDefinition5 = new RowDefinition();

 

            layourRowDefinition5.Height = GridLength.Auto;

 

            layoutGrid.RowDefinitions.Add(layourRowDefinition5);

 

            #endregion

            #region 레이아웃 열 정의

 

            ColumnDefinition layoutColumnDefinition = new ColumnDefinition();

 

            layoutColumnDefinition.Width = new GridLength(650, GridUnitType.Pixel);

 

            layoutGrid.ColumnDefinitions.Add(layoutColumnDefinition);

 

            #endregion

 

            Grid controlGrid = new Grid();

 

            layoutGrid.Children.Add(controlGrid);

 

            Grid.SetRow(controlGrid, 0);

 

            #region 헤더 컨트롤 행 정의

 

            RowDefinition headerControlRowDefinition = new RowDefinition();

 

            headerControlRowDefinition.Height = GridLength.Auto;

 

            controlGrid.RowDefinitions.Add(headerControlRowDefinition);

 

            #endregion

            #region 컨트롤 행 정의

 

            RowDefinition controlRowDefinition = new RowDefinition();

 

            controlRowDefinition.Height = new GridLength(100, GridUnitType.Star);

 

            controlGrid.RowDefinitions.Add(controlRowDefinition);

 

            #endregion

            #region 폰트 패밀리 열 정의

 

            ColumnDefinition fontFamilyColumnDefinition = new ColumnDefinition();

 

            fontFamilyColumnDefinition.Width = new GridLength(150, GridUnitType.Star);

 

            controlGrid.ColumnDefinitions.Add(fontFamilyColumnDefinition);

 

            #endregion

            #region 분리자 열 정의

 

            ColumnDefinition splitterColumnDefinition1 = new ColumnDefinition();

 

            splitterColumnDefinition1.Width = new GridLength(10, GridUnitType.Pixel);

 

            controlGrid.ColumnDefinitions.Add(splitterColumnDefinition1);

 

            #endregion

            #region 폰트 스타일 열 정의

 

            ColumnDefinition fontStyleColumnDefinition = new ColumnDefinition();

 

            fontStyleColumnDefinition.Width = new GridLength(100, GridUnitType.Star);

 

            controlGrid.ColumnDefinitions.Add(fontStyleColumnDefinition);

 

            #endregion

            #region 분리자 열 정의

 

            ColumnDefinition splitterColumnDefinition2 = new ColumnDefinition();

 

            splitterColumnDefinition2.Width = new GridLength(10, GridUnitType.Pixel);

 

            controlGrid.ColumnDefinitions.Add(splitterColumnDefinition2);

 

            #endregion

            #region 폰트 두께 열 정의

 

            ColumnDefinition fontWeightColumnDefinition = new ColumnDefinition();

 

            fontWeightColumnDefinition.Width = new GridLength(100, GridUnitType.Star);

 

            controlGrid.ColumnDefinitions.Add(fontWeightColumnDefinition);

 

            #endregion

            #region 분리자 열 정의

 

            ColumnDefinition splitterColumnDefinition3 = new ColumnDefinition();

 

            splitterColumnDefinition3.Width = new GridLength(10, GridUnitType.Pixel);

 

            controlGrid.ColumnDefinitions.Add(splitterColumnDefinition3);

 

            #endregion

            #region 폰트 확장 열 정의

 

            ColumnDefinition fontStretchColumnDefinition = new ColumnDefinition();

 

            fontStretchColumnDefinition.Width = new GridLength(100, GridUnitType.Star);

 

            controlGrid.ColumnDefinitions.Add(fontStretchColumnDefinition);

 

            #endregion

            #region 분리자 열 정의

 

            ColumnDefinition splitterColumnDefinition4 = new ColumnDefinition();

 

            splitterColumnDefinition4.Width = new GridLength(10, GridUnitType.Pixel);

 

            controlGrid.ColumnDefinitions.Add(splitterColumnDefinition4);

 

            #endregion

            #region 폰트 크기 열 정의

 

            ColumnDefinition fontSizeColumnDefinition = new ColumnDefinition();

 

            fontSizeColumnDefinition.Width = new GridLength(75, GridUnitType.Star);

 

            controlGrid.ColumnDefinitions.Add(fontSizeColumnDefinition);

 

            #endregion

            #region 폰트 패밀리 라벨

 

            Label fontFamilyLabel = new Label();

 

            fontFamilyLabel.Content = "폰트";

            fontFamilyLabel.Margin  = new Thickness(0);

 

            controlGrid.Children.Add(fontFamilyLabel);

 

            Grid.SetRow   (fontFamilyLabel, 0);

            Grid.SetColumn(fontFamilyLabel, 0);

 

            #endregion

            #region 폰트 패밀리

 

            this.fontFamilyTextListControl = new TextListControl();

 

            this.fontFamilyTextListControl.IsReadOnly = true;

            this.fontFamilyTextListControl.Margin     = new Thickness(0);

 

            controlGrid.Children.Add(this.fontFamilyTextListControl);

 

            Grid.SetRow   (this.fontFamilyTextListControl, 1);

            Grid.SetColumn(this.fontFamilyTextListControl, 0);

 

            #endregion

            #region 폰트 스타일 라벨

 

            Label fontStyleLabel = new Label();

 

            fontStyleLabel.Content = "형식";

            fontStyleLabel.Margin  = new Thickness(0);

 

            controlGrid.Children.Add(fontStyleLabel);

 

            Grid.SetRow   (fontStyleLabel, 0);

            Grid.SetColumn(fontStyleLabel, 2);

 

            #endregion

            #region 폰트 스타일

 

            this.fontStyleTextListControl = new TextListControl();

 

            this.fontStyleTextListControl.IsReadOnly = true;

            this.fontStyleTextListControl.Margin     = new Thickness(0);

 

            controlGrid.Children.Add(this.fontStyleTextListControl);

 

            Grid.SetRow   (this.fontStyleTextListControl, 1);

            Grid.SetColumn(this.fontStyleTextListControl, 2);

 

            #endregion

            #region 폰트 두께 라벨

 

            Label fontWeightLabel = new Label();

 

            fontWeightLabel.Content = "두께";

            fontWeightLabel.Margin  = new Thickness(0);

 

            controlGrid.Children.Add(fontWeightLabel);

 

            Grid.SetRow   (fontWeightLabel, 0);

            Grid.SetColumn(fontWeightLabel, 4);

 

            #endregion

            #region 폰트 두께

 

            this.fontWeightTextListControl = new TextListControl();

 

            this.fontWeightTextListControl.IsReadOnly = true;

            this.fontWeightTextListControl.Margin     = new Thickness(0);

 

            controlGrid.Children.Add(this.fontWeightTextListControl);

 

            Grid.SetRow   (this.fontWeightTextListControl, 1);

            Grid.SetColumn(this.fontWeightTextListControl, 4);

 

            #endregion

            #region 폰트 확장 라벨

 

            Label fontStretchLabel = new Label();

 

            fontStretchLabel.Content = "너비";

            fontStretchLabel.Margin  = new Thickness(0);

 

            controlGrid.Children.Add(fontStretchLabel);

 

            Grid.SetRow   (fontStretchLabel, 0);

            Grid.SetColumn(fontStretchLabel, 6);

 

            #endregion

            #region 폰트 확장

 

            this.fontStretchTextListControl = new TextListControl();

 

            this.fontStretchTextListControl.IsReadOnly = true;

            this.fontStretchTextListControl.Margin     = new Thickness(0);

 

            controlGrid.Children.Add(this.fontStretchTextListControl);

 

            Grid.SetRow   (this.fontStretchTextListControl, 1);

            Grid.SetColumn(this.fontStretchTextListControl, 6);

 

            #endregion

            #region 폰트 크기 라벨

 

            Label fontSizeLabel = new Label();

 

            fontSizeLabel.Content = "크기";

            fontSizeLabel.Margin  = new Thickness(0);

 

            controlGrid.Children.Add(fontSizeLabel);

 

            Grid.SetRow   (fontSizeLabel, 0);

            Grid.SetColumn(fontSizeLabel, 8);

 

            #endregion

            #region 폰트 크기

 

            this.fontSizeTextListControl = new TextListControl();

 

            this.fontSizeTextListControl.Margin = new Thickness(0);

 

            controlGrid.Children.Add(this.fontSizeTextListControl);

 

            Grid.SetRow   (this.fontSizeTextListControl, 1);

            Grid.SetColumn(this.fontSizeTextListControl, 8);

 

            #endregion

 

            Border displayBorder = new Border();

 

            displayBorder.BorderBrush     = Brushes.Blue;

            displayBorder.BorderThickness = new Thickness(1);

 

            layoutGrid.Children.Add(displayBorder);

 

            Grid.SetRow(displayBorder, 2);

 

            this.displayLabel = new Label();

 

            this.displayLabel.Content                    = "가나다라마 ABC abc 12345";

            this.displayLabel.HorizontalContentAlignment = HorizontalAlignment.Center;

            this.displayLabel.VerticalContentAlignment   = VerticalAlignment.Center;

 

            displayBorder.Child = this.displayLabel;

 

            Grid buttonGrid = new Grid();

 

            layoutGrid.Children.Add(buttonGrid);

 

            Grid.SetRow(buttonGrid, 4);

 

            for(int i = 0; i < 5; i++)

            {

                buttonGrid.ColumnDefinitions.Add(new ColumnDefinition());

            }

 

            Button okButton = new Button();

 

            okButton.Content             = "확인";

            okButton.IsDefault           = true;

            okButton.HorizontalAlignment = HorizontalAlignment.Center;

            okButton.Width               = 100;

            okButton.Height              = 30;

 

            okButton.Click += okButton_Click;

 

            buttonGrid.Children.Add(okButton);

 

            Grid.SetColumn(okButton, 1);

 

            Button cancelButton = new Button();

 

            cancelButton.Content             = "취소";

            cancelButton.IsCancel            = true;

            cancelButton.HorizontalAlignment = HorizontalAlignment.Center;

            cancelButton.Width               = 100;

            cancelButton.Height              = 30;

 

            buttonGrid.Children.Add(cancelButton);

 

            Grid.SetColumn(cancelButton, 3);

 

            foreach(FontFamily fontFamily in Fonts.SystemFontFamilies.OrderBy(f => f.ToString()))

            {

                this.fontFamilyTextListControl.Add(fontFamily);

            }

 

            double[] fontSizeArray = new double[]

            {

                8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72

            };

 

            foreach(double fontSize in fontSizeArray)

            {

                this.fontSizeTextListControl.Add(fontSize);

            }

 

            #region 이벤트를 설정한다.

 

            this.fontFamilyTextListControl.SelectionChanged  += fontFamilyTextListControl_SelectionChanged;

            this.fontStyleTextListControl.SelectionChanged   += fontStyleTextListControl_SelectionChanged;

            this.fontWeightTextListControl.SelectionChanged  += fontWeightTextListControl_SelectionChanged;

            this.fontStretchTextListControl.SelectionChanged += fontStretchTextListControl_SelectionChanged;

            this.fontSizeTextListControl.TextChanged         += fontSizeTextListControl_TextChanged;

 

            #endregion

 

            Typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);

 

            FaceSize = FontSize;

 

            this.fontFamilyTextListControl.Focus();

 

            this.isUpdateSuppressed = false;

 

            UpdateSample();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 폰트 패밀리 선택 변경시 처리하기 - fontFamilyTextListControl_SelectionChanged(sender, e)

 

        /// <summary>

        /// 폰트 패밀리 선택 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void fontFamilyTextListControl_SelectionChanged(object sender, EventArgs e)

        {

            FontFamily   fontFamily          = this.fontFamilyTextListControl.SelectedItem  as FontFamily  ;

            FontStyle?   previousFontStyle   = this.fontStyleTextListControl.SelectedItem   as FontStyle?  ;

            FontWeight?  previousFontWeight  = this.fontWeightTextListControl.SelectedItem  as FontWeight? ;

            FontStretch? previousFontStretch = this.fontStretchTextListControl.SelectedItem as FontStretch?;

 

            this.isUpdateSuppressed = true;

 

            this.fontStyleTextListControl.Clear();

            this.fontWeightTextListControl.Clear();

            this.fontStretchTextListControl.Clear();

 

            foreach(FamilyTypeface familyTypeface in fontFamily.FamilyTypefaces)

            {

                if(!this.fontStyleTextListControl.Contains(familyTypeface.Style))

                {

                    if(familyTypeface.Style == FontStyles.Normal)

                    {

                        this.fontStyleTextListControl.Insert(0, familyTypeface.Style);

                    }

                    else

                    {

                        this.fontStyleTextListControl.Add(familyTypeface.Style);

                    }

                }

 

                if(!this.fontWeightTextListControl.Contains(familyTypeface.Weight))

                {

                    if(familyTypeface.Weight == FontWeights.Normal)

                    {

                        this.fontWeightTextListControl.Insert(0, familyTypeface.Weight);

                    }

                    else

                    {

                        this.fontWeightTextListControl.Add(familyTypeface.Weight);

                    }

                }

 

                if(!this.fontStretchTextListControl.Contains(familyTypeface.Stretch))

                {

                    if(familyTypeface.Stretch == FontStretches.Normal)

                    {

                        this.fontStretchTextListControl.Insert(0, familyTypeface.Stretch);

                    }

                    else

                    {

                        this.fontStretchTextListControl.Add(familyTypeface.Stretch);

                    }

                }

            }

 

            if(this.fontStyleTextListControl.Contains(previousFontStyle))

            {

                this.fontStyleTextListControl.SelectedItem  = previousFontStyle;

            }

            else

            {

                this.fontStyleTextListControl.SelectedIndex = 0;

            }

 

            if(this.fontWeightTextListControl.Contains(previousFontWeight))

            {

                this.fontWeightTextListControl.SelectedItem  = previousFontWeight;

            }

            else

            {

                this.fontWeightTextListControl.SelectedIndex = 0;

            }

 

            if(this.fontStretchTextListControl.Contains(previousFontStretch))

            {

                this.fontStretchTextListControl.SelectedItem  = previousFontStretch;

            }

            else

            {

                this.fontStretchTextListControl.SelectedIndex = 0;

            }

 

            this.isUpdateSuppressed = false;

 

            UpdateSample();

        }

 

        #endregion

        #region 폰트 형식 선택 변경시 처리하기 - fontStyleTextListControl_SelectionChanged(sender, e)

 

        /// <summary>

        /// 폰트 형식 선택 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void fontStyleTextListControl_SelectionChanged(object sender, EventArgs e)

        {

            UpdateSample();

        }

 

        #endregion

        #region 폰트 두께 선택 변경시 처리하기 - fontWeightTextListControl_SelectionChanged(sender, e)

 

        /// <summary>

        /// 폰트 두께 선택 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void fontWeightTextListControl_SelectionChanged(object sender, EventArgs e)

        {

            UpdateSample();

        }

 

        #endregion

        #region 폰트 너비 선택 변경시 처리하기 - fontStretchTextListControl_SelectionChanged(sender, e)

 

        /// <summary>

        /// 폰트 너비 선택 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void fontStretchTextListControl_SelectionChanged(object sender, EventArgs e)

        {

            UpdateSample();

        }

 

        #endregion

        #region 폰트 크기 텍스트 변경시 처리하기 - fontSizeTextListControl_TextChanged(sender, e)

 

        /// <summary>

        /// 폰트 크기 텍스트 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void fontSizeTextListControl_TextChanged(object sender, TextChangedEventArgs e)

        {

            UpdateSample();

        }

 

        #endregion

        #region OK 버튼 클릭시 처리하기 - okButton_Click(sender, e)

 

        /// <summary>

        /// OK 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void okButton_Click(object sender, RoutedEventArgs e)

        {

            DialogResult = true;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 샘플 갱신하기 - UpdateSample()

 

        /// <summary>

        /// 샘플 갱신하기

        /// </summary>

        private void UpdateSample()

        {

            if(this.isUpdateSuppressed)

            {

                return;

            }

 

            this.displayLabel.FontFamily  = (FontFamily )this.fontFamilyTextListControl.SelectedItem;

            this.displayLabel.FontStyle   = (FontStyle  )this.fontStyleTextListControl.SelectedItem;

            this.displayLabel.FontWeight  = (FontWeight )this.fontWeightTextListControl.SelectedItem;

            this.displayLabel.FontStretch = (FontStretch)this.fontStretchTextListControl.SelectedItem;

 

            double fontSize;

 

            if(!Double.TryParse(this.fontSizeTextListControl.Text, out fontSize))

            {

                fontSize = 8.25;

            }

 

            this.displayLabel.FontSize = fontSize / 0.75d;

        }

 

        #endregion

        #region 폰트명 구하기 - GetFontName(fontFamily)

 

        /// <summary>

        /// 폰트명 구하기

        /// </summary>

        /// <param name="fontFamily">폰트 패밀리</param>

        /// <returns>폰트명</returns>

        public string GetFontName(FontFamily fontFamily)

        {

            if(fontFamily == null)

            {

                return string.Empty;

            }

 

            if

            (

                fontFamily.FamilyNames.Count > 1 &&

                fontFamily.FamilyNames.Keys.ElementAt<XmlLanguage>(fontFamily.FamilyNames.Count - 1).IetfLanguageTag == "ko-kr"

            )

            {

                return fontFamily.FamilyNames.Values.ElementAt<string>(fontFamily.FamilyNames.Count - 1);

            }

            else

            {

                return fontFamily.FamilyNames.Values.ElementAt<string>(0);

            }

        }

 

        #endregion

    }

}

 

 

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>

        <StackPanel

            HorizontalAlignment="Center"

            VerticalAlignment="Center">

            <TextBlock Name="textBlock"

                HorizontalAlignment="Center"

                Margin="10"

                Text="가나다라마바" />

            <Button Name="fontButton"

                HorizontalAlignment="Center"

                Margin="10"

                Width="100"

                Height="30"

                Content="폰트" />

        </StackPanel>

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Windows;

using System.Windows.Media;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            this.fontButton.Click += fontButton_Click;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 폰트 버튼 클릭시 처리하기 - fontButton_Click(sender, e)

 

        /// <summary>

        /// 폰트 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void fontButton_Click(object sender, RoutedEventArgs e)

        {

            FontDialog fontDialog = new FontDialog();

 

            fontDialog.Owner = this;

 

            fontDialog.Typeface = new Typeface

            (

                this.textBlock.FontFamily,

                this.textBlock.FontStyle,

                this.textBlock.FontWeight,

                this.textBlock.FontStretch

            );

 

            fontDialog.FaceSize = this.textBlock.FontSize;

 

            if(fontDialog.ShowDialog().GetValueOrDefault())

            {

                this.textBlock.FontFamily  = fontDialog.Typeface.FontFamily;

                this.textBlock.FontSize    = fontDialog.FaceSize;

                this.textBlock.FontStyle   = fontDialog.Typeface.Style;

                this.textBlock.FontWeight  = fontDialog.Typeface.Weight;

                this.textBlock.FontStretch = fontDialog.Typeface.Stretch;

            }

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요