MpuseDown 이벤트와 MouseUP 이벤트 를 해준다
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;
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;
break;
case "2":
type = ShapeType.Rectangle;
break;
case "3":
type = ShapeType.Line;
break;
}
}
private void SelectColor(object sender, EventArgs e)
{
ColorDialog dlg = new ColorDialog();
ToolStripMenuItem mi = (ToolStripMenuItem)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;
}
}
private void SelectLineThickness(object sender, EventArgs e)
{
ToolStripMenuItem mi = (ToolStripMenuItem)sender;
thickness = int.Parse(mi.Tag.ToString());
}
private Point start;
private Point start2;
private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
start = new Point(e.X, e.Y);
start2 = new Point(e.X, e.Y);
}
private void MainForm_MouseUp(object sender, MouseEventArgs e)
{
Graphics grfx = CreateGraphics();
Pen pen = new Pen(lineColor, thickness);
SolidBrush brush = new SolidBrush(fillColor);
Rectangle rect =
new Rectangle(start.X, start.Y,
e.X - start.X, e.Y - start.Y);
switch (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,
start, new Point(e.X, e.Y));
break;
}
grfx.Dispose();//비관리형 자원 해제
}
}
}
'프로그램 > C# - Study' 카테고리의 다른 글
C# - Ex06 - 그림판3 (0) | 2010.10.27 |
---|---|
C# - Ex06 - 그림판2 (3) | 2010.10.27 |
C# - Ex05 - 메모장 (0) | 2010.10.26 |
C# - Ex04 - 탐색기 만들기 (3) | 2010.10.22 |
EX13 - Microsoft Enterprise Library 사용해 간단하게 DB 자료 가져오기 (0) | 2010.10.21 |