728x90
반응형
728x170
▶ BitmapElement.cs
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace TestProject
{
/// <summary>
/// 비트맵 엘리먼트
/// </summary>
public class BitmapElement : UIElement
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Event
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 비트맵 실패시 - BitmapFailed
/// <summary>
/// 비트맵 실패시
/// </summary>
public event EventHandler<ExceptionEventArgs> BitmapFailed;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region 소스 속성 - SourceProperty
/// <summary>
/// 소스 속성
/// </summary>
public static readonly DependencyProperty SourceProperty = DependencyProperty.Register
(
"Source",
typeof(BitmapSource),
typeof(BitmapElement),
new FrameworkPropertyMetadata
(
null,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure,
new PropertyChangedCallback(SourcePropertyChangedCallback)
)
);
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 비트맵 소스 다운로드 완료시 이벤트 핸들러
/// </summary>
private EventHandler bitmapSourceDownloadCompletedEventHandler;
/// <summary>
/// 비트맵 소스 다운로드 실패시 이벤트 핸들러
/// </summary>
private EventHandler<ExceptionEventArgs> bitmapSourceDownloadFailedEventHandler;
/// <summary>
/// 픽셀 오프셋
/// </summary>
private Point pixelOffset;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 소스 - Source
/// <summary>
/// 소스
/// </summary>
public BitmapSource Source
{
get
{
return (BitmapSource)GetValue(SourceProperty);
}
set
{
SetValue(SourceProperty, value);
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - BitmapElement()
/// <summary>
/// 생성자
/// </summary>
public BitmapElement()
{
this.bitmapSourceDownloadCompletedEventHandler = new EventHandler(bitmapSource_DownloadCompleted);
this.bitmapSourceDownloadFailedEventHandler = new EventHandler<ExceptionEventArgs>(bitmapSource_DownloadFailed);
LayoutUpdated += UIElement_LayoutUpdated;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Private
#region 소스 속성 변경시 콜백 처리하기 - SourcePropertyChangedCallback(d, e)
/// <summary>
/// 소스 속성 변경시 콜백 처리하기
/// </summary>
/// <param name="d">의존 객체</param>
/// <param name="e">이벤트 인자</param>
private static void SourcePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
BitmapElement bitmapElement = (BitmapElement)d;
BitmapSource oldSource = (BitmapSource)e.OldValue;
BitmapSource newSource = (BitmapSource)e.NewValue;
if((oldSource != null) && (bitmapElement.bitmapSourceDownloadCompletedEventHandler != null) && !oldSource.IsFrozen && (oldSource is BitmapSource))
{
oldSource.DownloadCompleted -= bitmapElement.bitmapSourceDownloadCompletedEventHandler;
oldSource.DownloadFailed -= bitmapElement.bitmapSourceDownloadFailedEventHandler;
}
if((newSource != null) && (newSource is BitmapSource) && !newSource.IsFrozen)
{
newSource.DownloadCompleted += bitmapElement.bitmapSourceDownloadCompletedEventHandler;
newSource.DownloadFailed += bitmapElement.bitmapSourceDownloadFailedEventHandler;
}
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Instance
//////////////////////////////////////////////////////////////////////////////// Protected
#region 측정하기 (코어) - MeasureCore(availableSize)
/// <summary>
/// 측정하기 (코어)
/// </summary>
/// <param name="availableSize">이용 가능한 크기</param>
/// <returns>측정 크기</returns>
protected override Size MeasureCore(Size availableSize)
{
Size measureSize = new Size();
BitmapSource bitmapSource = Source;
if(bitmapSource != null)
{
PresentationSource presentationSource = PresentationSource.FromVisual(this);
if(presentationSource != null)
{
Matrix fromDeviceMartix = presentationSource.CompositionTarget.TransformFromDevice;
Vector pixelSizeVector = new Vector(bitmapSource.PixelWidth, bitmapSource.PixelHeight);
Vector measureSizeVector = fromDeviceMartix.Transform(pixelSizeVector);
measureSize = new Size(measureSizeVector.X, measureSizeVector.Y);
}
}
return measureSize;
}
#endregion
#region 렌더링시 처리하기 - OnRender(context)
/// <summary>
/// 렌더링시 처리하기
/// </summary>
/// <param name="context">드로잉 컨텍스트</param>
protected override void OnRender(DrawingContext context)
{
this.pixelOffset = GetPixelOffset();
BitmapSource bitmapSource = Source;
if(bitmapSource != null)
{
context.DrawImage(bitmapSource, new Rect(this.pixelOffset, DesiredSize));
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Private
////////////////////////////////////////////////////////////////////// Event
#region UI 엘리먼트 레이아웃 업데이트시 처리하기 - UIElement_LayoutUpdated(sender, e)
/// <summary>
/// UI 엘리먼트 레이아웃 업데이트시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void UIElement_LayoutUpdated(object sender, EventArgs e)
{
Point pixelOffset = GetPixelOffset();
if(!IsAreClosed(pixelOffset, this.pixelOffset))
{
InvalidateVisual();
}
}
#endregion
#region 비트맵 소스 다운로드 완료시 처리하기 - bitmapSource_DownloadCompleted(sender, e)
/// <summary>
/// 비트맵 소스 다운로드 완료시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void bitmapSource_DownloadCompleted(object sender, EventArgs e)
{
InvalidateMeasure();
InvalidateVisual();
}
#endregion
#region 비트맵 소스 다운로드 실패시 처리하기 - bitmapSource_DownloadFailed(sender, e)
/// <summary>
/// 비트맵 소스 다운로드 실패시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void bitmapSource_DownloadFailed(object sender, ExceptionEventArgs e)
{
Source = null;
BitmapFailed(this, e);
}
#endregion
////////////////////////////////////////////////////////////////////// Function
#region 비주얼 변환 매트릭스 구하기 - GetVisualTransformMatrix(visual)
/// <summary>
/// 비주얼 변환 매트릭스 구하기
/// </summary>
/// <param name="visual">비주얼</param>
/// <returns>비주얼 변환 매트릭스</returns>
private Matrix GetVisualTransformMatrix(Visual visual)
{
if(visual != null)
{
Matrix matrix = Matrix.Identity;
Transform transform = VisualTreeHelper.GetTransform(visual);
if(transform != null)
{
Matrix transformMatrix = transform.Value;
matrix = Matrix.Multiply(matrix, transformMatrix);
}
Vector offsetVector = VisualTreeHelper.GetOffset(visual);
matrix.Translate(offsetVector.X, offsetVector.Y);
return matrix;
}
return Matrix.Identity;
}
#endregion
#region 비주얼 변환 적용하기 - ApplyVisualTransform(point, visual, inverse, throwException, success)
/// <summary>
/// 비주얼 변환 적용하기
/// </summary>
/// <param name="point">포인트</param>
/// <param name="visual">비주얼</param>
/// <param name="inverse">반전 여부</param>
/// <param name="throwException">예외 발생 여부</param>
/// <param name="success">성공 여부</param>
/// <returns>비주얼 변환 포인트</returns>
private Point ApplyVisualTransform(Point point, Visual visual, bool inverse, bool throwException, out bool success)
{
success = true;
if(visual != null)
{
Matrix matrix = GetVisualTransformMatrix(visual);
if(inverse)
{
if(!throwException && !matrix.HasInverse)
{
success = false;
return new Point(0, 0);
}
matrix.Invert();
}
point = matrix.Transform(point);
}
return point;
}
#endregion
#region 비주얼 변환 적용하기 - ApplyVisualTransform(point, visual, inverse)
/// <summary>
/// 비주얼 변환 적용하기
/// </summary>
/// <param name="point">포인트</param>
/// <param name="visual">비주얼</param>
/// <param name="inverse">반전 여부</param>
/// <returns>비주얼 변환 포인트</returns>
private Point ApplyVisualTransform(Point point, Visual visual, bool inverse)
{
return ApplyVisualTransform(point, visual, inverse, true, out _);
}
#endregion
#region 픽셀 오프셋 구하기 - GetPixelOffset()
/// <summary>
/// 픽셀 오프셋 구하기
/// </summary>
/// <returns>픽셀 오프셋</returns>
private Point GetPixelOffset()
{
Point pixelOffset = new Point();
PresentationSource presentationSource = PresentationSource.FromVisual(this);
if(presentationSource != null)
{
Visual rootVisual = presentationSource.RootVisual;
pixelOffset = TransformToAncestor(rootVisual).Transform(pixelOffset);
pixelOffset = ApplyVisualTransform(pixelOffset, rootVisual, false);
pixelOffset = presentationSource.CompositionTarget.TransformToDevice.Transform(pixelOffset);
pixelOffset.X = Math.Round(pixelOffset.X);
pixelOffset.Y = Math.Round(pixelOffset.Y);
pixelOffset = presentationSource.CompositionTarget.TransformFromDevice.Transform(pixelOffset);
pixelOffset = ApplyVisualTransform(pixelOffset, rootVisual, true);
pixelOffset = rootVisual.TransformToDescendant(this).Transform(pixelOffset);
}
return pixelOffset;
}
#endregion
#region 호 폐쇄 여부 구하기 - IsAreClosed(value1, value2)
/// <summary>
/// 호 폐쇄 여부 구하기
/// </summary>
/// <param name="value1">값 1</param>
/// <param name="value2">값 2</param>
/// <returns>호 폐쇄 여부</returns>
private bool IsAreClosed(double value1, double value2)
{
if(value1 == value2)
{
return true;
}
double delta = value1 - value2;
return (delta < 1.53E-06) && (delta > -1.53E-06);
}
#endregion
#region 호 폐쇄 여부 구하기 - IsAreClosed(point1, point2)
/// <summary>
/// 호 폐쇄 여부 구하기
/// </summary>
/// <param name="point1">포인트 1</param>
/// <param name="point2">포인트 2</param>
/// <returns>호 폐쇄 여부</returns>
private bool IsAreClosed(Point point1, Point point2)
{
return IsAreClosed(point1.X, point2.X) && IsAreClosed(point1.Y, point2.Y);
}
#endregion
}
}
728x90
▶ GDITextQuality.cs
namespace TestProject
{
/// <summary>
/// GDI 텍스트 품질
/// </summary>
public enum GDITextQuality
{
/// <summary>
/// SystemDefault
/// </summary>
/// <remarks>
/// 각 문자는 시스템 기본 렌더링 힌트와 함께 해당 글리프 비트맵을 사용하여 그려집니다.
/// 텍스트는 사용자가 시스템에 대해 선택한 글꼴 다듬기 설정을 사용하여 그려집니다.
/// </remarks>
SystemDefault,
/// <summary>
/// SingleBitPerPixelGridFit
/// </summary>
/// <remarks>
/// 각 문자는 해당 글리프 비트맵을 사용하여 그려집니다.
/// 힌팅은 줄기와 곡률의 문자 모양을 개선하는 데 사용됩니다.
/// </remarks>
SingleBitPerPixelGridFit,
/// <summary>
/// SingleBitPerPixel
/// </summary>
/// <remarks>
/// 각 문자는 해당 글리프 비트맵을 사용하여 그려집니다.
/// 힌트는 사용되지 않습니다.
/// </remarks>
SingleBitPerPixel,
/// <summary>
/// AntiAliasGridFit
/// </summary>
/// <remarks>
/// 각 문자는 힌트가 있는 앤티앨리어싱된 글리프 비트맵을 사용하여 그려집니다.
/// 앤티앨리어싱으로 인해 훨씬 더 나은 품질을 제공하지만 더 높은 성능 비용이 듭니다.
/// </remarks>
AntiAliasGridFit,
/// <summary>
/// AntiAlias
/// </summary>
/// <remarks>
/// 각 문자는 힌트 없이 앤티앨리어싱된 글리프 비트맵을 사용하여 그려집니다.
/// 앤티 앨리어싱으로 인해 더 나은 품질.
/// 힌팅이 꺼져 있기 때문에 줄기 너비 차이가 눈에 띌 수 있습니다.
/// </remarks>
AntiAlias,
/// <summary>
/// ClearTypeGridFit
/// </summary>
/// <remarks>
/// 각 문자는 힌트와 함께 해당 글리프 ClearType 비트맵을 사용하여 그려집니다.
/// 최고 품질의 설정. ClearType 글꼴 기능을 활용하는 데 사용됩니다.
/// </remarks>
ClearTypeGridFit
}
}
반응형
▶ GDITextBlock.cs
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
namespace TestProject
{
/// <summary>
/// GDI 텍스트 블럭
/// </summary>
public class GDITextBlock : FrameworkElement
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region 텍스트 속성 - TextProperty
/// <summary>
/// 텍스트 속성
/// </summary>
public static readonly DependencyProperty TextProperty = TextBlock.TextProperty.AddOwner
(
typeof(GDITextBlock),
new FrameworkPropertyMetadata { AffectsMeasure = true }
);
#endregion
#region 텍스트 품질 속성 - TextQualityProperty
/// <summary>
/// 텍스트 품질 속성
/// </summary>
public static readonly DependencyProperty TextQualityProperty = DependencyProperty.Register
(
"TextQuality",
typeof(GDITextQuality),
typeof(GDITextBlock),
new FrameworkPropertyMetadata
{
DefaultValue = GDITextQuality.SingleBitPerPixelGridFit,
AffectsMeasure = true,
CoerceValueCallback = TextQualityProperty_CoerceValueCallback
}
);
#endregion
#region 텍스트 정렬 속성 - TextAlignmentProperty
/// <summary>
/// 텍스트 정렬 속성
/// </summary>
public static readonly DependencyProperty TextAlignmentProperty = Block.TextAlignmentProperty.AddOwner
(
typeof(GDITextBlock),
new FrameworkPropertyMetadata
{
AffectsMeasure = true,
CoerceValueCallback = TextAlignmentProperty_CoerceValueCallback
}
);
#endregion
#region 텍스트 래핑 속성 - TextWrappingProperty
/// <summary>
/// 텍스트 래핑 속성
/// </summary>
public static readonly DependencyProperty TextWrappingProperty = TextBlock.TextWrappingProperty.AddOwner
(
typeof(GDITextBlock),
new FrameworkPropertyMetadata
{
AffectsMeasure = true,
CoerceValueCallback = TextWrappingProperty_CoerceValueCallback
}
);
#endregion
#region 텍스트 트리밍 속성 - TextTrimmingProperty
/// <summary>
/// 텍스트 트리밍 속성
/// </summary>
public static readonly DependencyProperty TextTrimmingProperty = TextBlock.TextTrimmingProperty.AddOwner
(
typeof(GDITextBlock),
new FrameworkPropertyMetadata { AffectsMeasure = true }
);
#endregion
#region 폰트 패밀리 속성 - FontFamilyProperty
/// <summary>
/// 폰트 패밀리 속성
/// </summary>
public static readonly DependencyProperty FontFamilyProperty = TextElement.FontFamilyProperty.AddOwner
(
typeof(GDITextBlock),
new FrameworkPropertyMetadata { AffectsMeasure = true }
);
#endregion
#region 폰트 스타일 속성 - FontStyleProperty
/// <summary>
/// 폰트 스타일 속성
/// </summary>
public static readonly DependencyProperty FontStyleProperty = TextElement.FontStyleProperty.AddOwner
(
typeof(GDITextBlock),
new FrameworkPropertyMetadata
{
AffectsMeasure = true,
CoerceValueCallback = FontStyleProperty_CoerceValueCallback
}
);
#endregion
#region 폰트 가중치 속성 - FontWeightProperty
/// <summary>
/// 폰트 가중치 속성
/// </summary>
public static readonly DependencyProperty FontWeightProperty = TextElement.FontWeightProperty.AddOwner
(
typeof(GDITextBlock),
new FrameworkPropertyMetadata
{
AffectsMeasure = true,
CoerceValueCallback = FontWeightProperty_CoerceValueCallback
}
);
#endregion
#region 폰트 크기 속성 - FontSizeProperty
/// <summary>
/// 폰트 크기 속성
/// </summary>
public static readonly DependencyProperty FontSizeProperty = TextElement.FontSizeProperty.AddOwner
(
typeof(GDITextBlock),
new FrameworkPropertyMetadata { AffectsMeasure = true }
);
#endregion
#region 전경색 속성 - ForegroundProperty
/// <summary>
/// 전경색 속성
/// </summary>
public static readonly DependencyProperty ForegroundProperty = TextElement.ForegroundProperty.AddOwner
(
typeof(GDITextBlock),
new FrameworkPropertyMetadata
{
AffectsMeasure = true,
CoerceValueCallback = ForegroundProperty_CoerceValueCallback
}
);
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 비트맵 엘리먼트
/// </summary>
private BitmapElement bitmapElement;
/// <summary>
/// 그리기 브러시
/// </summary>
private System.Drawing.Brush drawingBrush;
/// <summary>
/// 그리기 폰트
/// </summary>
private System.Drawing.Font drawingFont;
/// <summary>
/// 그리기 문자열 포맷
/// </summary>
private System.Drawing.StringFormat drawingStringFormat;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 텍스트 - Text
/// <summary>
/// 텍스트
/// </summary>
public string Text
{
get
{
return (string)GetValue(TextProperty);
}
set
{
SetValue(TextProperty, value);
}
}
#endregion
#region 텍스트 품질 - TextQuality
/// <summary>
/// 텍스트 품질
/// </summary>
public GDITextQuality TextQuality
{
get
{
return (GDITextQuality)GetValue(TextQualityProperty);
}
set
{
SetValue(TextQualityProperty, value);
}
}
#endregion
#region 텍스트 정렬 - TextAlignment
/// <summary>
/// 텍스트 정렬
/// </summary>
public TextAlignment TextAlignment
{
get
{
return (TextAlignment)GetValue(TextAlignmentProperty);
}
set
{
SetValue(TextAlignmentProperty, value);
}
}
#endregion
#region 텍스트 래핑 - TextWrapping
/// <summary>
/// 텍스트 래핑
/// </summary>
public TextWrapping TextWrapping
{
get
{
return (TextWrapping)GetValue(TextWrappingProperty);
}
set
{
SetValue(TextWrappingProperty, value);
}
}
#endregion
#region 텍스트 트리밍 - TextTrimming
/// <summary>
/// 텍스트 트리밍
/// </summary>
public TextTrimming TextTrimming
{
get
{
return (TextTrimming)GetValue(TextTrimmingProperty);
}
set
{
SetValue(TextTrimmingProperty, value);
}
}
#endregion
#region 폰트 패밀리 - FontFamily
/// <summary>
/// 폰트 패밀리
/// </summary>
public FontFamily FontFamily
{
get
{
return (FontFamily)GetValue(FontFamilyProperty);
}
set
{
SetValue(FontFamilyProperty, value);
}
}
#endregion
#region 폰트 스타일 - FontStyle
/// <summary>
/// 폰트 스타일
/// </summary>
public FontStyle FontStyle
{
get
{
return (FontStyle)GetValue(FontStyleProperty);
}
set
{
SetValue(FontStyleProperty, value);
}
}
#endregion
#region 폰트 가중치 - FontWeight
/// <summary>
/// 폰트 가중치
/// </summary>
public FontWeight FontWeight
{
get
{
return (FontWeight)GetValue(FontWeightProperty);
}
set
{
SetValue(FontWeightProperty, value);
}
}
#endregion
#region 폰트 크기 - FontSize
/// <summary>
/// 폰트 크기
/// </summary>
[TypeConverter(typeof(FontSizeConverter))]
public double FontSize
{
get
{
return (double)GetValue(FontSizeProperty);
}
set
{
SetValue(FontSizeProperty, value);
}
}
#endregion
#region 전경색 - Foreground
/// <summary>
/// 전경색
/// </summary>
public SolidColorBrush Foreground
{
get
{
return (SolidColorBrush)GetValue(ForegroundProperty);
}
set
{
SetValue(ForegroundProperty, value);
}
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Proptected
#region 비주얼 자식 수 - VisualChildrenCount
/// <summary>
/// 비주얼 자식 수
/// </summary>
protected override int VisualChildrenCount
{
get
{
return 1;
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Private
#region 텍스트 품질 속성 값 강제 콜백 처리하기 - TextQualityProperty_CoerceValueCallback(d, baseValue)
/// <summary>
/// 텍스트 품질 속성 값 강제 콜백 처리하기
/// </summary>
/// <param name="d">의존 객체</param>
/// <param name="baseValue">기본 값</param>
/// <returns>처리 결과</returns>
private static object TextQualityProperty_CoerceValueCallback(DependencyObject d, object baseValue)
{
if(!Enum.IsDefined(typeof(GDITextQuality), baseValue))
{
baseValue = TextQualityProperty.DefaultMetadata.DefaultValue;
}
return baseValue;
}
#endregion
#region 텍스트 정렬 속성 값 강제 콜백 처리하기 - TextAlignmentProperty_CoerceValueCallback(d, baseValue)
/// <summary>
/// 텍스트 정렬 속성 값 강제 콜백 처리하기
/// </summary>
/// <param name="d">의존 객체</param>
/// <param name="baseValue">기본 값</param>
/// <returns>처리 결과</returns>
private static object TextAlignmentProperty_CoerceValueCallback(DependencyObject d, object baseValue)
{
if(baseValue.Equals(TextAlignment.Justify))
{
baseValue = TextAlignmentProperty.DefaultMetadata.DefaultValue;
}
return baseValue;
}
#endregion
#region 텍스트 래핑 속성 값 강제 콜백 처리하기 - TextWrappingProperty_CoerceValueCallback(d, baseValue)
/// <summary>
/// 텍스트 래핑 속성 값 강제 콜백 처리하기
/// </summary>
/// <param name="d">의존 객체</param>
/// <param name="baseValue">기본 값</param>
/// <returns>처리 결과</returns>
private static object TextWrappingProperty_CoerceValueCallback(DependencyObject d, object baseValue)
{
if(baseValue.Equals(TextWrapping.WrapWithOverflow))
{
baseValue = TextWrapping.Wrap;
}
return baseValue;
}
#endregion
#region 폰트 스타일 속성 값 강제 콜백 처리하기 - FontStyleProperty_CoerceValueCallback(d, baseValue)
/// <summary>
/// 폰트 스타일 속성 값 강제 콜백 처리하기
/// </summary>
/// <param name="d">의존 객체</param>
/// <param name="baseValue">기본 값</param>
/// <returns>처리 결과</returns>
private static object FontStyleProperty_CoerceValueCallback(DependencyObject d, object baseValue)
{
if(baseValue.Equals(FontStyles.Oblique))
{
baseValue = TextAlignmentProperty.DefaultMetadata.DefaultValue;
}
return baseValue;
}
#endregion
#region 폰트 가중치 속성 값 강제 콜백 처리하기 - FontWeightProperty_CoerceValueCallback(d, baseValue)
/// <summary>
/// 폰트 가중치 속성 값 강제 콜백 처리하기
/// </summary>
/// <param name="d">의존 객체</param>
/// <param name="baseValue">기본 값</param>
/// <returns>처리 결과</returns>
private static object FontWeightProperty_CoerceValueCallback(DependencyObject d, object baseValue)
{
if(!baseValue.Equals(FontWeights.Bold) && !baseValue.Equals(FontWeights.Regular) && !baseValue.Equals(FontWeights.Normal))
{
baseValue = TextAlignmentProperty.DefaultMetadata.DefaultValue;
}
return baseValue;
}
#endregion
#region 전경색 속성 값 강제 콜백 처리하기 - ForegroundProperty_CoerceValueCallback(d, baseValue)
/// <summary>
/// 전경색 속성 값 강제 콜백 처리하기
/// </summary>
/// <param name="d">의존 객체</param>
/// <param name="baseValue">기본 값</param>
/// <returns>처리 결과</returns>
private static object ForegroundProperty_CoerceValueCallback(DependencyObject d, object baseValue)
{
if(!(baseValue is SolidColorBrush))
{
baseValue = ForegroundProperty.DefaultMetadata.DefaultValue;
}
return baseValue;
}
#endregion
#region 비트맵 소스 구하기 - GetBitmapSource(bitmap)
/// <summary>
/// 비트맵 소스 구하기
/// </summary>
/// <param name="bitmap">비트맵</param>
/// <returns>비트맵 소스</returns>
private static System.Windows.Media.Imaging.BitmapSource GetBitmapSource(System.Drawing.Bitmap bitmap)
{
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height);
System.Drawing.Imaging.BitmapData bitmapData = bitmap.LockBits
(
rectangle,
System.Drawing.Imaging.ImageLockMode.ReadOnly,
bitmap.PixelFormat
);
System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Media.Imaging.BitmapSource.Create
(
bitmap.Width,
bitmap.Height,
bitmap.HorizontalResolution,
bitmap.VerticalResolution,
PixelFormats.Pbgra32,
null,
bitmapData.Scan0,
bitmapData.Stride * bitmap.Height,
bitmapData.Stride
);
bitmap.UnlockBits(bitmapData);
return bitmapSource;
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Instance
//////////////////////////////////////////////////////////////////////////////// Protected
#region 초기화시 처리하기 - OnInitialized(e)
/// <summary>
/// 초기화시 처리하기
/// </summary>
/// <param name="e">이벤트 인자</param>
protected override void OnInitialized(EventArgs e)
{
this.bitmapElement = new BitmapElement();
AddVisualChild(this.bitmapElement);
base.OnInitialized(e);
}
#endregion
#region 비주얼 자식 구하기 - GetVisualChild(index)
/// <summary>
/// 비주얼 자식 구하기
/// </summary>
/// <param name="index"></param>
/// <returns>비주얼 자식</returns>
protected override Visual GetVisualChild(int index)
{
return this.bitmapElement;
}
#endregion
#region 측정하기 (오버라이드) - MeasureOverride(availableSize)
/// <summary>
/// 측정하기 (오버라이드)
/// </summary>
/// <param name="availableSize">이용 가능한 크기</param>
/// <returns>측정 크기</returns>
protected override Size MeasureOverride(Size availableSize)
{
Matrix dpiMatrix = PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice;
float dpiX = (float)dpiMatrix.M11;
float dpiY = (float)dpiMatrix.M22;
CreateDrawingObjects(dpiX);
System.Drawing.Size size = new System.Drawing.Size();
using(System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(1, 1))
{
using(System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap))
{
graphics.TextRenderingHint = (System.Drawing.Text.TextRenderingHint)TextQuality;
System.Drawing.SizeF boundarySize = new System.Drawing.SizeF
(
(float)availableSize.Width * dpiX,
(float)availableSize.Height * dpiY
);
boundarySize = graphics.MeasureString(Text, this.drawingFont, boundarySize, this.drawingStringFormat);
size = new System.Drawing.Size
(
(int)Math.Ceiling(boundarySize.Width ),
(int)Math.Ceiling(boundarySize.Height)
);
}
}
using(System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(size.Width, size.Height))
{
using(System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap))
{
graphics.TextRenderingHint = (System.Drawing.Text.TextRenderingHint)TextQuality;
Color color = Foreground.Color;
graphics.DrawString
(
Text,
this.drawingFont,
this.drawingBrush,
new System.Drawing.RectangleF(0, 0, size.Width, size.Height),
this.drawingStringFormat
);
this.bitmapElement.Source = GetBitmapSource(bitmap);
this.bitmapElement.InvalidateMeasure();
this.bitmapElement.InvalidateVisual();
}
}
this.bitmapElement.Measure(availableSize);
return this.bitmapElement.DesiredSize;
}
#endregion
#region 배열하기 (오버라이드) - ArrangeOverride(finalSize)
/// <summary>
/// 배열하기 (오버라이드)
/// </summary>
/// <param name="finalSize">최종 크기</param>
/// <returns>배열 크기</returns>
protected override Size ArrangeOverride(Size finalSize)
{
this.bitmapElement.Arrange(new Rect(finalSize));
return finalSize;
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Private
#region 그리기 객체 리소스 해제하기 - DisposeDrawingObjects()
/// <summary>
/// 그리기 객체 리소스 해제하기
/// </summary>
private void DisposeDrawingObjects()
{
if(this.drawingBrush != null)
{
this.drawingBrush.Dispose();
}
if(this.drawingFont != null)
{
this.drawingFont.Dispose();
}
if(this.drawingStringFormat != null)
{
this.drawingStringFormat.Dispose();
}
}
#endregion
#region 그리기 객체 생성하기 - CreateDrawingObjects(dpi)
/// <summary>
/// 그리기 객체 생성하기
/// </summary>
/// <param name="dpi">DPI</param>
private void CreateDrawingObjects(float dpi)
{
DisposeDrawingObjects();
Color color = Foreground.Color;
this.drawingBrush = new System.Drawing.SolidBrush
(
System.Drawing.Color.FromArgb
(
(int)color.A,
(int)color.R,
(int)color.G,
(int)color.B
)
);
System.Drawing.FontStyle style = System.Drawing.FontStyle.Regular;
if(FontWeight == FontWeights.Bold)
{
style |= System.Drawing.FontStyle.Bold;
}
if(FontStyle == FontStyles.Italic)
{
style |= System.Drawing.FontStyle.Italic;
}
this.drawingFont = new System.Drawing.Font
(
FontFamily.Source,
(float)(FontSize * dpi),
style,
System.Drawing.GraphicsUnit.Pixel
);
this.drawingStringFormat = new System.Drawing.StringFormat();
switch(TextTrimming)
{
case TextTrimming.None :
this.drawingStringFormat.Trimming = System.Drawing.StringTrimming.None;
break;
case TextTrimming.CharacterEllipsis :
this.drawingStringFormat.Trimming = System.Drawing.StringTrimming.EllipsisCharacter;
break;
case TextTrimming.WordEllipsis :
this.drawingStringFormat.Trimming = System.Drawing.StringTrimming.EllipsisWord;
break;
}
this.drawingStringFormat.FormatFlags = System.Drawing.StringFormatFlags.LineLimit;
if(TextWrapping == TextWrapping.NoWrap)
{
this.drawingStringFormat.FormatFlags |= System.Drawing.StringFormatFlags.NoWrap;
}
if(FlowDirection == FlowDirection.RightToLeft)
{
this.drawingStringFormat.FormatFlags |= System.Drawing.StringFormatFlags.DirectionRightToLeft;
}
switch(TextAlignment)
{
case TextAlignment.Left :
this.drawingStringFormat.Alignment = System.Drawing.StringAlignment.Near;
break;
case TextAlignment.Center :
this.drawingStringFormat.Alignment = System.Drawing.StringAlignment.Center;
break;
case TextAlignment.Right :
this.drawingStringFormat.Alignment = System.Drawing.StringAlignment.Far;
break;
}
}
#endregion
}
}
300x250
▶ 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:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:TestProject"
Width="800"
Height="600"
Title="FrameworkElement 클래스 : GDI+를 사용해 텍스트 블럭 만들기"
FontFamily="나눔고딕코딩"
FontSize="16">
<Window.Resources>
<sys:String x:Key="TextStringKey">
Sed ut perspiciatis, unde omnis iste natus error sit voluptatem accusantium doloremque laudantium,
totam rem aperiam eaque ipsa, quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt,
explicabo. Nemo enim ipsam voluptatem, quia voluptas sit, aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos,
qui ratione voluptatem sequi nesciunt, neque porro quisquam est, qui dolorem ipsum, quia dolor sit, amet, consectetur,
adipisci velit, sed quia non numquam eius modi tempora incidunt, ut labore et dolore magnam aliquam quaerat voluptatem.
Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur?
Quis autem vel eum iure reprehenderit, qui in ea voluptate velit esse, quam nihil molestiae consequatur, vel illum,
qui dolorem eum fugiat, quo voluptas nulla pariatur? At vero eos et accusamus et iusto odio dignissimos ducimus,
qui blanditiis praesentium voluptatum deleniti atque corrupti, quos dolores et quas molestias excepturi sint,
obcaecati cupiditate non provident, similique sunt in culpa, qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio,
cumque nihil impedit, quo minus id, quod maxime placeat, facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet,
ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus,
ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
</sys:String>
<Style x:Key="ColorComboBoxItemKey" TargetType="ComboBoxItem">
<Setter Property="Background" Value="{Binding Value}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontSize" Value="16" />
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid
Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0"
Orientation="Horizontal">
<TextBlock
VerticalAlignment="Center"
Text="텍스트 크기" />
<TextBlock
VerticalAlignment="Center"
Margin="10 0 0 0"
Text="{Binding ElementName=textSizeSlider, Path=Value}" />
<Slider Name="textSizeSlider"
VerticalAlignment="Center"
Margin="10 0 0 0"
Width="100"
Minimum="10"
Maximum="30"
SmallChange="1"
LargeChange="2"
Value="20"
IsSnapToTickEnabled="True" />
<TextBlock
VerticalAlignment="Center"
Margin="10 0 0 0"
Text="배경색" />
<ComboBox Name="backgroundColorComboBox"
VerticalAlignment="Center"
Margin="10 0 0 0"
Width="100"
SelectedValuePath="Value"
DisplayMemberPath="Key"
ItemContainerStyle="{StaticResource ColorComboBoxItemKey}" />
<TextBlock
VerticalAlignment="Center"
Margin="10 0 0 0"
Text="전경색" />
<ComboBox Name="foregroundColorComboBox"
Margin="10 0 0 0"
Width="100"
SelectedValuePath="Value"
DisplayMemberPath="Key"
ItemContainerStyle="{StaticResource ColorComboBoxItemKey}" />
</StackPanel>
<Grid Grid.Row="1"
Margin="0 10 0 0"
Background="{Binding ElementName=backgroundColorComboBox, Path=SelectedValue}"
TextBlock.Foreground="{Binding ElementName=foregroundColorComboBox, Path=SelectedValue}"
TextBlock.FontFamily="Times New Roman"
TextBlock.FontSize="{Binding ElementName=textSizeSlider, Path=Value}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Content="GDI+" Grid.Column="0" />
<ScrollViewer Name="gdiScrollViewer" Grid.Row="1" Grid.Column="0"
ScrollChanged="ScrollViewer_ScrollChanged">
<local:GDITextBlock
TextWrapping="Wrap"
TextTrimming="WordEllipsis"
Text="{StaticResource TextStringKey}" />
</ScrollViewer>
<Label Content="WPF" Grid.Column="2" />
<ScrollViewer Name="wpfScrollViewer" Grid.Row="1" Grid.Column="2"
ScrollChanged="ScrollViewer_ScrollChanged">
<TextBlock
TextWrapping="Wrap"
TextTrimming="WordEllipsis"
Text="{StaticResource TextStringKey}" />
</ScrollViewer>
</Grid>
</Grid>
</Window>
▶ MainWindow.xaml.cs
using System.Collections.Generic;
using System.Linq;
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();
KeyValuePair<string, SolidColorBrush>[] brushKeyValuePairArray = typeof(Brushes).GetProperties()
.Where(pi => pi.PropertyType == typeof(SolidColorBrush))
.Select(pi => new KeyValuePair<string, SolidColorBrush>(pi.Name, (SolidColorBrush)pi.GetValue(null, null)))
.ToArray();
this.backgroundColorComboBox.ItemsSource = brushKeyValuePairArray;
this.backgroundColorComboBox.SelectedItem = brushKeyValuePairArray.First(c => c.Key == "Transparent");
this.foregroundColorComboBox.ItemsSource = brushKeyValuePairArray;
this.foregroundColorComboBox.SelectedItem = brushKeyValuePairArray.First(c => c.Key == "Black");
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 스크롤 뷰어 스크롤 변경시 처리하기 - ScrollViewer_ScrollChanged(sender, e)
/// <summary>
/// 스크롤 뷰어 스크롤 변경시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
ScrollViewer scrollViewer = (sender == this.wpfScrollViewer) ? this.gdiScrollViewer : this.wpfScrollViewer;
scrollViewer.ScrollToVerticalOffset(e.VerticalOffset);
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] Path 엘리먼트 : 별점(Star Rating) 아이콘 만들기 (0) | 2022.01.09 |
---|---|
[C#/WPF] Path 엘리먼트 : RSS 아이콘 만들기 (0) | 2022.01.09 |
[C#/WPF] Rectangle 엘리먼트 : 젤(Gel) 버튼 만들기 (0) | 2022.01.09 |
[C#/WPF] Canvas 엘리먼트 : 축구 경기장 만들기 (0) | 2022.01.09 |
[C#/WPF] VisualBrush 엘리먼트 : 해시 패턴 사용하기 (0) | 2022.01.09 |
[C#/WPF] x:ClassModifier 속성 사용하기 (0) | 2022.01.09 |
[C#/WPF] ResourceDictionary 클래스 : 느슨한 XAML 스키닝 사용하기 (0) | 2022.01.09 |
[C#/WPF] ResourceDictionary 클래스 : 컴파일 및 동적 스키닝 사용하기 (기능 개선) (0) | 2022.01.09 |
[C#/WPF] ResourceDictionary 클래스 : 컴파일 및 동적 스키닝 사용하기 (0) | 2022.01.09 |
[C#/WPF] ResourceDictionary 클래스 : 컴파일 및 정적 스키닝 사용하기 (0) | 2022.01.09 |
댓글을 달아 주세요