첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainWindow.xaml

<dxr:DXRibbonWindow x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
    xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/core"
    xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
    xmlns:dxr="http://schemas.devexpress.com/winfx/2008/xaml/ribbon"
    xmlns:local="clr-namespace:TestProject"
    Width="800"
    Height="600"
    Icon="pack://application:,,,/Image/applicationIcon-16x16.png"
    dxc:ThemeManager.ThemeName="Azure"
    Title="리본 컨트롤 생성하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <dxb:BarManager Name="barManager">
        <dxb:BarManager.Items>
            <dxb:BarButtonItem x:Name="newBarButtonItem"
                Glyph="pack://application:,,,/Image/new-16x16.png" 
                LargeGlyph="pack://application:,,,/Image/new-32x32.png"  
                Content="New" 
                Description="New blank document."
                Hint="New blank document." />
            <dxb:BarButtonItem x:Name="openBarButtonItem"
                Glyph="pack://application:,,,/Image/open-16x16.png" 
                LargeGlyph="pack://application:,,,/Image/open-32x32.png" 
                Content="Open" 
                Description="Open a file."
                Hint="Open a file." />
            <dxb:BarButtonItem x:Name="closeBarButtonItem"
                Glyph="pack://application:,,,/Image/close-16x16.png" 
                LargeGlyph="pack://application:,,,/Image/close-32x32.png"
                Hint="Close the current document"
                Content="Close" />
            <dxb:BarButtonItem x:Name="printBarButtonItem"
                Glyph="pack://application:,,,/Image/print-16x16.png" 
                LargeGlyph="pack://application:,,,/Image/print-32x32.png"  
                Content="Print" 
                Description="Prints the document."
                Hint="Prints the document." />
            <dxb:BarButtonItem x:Name="saveBarButtonItem"
                Glyph="pack://application:,,,/Image/save-16x16.png" 
                LargeGlyph="pack://application:,,,/Image/save-32x32.png"  
                Content="Save" 
                Description="Saves the document."
                Hint="Saves the document." />
            <dxb:BarButtonItem x:Name="saveAsBarButtonItem"
                Glyph="pack://application:,,,/Image/save-as-16x16.png" 
                LargeGlyph="pack://application:,,,/Image/save-as-32x32.png" 
                Content="Save As..." 
                Description="Save Document As..."
                Hint="Save Document As..." />
            <dxb:BarSplitButtonItem x:Name="saveBarSplitButtonItem"
                Glyph="pack://application:,,,/Image/save-16x16.png" 
                LargeGlyph="pack://application:,,,/Image/save-32x32.png"
                Content="Save">
                <dxb:BarSplitButtonItem.PopupControl >
                    <dxb:PopupMenuInfo>
                        <dxb:PopupMenuInfo.ItemLinks>
                            <dxb:BarItemLink BarItemName="saveBarButtonItem"   />
                            <dxb:BarItemLink BarItemName="saveAsBarButtonItem" />
                        </dxb:PopupMenuInfo.ItemLinks>
                    </dxb:PopupMenuInfo>
                </dxb:BarSplitButtonItem.PopupControl>
            </dxb:BarSplitButtonItem>
            <dxb:BarButtonItem x:Name="cutBarButtonItem"
                Glyph="pack://application:,,,/Image/cut-16x16.png"
                Content="Cut" />
            <dxb:BarButtonItem x:Name="copyBarButtonItem"
                Glyph="pack://application:,,,/Image/copy-16x16.png"
                Content="Copy" />
            <dxb:BarButtonItem x:Name="pasteBarButtonItem"
                Glyph="pack://application:,,,/Image/paste-16x16.png" 
                LargeGlyph="pack://application:,,,/Image/paste-32x32.png"
                Content="Paste" />
            <dxb:BarButtonItem x:Name="clearBarButtonItem"
                Glyph="pack://application:,,,/Image/delete-16x16.png"
                Content="Clear" />
            <dxb:BarCheckItem x:Name="boldBarCheckItem"
                Glyph="pack://application:,,,/Image/bold-16x16.png"
                Content="Bold" />
            <dxb:BarCheckItem x:Name="italicBarCheckItem"
                Glyph="pack://application:,,,/Image/italic-16x16.png"
                Content="Italic" />
            <dxb:BarCheckItem Name="underlineBarCheckItem"
                Glyph="pack://application:,,,/Image/underline-16x16.png"
                Content="Underline" />
            <dxb:BarButtonItem x:Name="aboutBarButtonItem"
                Glyph="pack://application:,,,/Image/info-16x16.png" 
                LargeGlyph="pack://application:,,,/Image/info-32x32.png" 
                Content="About"
                ItemClick="aboutBarButtonItem_ItemClick" />
            <dxr:BarButtonGroup Name="fontShapeBarButtonGroup">
                <dxr:BarButtonGroup.ItemLinks>
                    <dxb:BarItemLink BarItemName="boldBarCheckItem"      />
                    <dxb:BarItemLink BarItemName="italicBarCheckItem"    />
                    <dxb:BarItemLink BarItemName="underlineBarCheckItem" />
                </dxr:BarButtonGroup.ItemLinks>
            </dxr:BarButtonGroup>
            <dxb:BarEditItem x:Name="fontSizeBarEditItem"
                EditWidth="60" 
                Content="Font Size : "
                EditValueChanged="fontSizeBarEditItem_EditValueChanged">
                <dxb:BarEditItem.EditSettings>
                    <dxe:ComboBoxEditSettings
                        IsTextEditable="False"
                        PopupMaxHeight="250" />
                </dxb:BarEditItem.EditSettings>
            </dxb:BarEditItem>
            <dxr:RibbonGalleryBarItem x:Name="fontRibbonGalleryBarItem">
                <dxr:RibbonGalleryBarItem.Links>
                    <dxb:BarItemLink BarItemName="fontSizeBarEditItem" />
                </dxr:RibbonGalleryBarItem.Links>
                <dxr:RibbonGalleryBarItem.Gallery>
                    <dxb:Gallery x:Name="fontFamilyGallery"
                        MinColCount="1"
                        ColCount="5" 
                        ItemCheckMode="Single" 
                        IsGroupCaptionVisible="False" 
                        IsItemCaptionVisible="False"
                        IsItemDescriptionVisible="False" 
                        ItemChecked="fontFamilyGallery_ItemChecked" 
                        ItemCaptionVerticalAlignment="Center">
                        <dxb:Gallery.Groups>
                            <dxb:GalleryItemGroup x:Name="fontFamilyGalleryItemGroup" />
                        </dxb:Gallery.Groups>
                    </dxb:Gallery>
                </dxr:RibbonGalleryBarItem.Gallery>
                <dxr:RibbonGalleryBarItem.DropDownGallery>
                    <dxb:Gallery
                        MinColCount="1"
                        ColCount="1" 
                        RowCount="10" 
                        ItemCheckMode="Single" 
                        IsGroupCaptionVisible="False"
                        IsItemGlyphVisible="True" 
                        IsItemDescriptionVisible="False" 
                        ItemContentHorizontalAlignment="Stretch"
                        ItemContentVerticalAlignment="Center" 
                        ItemChecked="fontFamilyGallery_ItemChecked"
                        SizeMode="Vertical">
                        <dxb:Gallery.Groups>
                            <dxb:GalleryItemGroup Name="fontFamilyDropDownGalleryItemGroup" />
                        </dxb:Gallery.Groups>
                    </dxb:Gallery>
                </dxr:RibbonGalleryBarItem.DropDownGallery>
            </dxr:RibbonGalleryBarItem>
            <dxb:BarEditItem x:Name="ribbonStyleBarEditItem"
                Content="Ribbon Style :" 
                EditWidth="100"
                ClosePopupOnChangingEditValue="True"
                EditValue="{Binding ElementName=ribbonControl, Path=RibbonStyle}">
                <dxb:BarEditItem.EditSettings>
                    <dxe:ComboBoxEditSettings
                        IsTextEditable="False"
                        PopupMaxHeight="250">
                        <dxe:ComboBoxEditSettings.Items>
                            <dxr:RibbonStyle>Office2010</dxr:RibbonStyle>
                            <dxr:RibbonStyle>Office2007</dxr:RibbonStyle>
                        </dxe:ComboBoxEditSettings.Items>
                    </dxe:ComboBoxEditSettings>
                </dxb:BarEditItem.EditSettings>
            </dxb:BarEditItem>
            <dxb:BarStaticItem x:Name="positionBarStaticItem"
                ItemMinWidth="150" />
            <dxb:BarStaticItem x:Name="fileNameBarStaticItem"
                ItemMinWidth="150"
                AutoSizeMode="Fill"
                Content="Binding" />
        </dxb:BarManager.Items>
        <DockPanel>
            <dxr:RibbonControl x:Name="ribbonControl"
                DockPanel.Dock="Top" 
                ApplicationButtonLargeIcon="pack://application:,,,/Image/ribbon-application-32x32.png" 
                ApplicationButtonText="File" 
                PageCategoryAlignment="Right">
                <dxr:RibbonControl.ApplicationMenu>
                    <dxr:ApplicationMenu
                        ShowRightPane="True"
                        RightPaneWidth="280">
                        <dxr:ApplicationMenu.ItemLinks>
                            <dxb:BarItemLink BarItemName="newBarButtonItem"       />
                            <dxb:BarItemLink BarItemName="openBarButtonItem"      />
                            <dxb:BarItemLinkSeparator/>
                            <dxb:BarItemLink BarItemName="saveBarSplitButtonItem" />
                            <dxb:BarItemLink BarItemName="printBarButtonItem"     />
                            <dxb:BarItemLinkSeparator/>
                            <dxb:BarItemLink BarItemName="aboutBarButtonItem"     />
                        </dxr:ApplicationMenu.ItemLinks>
                        <dxr:ApplicationMenu.RightPane>
                            <Border
                                BorderThickness="1 0 0 0"
                                BorderBrush="LightGray"
                                Background="White">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="*"    />
                                    </Grid.RowDefinitions>
                                    <Border
                                        Margin="7 5 5 0"
                                        BorderThickness="0 0 0 1"
                                        BorderBrush="LightGray">
                                        <Label FontWeight="Bold">Recent Documents :</Label>
                                    </Border>
                                    <ListBox Grid.Row="1"
                                        Margin="2 0 0 0"
                                        BorderThickness="0">
                                        <ListBox.ItemTemplate>
                                            <DataTemplate>
                                                <StackPanel
                                                    Margin="0 2 0 0"
                                                    Orientation="Horizontal">
                                                    <Border
                                                        BorderThickness="0 0 0 1"
                                                        BorderBrush="Black">
                                                        <TextBlock Text="{Binding Number}" />
                                                    </Border>
                                                    <TextBlock
                                                        Margin="7 0 0 0"
                                                        Text="{Binding FileName}" />
                                                </StackPanel>
                                            </DataTemplate>
                                        </ListBox.ItemTemplate>
                                        <ListBox.Items>
                                            <local:RecentItem Number="1" FileName="Document4.rtf" />
                                            <local:RecentItem Number="2" FileName="Document3.rtf" />
                                            <local:RecentItem Number="3" FileName="Document2.rtf" />
                                            <local:RecentItem Number="4" FileName="Document1.rtf" />
                                        </ListBox.Items>
                                    </ListBox>
                                </Grid>
                            </Border>
                        </dxr:ApplicationMenu.RightPane>
                        <dxr:ApplicationMenu.BottomPane>
                            <StackPanel Orientation="Horizontal">
                                <Button
                                    Margin="0 0 10 0"
                                    Width="100"
                                    Content="Options"
                                    Click="optionButton_Click" />
                                <Button
                                    Width="100"
                                    Content="Exit"
                                    Click="exitButton_Click" />
                            </StackPanel>
                        </dxr:ApplicationMenu.BottomPane>
                    </dxr:ApplicationMenu>
                </dxr:RibbonControl.ApplicationMenu>
                <dxr:RibbonControl.PageHeaderItemLinks>
                    <dxb:BarItemLink BarItemName="ribbonStyleBarEditItem"/>
                </dxr:RibbonControl.PageHeaderItemLinks>
                <dxr:RibbonControl.ToolbarItemLinks>
                    <dxb:BarItemLink BarItemName="openBarButtonItem" />
                    <dxb:BarItemLink BarItemName="saveBarButtonItem" />
                </dxr:RibbonControl.ToolbarItemLinks>
                <dxr:RibbonDefaultPageCategory>
                    <dxr:RibbonPage Caption="Home">
                        <dxr:RibbonPageGroup x:Name="fileRibbonPageGroup"
                            Caption="File"
                            CaptionButtonClick="fileRibbonPageGroup_CaptionButtonClick">
                            <dxb:BarItemLink
                                BarItemName="newBarButtonItem"
                                RibbonStyle="Large" />
                            <dxb:BarItemLink
                                BarItemName="openBarButtonItem"
                                RibbonStyle="SmallWithText" />
                            <dxb:BarItemLink
                                BarItemName="closeBarButtonItem"
                                RibbonStyle="SmallWithText" />
                            <dxb:BarItemLink
                                BarItemName="printBarButtonItem"
                                RibbonStyle="SmallWithText" />
                            <dxb:BarItemLinkSeparator />
                            <dxb:BarItemLink
                                BarItemName="saveBarSplitButtonItem"
                                RibbonStyle="Large" />
                        </dxr:RibbonPageGroup>
                        <dxr:RibbonPageGroup x:Name="editRibbonPageGroup"
                            Caption="Edit"
                            CaptionButtonClick="editRibbonPageGroup_CaptionButtonClick">
                            <dxb:BarItemLink
                                BarItemName="pasteBarButtonItem"
                                RibbonStyle="Large" />
                            <dxb:BarItemLink
                                BarItemName="cutBarButtonItem"
                                RibbonStyle="SmallWithText" />
                            <dxb:BarItemLink
                                BarItemName="copyBarButtonItem"
                                RibbonStyle="SmallWithText" />
                            <dxb:BarItemLink
                                BarItemName="clearBarButtonItem"
                                RibbonStyle="SmallWithText" />
                        </dxr:RibbonPageGroup>
                        <dxr:RibbonPageGroup x:Name="formatRibbonPageGroup"
                            ShowCaptionButton="False"
                            Caption="Format">
                            <dxb:BarItemLink
                                BarItemName="fontShapeBarButtonGroup"
                                RibbonStyle="SmallWithoutText" />
                        </dxr:RibbonPageGroup>
                    </dxr:RibbonPage>
                </dxr:RibbonDefaultPageCategory>
                <dxr:RibbonPageCategory x:Name="selectionRibbonPageCategory"
                    IsVisible="False"
                    Color="Yellow"
                    Caption="Selection">
                    <dxr:RibbonPage Caption="Gallery Page">
                        <dxr:RibbonPageGroup
                            ShowCaptionButton="False"
                            Caption="Font">
                            <dxb:BarItemLink BarItemName="fontRibbonGalleryBarItem" />
                            <dxb:BarItemLink
                                BarItemName="fontSizeBarEditItem"
                                RibbonStyle="SmallWithoutText" />
                        </dxr:RibbonPageGroup>
                    </dxr:RibbonPage>
                </dxr:RibbonPageCategory>
            </dxr:RibbonControl>
            <dxr:RibbonStatusBarControl x:Name="ribbonStatusBarControl"
                DockPanel.Dock="Bottom"
                IsSizeGripVisible="True">
                <dxr:RibbonStatusBarControl.RightItemLinks>
                    <dxb:BarItemLink BarItemName="fileNameBarStaticItem" />
                </dxr:RibbonStatusBarControl.RightItemLinks>
                <dxr:RibbonStatusBarControl.LeftItemLinks>
                     <dxb:BarItemLink BarItemName="positionBarStaticItem" />
                </dxr:RibbonStatusBarControl.LeftItemLinks>
            </dxr:RibbonStatusBarControl>
            <RichTextBox x:Name="richTextBox"
                DockPanel.Dock="Top"
                BorderThickness="0"
                SelectionChanged="richTextBox_SelectionChanged"/>
        </DockPanel>
    </dxb:BarManager>
