127. 마우스다운(MouseDown) / 마우스무브(MouseMove) 이벤트(= 마우스드래그하여 선그리기)
.NET프로그래밍/WinForm 2009. 9. 2. 11:07 |
* "마우스다운(MouseDown)" 이벤트는 마우스 "우클릭" --> 폼(Form)에서 원 만들기
"마우스무브(MouseMove)" 이벤트는 마우스 "좌클릭"하여 "드래그" --> 폼(Form)에서 선그리기
* 이 예제에서는 'MyWinForms'에 따로 폴더 추가하여 "Mouse"로 이름 지정한 후 이 폴더에 "FrmMouseDown.cs"를 만들었다.
==> Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace MyWinForms
{
static class Program
{
/// <summary>
/// 해당 응용 프로그램의 주 진입점입니다.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Mouse.FrmMouseDown());
}
}
}
==============================================================================================
==> FrmMouseDown.cs
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 MyWinForms.Mouse
{
public partial class FrmMouseDown : Form
{
public FrmMouseDown()
{
InitializeComponent();
}
private void FrmMouseDown_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button== MouseButtons.Right) // 마우스 우클릭
{
// 그래픽 객체 생성
Graphics g = CreateGraphics();
// 원 그리기
g.DrawEllipse(Pens.Red, e.X, e.Y, 10, 10);
// 객체 해제
g.Dispose();
}
p.X = e.X; p.Y = e.Y;
}
private Point p; // 좌표
private void FrmMouseDown_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) // 마우스 좌클릭
{
Graphics g = this.CreateGraphics();
g.DrawLine(Pens.Blue, p.X, p.Y, e.X, e.Y);
p.X = e.X; p.Y = e.Y; // 재설정
g.Dispose();
}
}
}
}
< 실행결과 >
--> 마우스 "우클릭"으로 "마우스다운(MouseDown)" 이벤트 실행하여 폼(Form)안에 빨간색 원을 그렸고 / 마우스 "좌클릭" 후 '드래그'해서 "마우스무브(MouseMove)"이벤트 실행하여 파란색 선을 그렸다.
'.NET프로그래밍 > WinForm' 카테고리의 다른 글
129. 폼 클로징(즈) 이벤트 (FormClosing) (0) | 2009.09.02 |
---|---|
128. CurveList (다른 폼(ex : 메모장)에 의해 의해 내가 마우스로 폼에 그렸던 그림 손상시키지 않기.) (0) | 2009.09.02 |
126. KeyDown이벤트를 이용하여 원 그리기 (0) | 2009.09.02 |
125. 키보드(Keyboard) 이벤트 실습 (0) | 2009.09.02 |
119. 초간단 메모장에서 "인쇄 미리 보기" 기능 구현하기 (0) | 2009.08.31 |