728x90
반응형
728x170
▶ MainForm.cs
using System;
using System.Collections.Generic;
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 text = "When in the course of human events it becomes necessary for the quick brown fox to jump over the lazy dog...";
/// <summary>
/// 사각형 리스트
/// </summary>
private List<RectangleF> rectangleList;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
Load += Form_Load;
this.pictureBox.MouseMove += pictureBox_MouseMove;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Event
#region 폼 로드시 처리하기 - Form_Load(sender, e)
/// <summary>
/// 폼 로드시 처리하기
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form_Load(object sender, EventArgs e)
{
Bitmap bitmap = new Bitmap
(
this.pictureBox.ClientSize.Width,
this.pictureBox.ClientSize.Height
);
using(Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.Clear(Color.White);
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
using(Font font = new Font("나눔고딕코딩", 16, FontStyle.Regular))
{
graphics.DrawString(this.text, font, Brushes.Blue, 4, 4);
this.rectangleList = GetCharacterRectangleList(graphics, font, this.text);
}
}
this.pictureBox.Image = bitmap;
}
#endregion
#region 픽처 박스 마우스 이동시 처리하기 - pictureBox_MouseMove(sender, e)
/// <summary>
/// 픽처 박스 마우스 이동시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
string message = string.Empty;
for(int i = 0; i < this.text.Length; i++)
{
if(rectangleList[i].Contains(e.Location))
{
message = $"{i} 번째 문자 : {this.text[i]}";
break;
}
}
if(this.messageLabel.Text != message)
{
this.messageLabel.Text = message;
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 문자 사각형 리스트 구하기 (단어용) - GetCharacterRectangleListForCharacter(graphics, font, text)
/// <summary>
/// 문자 사각형 리스트 구하기 (단어용)
/// </summary>
/// <param name="graphics">그래픽스</param>
/// <param name="font">폰트</param>
/// <param name="text">텍스트</param>
/// <returns>문자 사각형 리스트</returns>
private List<RectangleF> GetCharacterRectangleListForCharacter(Graphics graphics, Font font, string text)
{
List<RectangleF> rectangleList = new List<RectangleF>();
using(StringFormat stringFormat = new StringFormat())
{
stringFormat.Alignment = StringAlignment.Near;
stringFormat.LineAlignment = StringAlignment.Near;
stringFormat.Trimming = StringTrimming.None;
stringFormat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
CharacterRange[] characterRangeArray = new CharacterRange[text.Length];
for(int i = 0; i < text.Length; i++)
{
characterRangeArray[i] = new CharacterRange(i, 1);
}
stringFormat.SetMeasurableCharacterRanges(characterRangeArray);
RectangleF rectangle = new RectangleF(0, 0, 10000, 100);
Region[] regionArray = graphics.MeasureCharacterRanges
(
text,
font,
ClientRectangle,
stringFormat
);
foreach(Region region in regionArray)
{
rectangleList.Add(region.GetBounds(graphics));
}
}
return rectangleList;
}
#endregion
#region 문자 사각형 리스트 구하기 - GetCharacterRectangleList(graphics, font, text)
/// <summary>
/// 문자 사각형 리스트 구하기
/// </summary>
/// <param name="graphics">그래픽스</param>
/// <param name="font">폰트</param>
/// <param name="text">텍스트</param>
/// <returns>문자 사각형 리스트</returns>
private List<RectangleF> GetCharacterRectangleList(Graphics graphics, Font font, string text)
{
List<RectangleF> rectangleList = new List<RectangleF>();
float x = 0;
for(int start = 0; start < text.Length; start += 32)
{
int length = 32;
if(start + length >= text.Length)
{
length = text.Length - start;
}
string substring = text.Substring(start, length);
List<RectangleF> temporaryList = GetCharacterRectangleListForCharacter(graphics, font, substring);
if(start == 0)
{
x += temporaryList[0].Left;
}
for(int i = 0; i < temporaryList.Count + 1 - 1; i++)
{
RectangleF newRectangle = new RectangleF
(
x,
temporaryList[i].Top,
temporaryList[i].Width,
temporaryList[i].Height
);
rectangleList.Add(newRectangle);
x += temporaryList[i].Width;
}
}
return rectangleList;
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] 미로 길 찾기 (애니메이션) (0) | 2020.08.05 |
---|---|
[C#/WINFORM] 미로 길 찾기 (0) | 2020.08.05 |
[C#/WINFORM] ImageAttributes 클래스 : 이미지 수정하기 (0) | 2020.08.04 |
[C#/WINFORM] 라운드 다각형 그리기 (0) | 2020.08.03 |
[C#/WINFORM] 기준선을 사용해 이미지 회전하기 (0) | 2020.08.03 |
[C#/WINFORM] 마우스 아래 그려진 문자 찾기 (0) | 2020.08.03 |
[C#/WINFORM] 다각형 내에서 이미지 그리기 (0) | 2020.08.03 |
[C#/WINFORM] RGB 색상에서 HSV 색상 구하기 (0) | 2020.08.02 |
[C#/WINFORM] 다각형 선택자를 사용해 다각형 그리기 (0) | 2020.08.01 |
[C#/WINFORM] X/Y축 기준선에 따라 다각형 그리기 (0) | 2020.07.30 |
[C#/WINFORM] Graphics 클래스 : 점(dot) 그리기 (0) | 2020.07.30 |
댓글을 달아 주세요