</dxr:DXRibbonWindow>

 

728x90

 

▶ MainWindow.xaml.cs

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

using DevExpress.Xpf.Bars;
using DevExpress.Xpf.Editors.Settings;
using DevExpress.Xpf.Ribbon;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : DXRibbonWindow
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 포맷 텍스트
        /// </summary>
        private FormattedText formattedText = null;

        /// <summary>
        /// 호출 보류 여부
        /// </summary>
        private bool isInvokePending = false;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 선택 텍스트 널 또는 빈 문자열 여부 - SelectedTextIsNullOrEmpty

        /// <summary>
        /// 선택 텍스트 널 또는 빈 문자열 여부
        /// </summary>
        public bool SelectedTextIsNullOrEmpty
        {
            get
            {
                return string.IsNullOrEmpty(this.richTextBox.Selection.Text);
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            this.fileNameBarStaticItem.Content = "Document 1";

            ((ComboBoxEditSettings)this.fontSizeBarEditItem.EditSettings).ItemsSource = (new FontSizeList()).ItemArray;

            InitializeFontFamilyGallery();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Protected
        //////////////////////////////////////////////////////////////////////////////// Function

        #region 포맷 갱신하기 - UpdateFormat()

        /// <summary>
        /// 포맷 갱신하기
        /// </summary>
        protected void UpdateFormat()
        {
            object value = this.richTextBox.Selection.GetPropertyValue(TextElement.FontSizeProperty);

            this.fontSizeBarEditItem.EditValue = (value == DependencyProperty.UnsetValue) ? null : value;
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region About 바 버튼 항목 항목 클릭시 처리하기 - aboutBarButtonItem_ItemClick(sender, e)

        /// <summary>
        /// About 바 버튼 항목 항목 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void aboutBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            MessageBox.Show("This is About Window", "About Window");
        }

        #endregion
        #region Option 버튼 클릭시 처리하기 - optionButton_Click(sender, e)

        /// <summary>
        /// Option 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void optionButton_Click(object sender, RoutedEventArgs e)
        {
            (this.ribbonControl.ApplicationMenu as ApplicationMenu).ClosePopup();
        }

        #endregion
        #region Exit 버튼 클릭시 처리하기 - exitButton_Click(sender, e)

        /// <summary>
        /// Exit 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void exitButton_Click(object sender, RoutedEventArgs e)
        {
            (this.ribbonControl.ApplicationMenu as ApplicationMenu).ClosePopup();
        }

        #endregion
        #region File 리본 페이지 그룹 제목 버튼 클릭시 처리하기 - fileRibbonPageGroup_CaptionButtonClick(sender, e)

        /// <summary>
        /// File 리본 페이지 그룹 제목 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void fileRibbonPageGroup_CaptionButtonClick(object sender, EventArgs e)
        {
            MessageBox.Show("This is File Open dialog", "File Open Dialog");
        }

        #endregion
        #region Edit 리본 페이지 그룹 제목 버튼 클릭시 처리하기 - editRibbonPageGroup_CaptionButtonClick(sender, e)

        /// <summary>
        /// Edit 리본 페이지 그룹 제목 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void editRibbonPageGroup_CaptionButtonClick(object sender, EventArgs e)
        {
            MessageBox.Show("This is Edit Settings dialog", "Edit Settings Dialog");
        }

        #endregion
        #region 리치 텍스트 박스 선택 변경시 처리하기 - richTextBox_SelectionChanged(sender, e)

        /// <summary>
        /// 리치 텍스트 박스 선택 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void richTextBox_SelectionChanged(object sender, RoutedEventArgs e)
        {
            ShowSelectionRibbonPageCategory();

            UpdatePositionBarStaticItem();

            InvokeUpdateFormat();
        }

        #endregion
        #region 폰트 패밀리 갤러리 항목 체크시 처리하기 - fontFamilyGallery_ItemChecked(sender, e)

        /// <summary>
        /// 폰트 패밀리 갤러리 항목 체크시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void fontFamilyGallery_ItemChecked(object sender, GalleryItemEventArgs e)
        {
            FontFamily fontFamily = e.Item.Tag as FontFamily;

            ApplyPropertyValue(this.richTextBox, TextElement.FontFamilyProperty, fontFamily);
        }

        #endregion
        #region 폰트 크기 바 에디트 항목 편집 값 변경시 처리하기 - fontSizeBarEditItem_EditValueChanged(sender, e)

        /// <summary>
        /// 폰트 크기 바 에디트 항목 편집 값 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void fontSizeBarEditItem_EditValueChanged(object sender, RoutedEventArgs e)
        {
            ApplyPropertyValue(this.richTextBox, TextElement.FontSizeProperty, this.fontSizeBarEditItem.EditValue);
        }

        #endregion

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

        #region 포맷 텍스트 생성하기 - CreateFormattedText(fontFamily)

        /// <summary>
        /// 포맷 텍스트 생성하기
        /// </summary>
        /// <param name="fontFamily">폰트 패밀리</param>
        /// <returns>포맷 텍스트</returns>
        private FormattedText CreateFormattedText(FontFamily fontFamily)
        {
            return new FormattedText
            (
                "Aa",
                CultureInfo.CurrentCulture,
                FlowDirection.LeftToRight,
                new Typeface(fontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
                18,
                Brushes.Black,
                null,
                TextFormattingMode.Ideal
            );
        }

        #endregion
        #region 이미지 생성하기 - CreateImage(fontFamily)

        /// <summary>
        /// 이미지 생성하기
        /// </summary>
        /// <param name="fontFamily">폰트 패밀리</param>
        /// <returns>이미지 소스</returns>
        private ImageSource CreateImage(FontFamily fontFamily)
        {
            const double dimensionSize     = 32d;
            const double halfDimensionSize = dimensionSize / 2d;

            DrawingVisual  drawingVisual  = new DrawingVisual();
            DrawingContext drawingContext = drawingVisual.RenderOpen();

            drawingContext.DrawRectangle(Brushes.White, null, new Rect(0d, 0d, dimensionSize, dimensionSize));

            if(this.formattedText == null)
            {
                this.formattedText = CreateFormattedText(fontFamily);
            }

            this.formattedText.SetFontFamily(fontFamily);

            this.formattedText.TextAlignment = TextAlignment.Center;

            double verticalOffset = (dimensionSize - this.formattedText.Baseline) / 2d;

            drawingContext.DrawText(this.formattedText, new Point(halfDimensionSize, verticalOffset));

            drawingContext.Close();

            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int)dimensionSize, (int)dimensionSize, 96, 96, PixelFormats.Pbgra32);

            renderTargetBitmap.Render(drawingVisual);

            return renderTargetBitmap;
        }

        #endregion
        #region 폰트 패밀리 갤러기 초기화 하기 - InitializeFontFamilyGallery()

        /// <summary>
        /// 폰트 패밀리 갤러기 초기화 하기
        /// </summary>
        private void InitializeFontFamilyGallery()
        {
            foreach(FontFamily fontFamily in FontFamilyList.FontFamilyCollection)
            {
                ImageSource imageSource = CreateImage(fontFamily);

                this.fontFamilyGalleryItemGroup.Items.Add(CreateGalleryItem(fontFamily, imageSource));

                this.fontFamilyDropDownGalleryItemGroup.Items.Add(CreateGalleryItem(fontFamily, imageSource));
            }
        }

        #endregion
        #region 갤러리 항목 생성하기 - CreateGalleryItem(fontFamily, imageSource)

        /// <summary>
        /// 갤러리 항목 생성하기
        /// </summary>
        /// <param name="fontFamily">폰트 패밀리</param>
        /// <param name="imageSource">이미지 소스</param>
        /// <returns>갤러리 아이템</returns>
        private GalleryItem CreateGalleryItem(FontFamily fontFamily, ImageSource imageSource)
        {
            GalleryItem galleryItem = new GalleryItem();

            galleryItem.Glyph   = imageSource;
            galleryItem.Caption = fontFamily.ToString();
            galleryItem.Tag     = fontFamily;

            return galleryItem;
        }

        #endregion
        #region Selection 리본 페이지 카테고리 보여주기 - ShowSelectionRibbonPageCategory()

        /// <summary>
        /// Selection 리본 페이지 카테고리 보여주기
        /// </summary>
        private void ShowSelectionRibbonPageCategory()
        {
            if(this.richTextBox == null)
            {
                this.selectionRibbonPageCategory.IsVisible = false;
            }
            else
            {
                this.selectionRibbonPageCategory.IsVisible = !SelectedTextIsNullOrEmpty;
            }

            if(this.selectionRibbonPageCategory.IsVisible)
            {
                this.ribbonControl.SelectedPage = this.selectionRibbonPageCategory.Pages[0];
            }
        }

        #endregion
        #region 속성 값 적용하기 - ApplyPropertyValue(richTextBox, dependencyProperty, value)

        /// <summary>
        /// 속성 값 적용하기
        /// </summary>
        /// <param name="richTextBox">RichTextBox</param>
        /// <param name="dependencyProperty">의존 속성</param>
        /// <param name="value">값</param>
        private void ApplyPropertyValue(RichTextBox richTextBox, DependencyProperty dependencyProperty, object value)
        {
            if(value == null)
            {
                return;
            }

            richTextBox.Selection.ApplyPropertyValue(dependencyProperty, value);
        }

        #endregion
        #region 위치 바 정적 항목 갱신하기 - UpdatePositionBarStaticItem()

        /// <summary>
        /// 위치 바 정적 항목 갱신하기
        /// </summary>
        private void UpdatePositionBarStaticItem()
        {
            int line = 0;

            this.richTextBox.CaretPosition.GetLineStartPosition(-100000, out line);

            int column = this.richTextBox.CaretPosition.GetOffsetToPosition(this.richTextBox.CaretPosition.GetLineStartPosition(0));

            this.positionBarStaticItem.Content = "Line : " + (-line).ToString() + "  Position : " + (-column).ToString();
        }

        #endregion
        #region 포맷 갱신 호출하기 - InvokeUpdateFormat()

        /// <summary>
        /// 포맷 갱신 호출하기
        /// </summary>
        private void InvokeUpdateFormat()
        {
            if(!this.isInvokePending)
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(UpdateFormat));

                this.isInvokePending = true;
            }

            UpdateFormat();
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요