* "마우스다운(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)"이벤트 실행하여 파란색 선을 그렸다.





Posted by holland14
: