using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Ex06
{
enum ShapeType
{
Ellipse,
Rectangle,
Line
}
public partial class MainForm : Form
{
private ShapeType type = ShapeType.Ellipse;
private int thickness = 1;
private Color lineColor = Color.Black;
private Color fillColor = Color.Orange;
//Paint를 하기 위해 추가
private LinkedList<ShapeInfo> shapes = new LinkedList<ShapeInfo>();
public MainForm()
{
InitializeComponent();
}
private void SelectShape(object sender, EventArgs e)
{
ToolStripMenuItem mi = (ToolStripMenuItem)sender;
//MessageBox.Show(mi.Text);
//switch (mi.Text)
//{
// case "원": break;
// case "사각형": break;
// case "선": break;
//}
//설정한tag 값을 읽는다.
switch (mi.Tag.ToString())
{
//SelectNumber
case "1":
type = ShapeType.Ellipse;
lblType.Text = "[도형 : 원]"; // statusStrip 항목 에 추가
break;
case "2":
type = ShapeType.Rectangle;
lblType.Text = "[도형 : 사각형]"; // statusStrip 항목 에 추가
break;
case "3":
type = ShapeType.Line;
lblType.Text = "[도형 : 선]"; // statusStrip 항목 에 추가
break;
}
}
private void SelectColor(object sender, EventArgs e)
{
ColorDialog dlg = new ColorDialog();
//변경
//1. 메뉴항목만 가능
//ToolStripMenuItem mi = (ToolStripMenuItem)sender;
//2. 메뉴 + 툴바 + ...
ToolStripItem mi = (ToolStripItem)sender;
switch (mi.Tag.ToString())
{
case "1":
dlg.Color = lineColor;
if (dlg.ShowDialog() == DialogResult.OK)
{
lineColor = dlg.Color;
}
break;
case "2":
dlg.Color = fillColor;
if (dlg.ShowDialog() == DialogResult.OK)
{
fillColor = dlg.Color;
}
break;
}
lblColor.Text = string.Format("[선 : {0}, 면 : {1}",
lineColor, fillColor); // statusStrip 항목 에 추가
// lineColor.ToKnownColor 하면 값이름 반환 없으면 0 반환
}
private void SelectLineThickness(object sender, EventArgs e)
{
ToolStripMenuItem mi = (ToolStripMenuItem)sender;
thickness = int.Parse(mi.Tag.ToString());
lblThickness.Text = string.Format("굵기 : {0}]", thickness);
}
private Point start;
private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
start = new Point(e.X, e.Y);
}
private void MainForm_MouseUp(object sender, MouseEventArgs e)
{
//Paint를 하기 위해 추가&변경
ShapeInfo info = new ShapeInfo
{
Type = type,
Start = start,
End = new Point(e.X, e.Y),
FillColor = fillColor,
LineColor = lineColor,
Thickness = thickness
};
shapes.AddLast(info);
Graphics grfx = CreateGraphics();
DrawShape(grfx, info);
grfx.Dispose();//비관리형 자원 해제
}
//Paint를 하기 위해 메서드 추가
private void MainForm_Paint(object sender, PaintEventArgs e)
{
Graphics grfx = e.Graphics;
foreach (ShapeInfo info in shapes)
{
DrawShape(grfx, info);
}
}
private void DrawShape(Graphics grfx, ShapeInfo info)
{
SolidBrush brush = new SolidBrush(info.FillColor);
Pen pen = new Pen(info.LineColor, info.Thickness);
//Rectangle rect = new Rectangle(
// info.Start.X, info.Start.Y,
// info.End.X - info.Start.X,
// info.End.Y - info.Start.Y);
//Paint를 하기 위해 변경
Rectangle rect =
GetPositiveRectangle(info.Start, info.End);
switch (info.Type)
{
case ShapeType.Ellipse:
grfx.FillEllipse(brush, rect);
grfx.DrawEllipse(pen, rect);
break;
case ShapeType.Rectangle:
grfx.FillRectangle(brush, rect);
grfx.DrawRectangle(pen, rect);
break;
case ShapeType.Line:
grfx.DrawLine(pen,
info.Start, info.End);
break;
}
}
//Paint를 하기 위해 메서드 추가( 사각형 자유롭게 그리기 위해)
private Rectangle GetPositiveRectangle(Point start, Point end)
{
Rectangle rect = new Rectangle
{
X = end.X > start.X ? start.X : end.X,
Y = end.Y > start.Y ? start.Y : end.Y,
Width = Math.Abs(end.X - start.X),
Height = Math.Abs(end.Y - start.Y)
};
return rect;
}
private void tscType_SelectedIndexChanged(object sender, EventArgs e)
{
type = (ShapeType)tscType.SelectedIndex;
}
private void tstThickness_TextChanged(object sender, EventArgs e)
{
// TryParse는 변환 성공여부 확인
if(!int.TryParse(tstThickness.Text.Trim(), out thickness))
{
MessageBox.Show("숫자만 입력하세요");
tstThickness.Text = tstThickness.ToString();
return;
}
if (thickness < 1 || thickness > 20)
{
MessageBox.Show("음수 또는 20 이상의 값을 허용하지 않습니다.");
thickness = 1;
tstThickness.Text = tstThickness.ToString();
}
}
}
}
'프로그램 > C# - Study' 카테고리의 다른 글
C# - Ex08 Client - Server (1ver) (0) | 2010.10.29 |
---|---|
C# - Ex07 - Brush 다루기 (0) | 2010.10.28 |
C# - Ex06 - 그림판2 (3) | 2010.10.27 |
C# - Ex06 - 그림판 (0) | 2010.10.27 |
C# - Ex05 - 메모장 (0) | 2010.10.26 |