728x90
반응형
728x170
▶ CustomTextBlock.cs
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Media;
namespace TestProject
{
/// <summary>
/// 커스텀 텍스트 블럭
/// </summary>
public class CustomTextBlock : FrameworkElement
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region 폰트 패밀리 속성 - FontFamilyProperty
/// <summary>
/// 폰트 패밀리 속성
/// </summary>
public static readonly DependencyProperty FontFamilyProperty = DependencyProperty.Register
(
"FontFamily",
typeof(FontFamily),
typeof(CustomTextBlock),
new FrameworkPropertyMetadata
(
new FontFamily("나눔고딕코딩"),
FrameworkPropertyMetadataOptions.AffectsRender
)
);
#endregion
#region 폰트 크기 속성 - FontSizeProperty
/// <summary>
/// 폰트 크기 속성
/// </summary>
public static readonly DependencyProperty FontSizeProperty = DependencyProperty.Register
(
"FontSize",
typeof(double),
typeof(CustomTextBlock),
new FrameworkPropertyMetadata
(
(double)11,
FrameworkPropertyMetadataOptions.AffectsRender
)
);
#endregion
#region 폰트 확장 속성 - FontStretchProperty
/// <summary>
/// 폰트 확장 속성
/// </summary>
public static readonly DependencyProperty FontStretchProperty = DependencyProperty.Register
(
"FontStretch",
typeof(double),
typeof(CustomTextBlock),
new FrameworkPropertyMetadata
(
(double)1,
FrameworkPropertyMetadataOptions.AffectsRender
)
);
#endregion
#region 전경 브러시 속성 - ForegroundProperty
/// <summary>
/// 전경 브러시 속성
/// </summary>
public static readonly DependencyProperty ForegroundProperty = DependencyProperty.Register
(
"Foreground",
typeof(Brush),
typeof(CustomTextBlock),
new FrameworkPropertyMetadata
(
Brushes.Black,
FrameworkPropertyMetadataOptions.AffectsRender
)
);
#endregion
#region 텍스트 속성 - TextProperty
/// <summary>
/// 텍스트 속성
/// </summary>
public static readonly DependencyProperty TextProperty = DependencyProperty.Register
(
"Text",
typeof(string),
typeof(CustomTextBlock),
new FrameworkPropertyMetadata
(
"",
FrameworkPropertyMetadataOptions.AffectsRender
)
);
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 폰트 패밀리 - FontFamily
/// <summary>
/// 폰트 패밀리
/// </summary>
public FontFamily FontFamily
{
get
{
return (FontFamily)GetValue(FontFamilyProperty);
}
set
{
SetValue(FontFamilyProperty, value);
InvalidateVisual();
}
}
#endregion
#region 폰트 크기 - FontSize
/// <summary>
/// 폰트 크기
/// </summary>
public double FontSize
{
get
{
return (double)GetValue(FontSizeProperty);
}
set
{
SetValue(FontSizeProperty, value);
Height = value;
}
}
#endregion
#region 폰트 확장 - FontStretch
/// <summary>
/// 폰트 확장
/// </summary>
public Double FontStretch
{
get
{
return (double)GetValue(FontStretchProperty);
}
set
{
SetValue(FontStretchProperty, value);
InvalidateVisual();
}
}
#endregion
#region 전경 브러시 - Foreground
/// <summary>
/// 전경 브러시
/// </summary>
public Brush Foreground
{
get
{
return (Brush)GetValue(ForegroundProperty);
}
set
{
SetValue(ForegroundProperty, value);
InvalidateVisual();
}
}
#endregion
#region 텍스트 - Text
/// <summary>
/// 텍스트
/// </summary>
public String Text
{
get
{
return (string)GetValue(TextProperty);
}
set
{
SetValue(TextProperty, value);
InvalidateVisual();
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Protected
#region 측정하기 (오바라이드용) - MeasureOverride(availableSize)
/// <summary>
/// 측정하기 (오바라이드용)
/// </summary>
/// <param name="availableSize">이용 가능한 크기</param>
/// <returns>측정 크기</returns>
protected override Size MeasureOverride(Size availableSize)
{
availableSize.Height = FontSize * 1.2;
return availableSize;
}
#endregion
#region 렌더링시 처리하기 - OnRender(drawingContext)
/// <summary>
/// 렌더링시 처리하기
/// </summary>
/// <param name="drawingContext">드로잉 컨텍스트</param>
protected override void OnRender(DrawingContext drawingContext)
{
double offsetX = 0;
if(Text == null)
{
return;
}
foreach(char character in Text)
{
FormattedText formattedText = new FormattedText
(
character.ToString(),
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface
(
FontFamily,
FontStyles.Normal,
FontWeights.Normal,
FontStretches.Normal
),
FontSize,
Foreground
);
drawingContext.DrawText(formattedText, new Point(offsetX, 0));
offsetX += formattedText.WidthIncludingTrailingWhitespace * FontStretch / 100;
}
}
#endregion
}
}
728x90
▶ 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"
xmlns:local="clr-namespace:TestProject"
Width="800"
Height="600"
Title="FrameworkElement 클래스 : 장평을 설정하는 커스텀 텍스트 블럭 사용하기"
FontFamily="나눔고딕코딩"
FontSize="16">
<Grid>
<StackPanel>
<StackPanel.Resources>
<Style TargetType="{x:Type local:CustomTextBlock}">
<Setter Property="Text" Value="테스트 문자열 입니다."/>
<Setter Property="FontSize" Value="25" />
<Setter Property="FontFamily" Value="나눔고딕코딩" />
</Style>
</StackPanel.Resources>
<TextBlock Text="장평 120" />
<local:CustomTextBlock FontStretch="120" />
<TextBlock Text="장평 110" />
<local:CustomTextBlock FontStretch="110" />
<TextBlock Text="장평 100" />
<local:CustomTextBlock FontStretch="100" />
<TextBlock Text="장평90" />
<local:CustomTextBlock FontStretch="90" />
<TextBlock Text="장평 80" />
<local:CustomTextBlock FontStretch="80" />
</StackPanel>
</Grid>
</Window>
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] Canvas 클래스 : 사각형 도형 추가하기 (0) | 2020.12.26 |
---|---|
[C#/WPF] FFMpeg을 사용해 웹 카메라 사용하기 (기능 개선) (0) | 2020.12.25 |
[C#/WPF] ItemsControl 클래스 : 리스트 컨트롤 사용하기 (드래그 기능 개선) (0) | 2020.12.19 |
[C#/WPF] MarkupExtension 클래스 : 마크업 확장 사용하기 (0) | 2020.12.15 |
[C#/WPF] Image 클래스 : LayoutUpdated 이벤트를 사용해 이미지 픽셀 보정하기 (0) | 2020.12.13 |
[C#/WPF] Grid 클래스 : 환형 패널 사용하기 (0) | 2020.12.12 |
[C#/WPF] ObjectDataProvider 엘리먼트 : Colors 클래스의 색상 정적 속성 구하기 (0) | 2020.12.07 |
[C#/WPF] 파노라마 뷰 사용하기 (0) | 2020.12.06 |
[C#/WPF] TabControl 클래스 : FADE IN/OUT 탭 컨트롤 사용하기 (0) | 2020.12.06 |
[C#/WPF] FrameworkElement 엘리먼트 : FocusVisualStyle 속성을 사용해 포커스 사각형 제거하기 (0) | 2020.12.05 |
댓글을 달아 주세요