728x90
반응형
728x170
▶ MainForm.cs
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TestProject
{
/// <summary>
/// 메인 폼
/// </summary>
public partial class MainForm : Form
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
#region 이벤트를 설정한다.
Load += Form_Load;
Resize += Form_Resize;
#endregion
}
#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)
{
DrawText();
}
#endregion
#region 폼 크기 조정시 처리하기 - Form_Resize(sender, e)
/// <summary>
/// 폼 크기 조정시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void Form_Resize(object sender, EventArgs e)
{
DrawText();
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 텍스트 그리기 - DrawText()
/// <summary>
/// 텍스트 그리기
/// </summary>
private void DrawText()
{
string text = @"This is a fairly long piece of text that won't necessarily fit in the left PictureBox.
When you change the form's height, that changes the amount of text that fits in the left PictureBox.
The program draws as much text as will fit in the left PictureBox and then draws the remainder of the text in the right PictureBox.";
using(Font font = new Font("나눔고딕코딩", 16))
{
using(StringFormat stringFormat = new StringFormat())
{
stringFormat.Trimming = StringTrimming.Word;
Bitmap leftBitmap = new Bitmap
(
this.leftPictureBox.ClientSize.Width,
this.leftPictureBox.ClientSize.Height
);
using(Graphics leftGraphics = Graphics.FromImage(leftBitmap))
{
leftGraphics.Clear(this.leftPictureBox.BackColor);
const int MARGIN = 5;
Rectangle rectangle = new Rectangle
(
MARGIN,
MARGIN,
this.leftPictureBox.ClientSize.Width - 2 * MARGIN,
this.leftPictureBox.ClientSize.Height - 2 * MARGIN
);
leftGraphics.DrawRectangle(Pens.LightGreen, rectangle);
int fitCharacterCount;
int fillLineCount;
SizeF availableSize = new SizeF(rectangle.Width, rectangle.Height);
leftGraphics.MeasureString
(
text,
font,
availableSize,
stringFormat,
out fitCharacterCount,
out fillLineCount
);
string fitText = text.Substring(0, fitCharacterCount);
leftGraphics.DrawString(fitText, font, Brushes.Black, rectangle, stringFormat);
this.leftPictureBox.Image = leftBitmap;
text = text.Substring(fitCharacterCount);
}
Bitmap rightBitmap = new Bitmap
(
this.rightPictureBox.ClientSize.Width,
this.rightPictureBox.ClientSize.Height
);
using(Graphics rightGraphics = Graphics.FromImage(rightBitmap))
{
rightGraphics.Clear(this.rightPictureBox.BackColor);
const int MARGIN = 5;
Rectangle rectangle = new Rectangle
(
MARGIN,
MARGIN,
this.rightPictureBox.ClientSize.Width - 2 * MARGIN,
this.rightPictureBox.ClientSize.Height - 2 * MARGIN
);
rightGraphics.DrawRectangle(Pens.LightGreen, rectangle);
rightGraphics.DrawString(text, font, Brushes.Black, rectangle, stringFormat);
}
this.rightPictureBox.Image = rightBitmap;
}
}
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] 콤보 박스/리스트 박스 데이터 소스 연결하기 (0) | 2019.01.07 |
---|---|
[C#/WINFORM] 바퀴와 연결된 2개의 막대 움직이기 (0) | 2019.01.07 |
[C#/WINFORM] 로봇 팔 마우스 추적하기 (0) | 2019.01.07 |
[C#/WINFORM] 타원을 임의 선으로 채우기 (0) | 2019.01.06 |
[C#/WINFORM] RichTextBox 클래스 : 위/아래 첨자 사용하기 (0) | 2019.01.06 |
[C#/WINFORM] WebBrowser 클래스 : 구글 맵 사용하기 (0) | 2019.01.06 |
[C#/WINFORM] Metafile 클래스 : 메타 파일 기록 열거하기 (0) | 2019.01.06 |
[C#/WINFORM] Metafile 클래스 : 메타 파일 로드하기 (0) | 2019.01.06 |
[C#/WINFORM] Metafile 클래스 : 메타 파일 사용하기 (0) | 2019.01.06 |
[C#/WINFORM] ComboBox 클래스 : 사용자 정의 그리기 (0) | 2019.01.06 |
댓글을 달아 주세요