728x90
반응형
728x170
▶ MainForm.cs
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace TestProject
{
/// <summary>
/// 메인 폼
/// </summary>
public partial class MainForm : Form
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
ResizeRedraw = true;
#region 이벤트를 설정한다.
Paint += Form_Paint;
#endregion
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Event
#region 폼 페인트시 처리하기 - Form_Paint(sender, e)
/// <summary>
/// 폼 페인트시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void Form_Paint(object sender, PaintEventArgs e)
{
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
int width = ClientSize.Width;
int height = ClientSize.Height / 2;
Rectangle rectangle = new Rectangle(0, 0, width, height);
using(Font font = new Font("나눔고딕코딩", 60, FontStyle.Italic))
{
DrawStringWithCharacterRectangle(e.Graphics, "테스트", font, rectangle);
}
width /= 2;
rectangle = new Rectangle(0, height, width, height);
using(Font font = new Font("나눔고딕코딩", 60, FontStyle.Regular))
{
DrawStringWithCharacterRectangle(e.Graphics, "테스트", font, rectangle);
}
rectangle = new Rectangle(width, height, width, height);
using(Font font = new Font("나눔고딕코딩", 60, FontStyle.Italic))
{
DrawStringWithCharacterRectangle(e.Graphics, "테스트", font, rectangle);
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 문자 사각형을 갖는 문자열 그리기 - DrawStringWithCharacterRectangle(graphics, text, font, rectangle)
/// <summary>
/// 문자 사각형을 갖는 문자열 그리기
/// </summary>
/// <param name="graphics">그래픽스</param>
/// <param name="text">텍스트</param>
/// <param name="font">폰트</param>
/// <param name="rectangle">사각형</param>
private void DrawStringWithCharacterRectangle(Graphics graphics, string text, Font font, Rectangle rectangle)
{
using(StringFormat stringFormat = new StringFormat())
{
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
graphics.DrawString(text, font, Brushes.Blue, rectangle, stringFormat);
List<CharacterRange> characterRangeList = new List<CharacterRange>();
for(int i = 0; i < text.Length; i++)
{
characterRangeList.Add(new CharacterRange(i, 1));
}
stringFormat.SetMeasurableCharacterRanges(characterRangeList.ToArray());
Region[] regionArray = graphics.MeasureCharacterRanges(text, font, rectangle, stringFormat);
for(int i = 0; i < text.Length; i++)
{
Rectangle textRectangle = Rectangle.Round(regionArray[i].GetBounds(graphics));
graphics.DrawRectangle(Pens.Red, textRectangle);
}
}
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] 외곽선 텍스트 그리기 (0) | 2018.12.08 |
---|---|
[C#/WINFORM] 부드러운 소용돌이 프랙탈(Smooth Vortex Fractal) 그리기 (0) | 2018.12.08 |
[C#/WINFORM] 소용돌이 프랙탈(Vortex Fractal) 그리기 (0) | 2018.12.08 |
[C#/WINFORM] 소용돌이 모양 프랙탈(Curlicue Fractal) 그리기 (0) | 2018.12.08 |
[C#/WINFORM] 폰트 메트릭(Font Metrics) 구하기 (0) | 2018.12.08 |
[C#/WINFORM] 문자 크기 측정하기 (0) | 2018.12.08 |
[C#/WINFORM] 마우스 드래그시 고무밴드 상자(Rubber Band Box) 표시하기 (0) | 2018.12.08 |
[C#/WINFORM] 포인트 리스트를 둘러싸는 원 구하기 (0) | 2018.12.05 |
[C#/WINFORM] 2개 직선 사이 최단거리 구하기 (0) | 2018.12.05 |
[C#/WINFORM] 2개 직선의 교차 여부 구하기 (0) | 2018.12.05 |
[C#/WINFORM] 하이포트로코이드(Hypotrochoid) 그리기 (0) | 2018.12.05 |
댓글을 달아 주세요