728x90
반응형
728x170
▶ MainForm.cs
using System;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
namespace TestProject
{
/// <summary>
/// 메인 폼
/// </summary>
public partial class MainForm : Form
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 소스 텍스트
/// </summary>
private string sourceText = "박항서 감독이 이끄는 베트남 축구대표팀이 지난 15일 10년 만에 처음으로";
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Event
#region 픽처 박스 크기 조정하기 - pictureBox_Resize(sender, e)
/// <summary>
/// 픽처 박스 크기 조정하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void pictureBox_Resize(object sender, EventArgs e)
{
this.pictureBox.Refresh();
}
#endregion
#region 픽처 박스 페인트시 처리하기 - pictureBox_Paint(sender, e)
/// <summary>
/// 픽처 박스 페인트시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(this.pictureBox.BackColor);
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
RectangleF rectangle = new RectangleF
(
5,
5,
this.pictureBox.ClientSize.Width - 10,
this.pictureBox.ClientSize.Height - 10
);
using(Font font = new Font("나눔고딕코딩", 12))
{
DrawString(e.Graphics, rectangle, font, Brushes.Blue, this.sourceText);
}
e.Graphics.DrawRectangle(Pens.Silver, Rectangle.Round(rectangle));
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 문자열 그리기 - DrawString(graphics, rectangle, font, brush, text)
/// <summary>
/// 문자열 그리기
/// </summary>
/// <param name="graphics">그래픽스</param>
/// <param name="rectangle">사각형</param>
/// <param name="font">폰트</param>
/// <param name="brush">브러시</param>
/// <param name="text">텍스트</param>
private void DrawString(Graphics graphics, RectangleF rectangle, Font font, Brush brush, string text)
{
string[] wordArray = text.Split(' ');
float[] wordWidthArray = new float[wordArray.Length];
float totalWidth = 0f;
for(int i = 0; i < wordArray.Length; i++)
{
SizeF size = graphics.MeasureString(wordArray[i], font);
wordWidthArray[i] = size.Width;
totalWidth += wordWidthArray[i];
}
float extraSpace = rectangle.Width - totalWidth;
int spaceCount = wordArray.Length - 1;
if(wordArray.Length > 1)
{
extraSpace /= spaceCount;
}
float x = rectangle.Left;
float y = rectangle.Top;
for(int i = 0; i < wordArray.Length; i++)
{
graphics.DrawString(wordArray[i], font, brush, x, y);
x += wordWidthArray[i] + extraSpace;
}
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] ImageAttributes 클래스 : 세피아 이미지 구하기 (0) | 2018.12.18 |
---|---|
[C#/WINFORM] ImageAttributes 클래스 : 모노크롬 이미지 구하기 (0) | 2018.12.18 |
[C#/WINFORM] ImageAttributes 클래스 : 이미지 명도 조정하기 (0) | 2018.12.18 |
[C#/WINFORM] Bitmap 클래스 : 회색조 비트맵 구하기 (0) | 2018.12.18 |
[C#/WINFORM] 문단 JUSTIFY 정렬하기 (0) | 2018.12.16 |
[C#/WINFORM] 다각형 에디터 사용하기 (0) | 2018.12.13 |
[C#/WINFORM] 라인 에디터 사용하기 (0) | 2018.12.13 |
[C#/WINFORM] 확대 창 사용하기 (0) | 2018.12.13 |
[C#/WINFORM] 2D/3D 테두리 그리기 (0) | 2018.12.13 |
[C#/WINFORM] 소수 프랙탈(Prime Number Fractal) 그리기 (0) | 2018.12.11 |
댓글을 달아 주세요