728x90
반응형
  • 마우스 이벤트 처리
  • 마우스를 이용한 좌표 출력
  • 마우스를 이용한 삼각형 출력
  • 마우스를 이용한 드래그

  • Click vs MouseClick

Click : 마우스 클릭

MouseClick : 컨트롤 위에서 마우스 클릭

 

  • 폼도 컨트롤을 상속하므로 폼 위에서 클릭은 Click() → MouseClick()

 

이벤트 구분

  • 컨트롤 위에서의 마우스 이벤트
private void button1_MouseDown(object sender, MouseEventArgs e)
{
    //컨트롤 위에서 마우스가 눌렸을때
    MessageBox.Show("버튼 On");
}

 

  • 이벤트 소속을 나타내는 부분
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
    MessageBox.Show("Mouse Click");
}

private void button1_MouseDown(object sender, MouseEventArgs e)
{
    //컨트롤 위에서 마우스가 눌렸을때
    MessageBox.Show("버튼 On");
}

 

 

마우스의 좌표를 출력

  • MouseMove 이벤트 : MouseEventArgs.X, MouseEventArgs.Y (매개변수 : X, Y)
  • 마우스의 이동 좌표를 출력하는 프로그램

 

예제

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        string strMousepos;
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            strMousepos = "X :" + e.X + "Y :" + e.Y;
            Invalidate(); //작성 해야 화면에 출력됨
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawString(strMousepos, Font, Brushes.Black, 10, 10);
        }
    }
}

 

 

이벤트 발생 순서

  • MSDN : 폼에서 발생하는 클릭 이벤트 수선
    • 마우스 클릭 : MouseDown → Click → MouseClick → MouseUp
    • 마우스 더블 클릭 : MouseDown → Click → MouseClick → MouseUp → MouseDown → DoubleClick → MouseDoubleClick → MouseUp

 

마우스 3번 클릭

  • 마우스를 3번 클릭할 때마다 삼각형 출력
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        string strMousepos;
        List<Point> ListPoint = new List<Point>();
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            strMousepos = "X :" + e.X + "Y :" + e.Y;
            Invalidate();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if(ListPoint.Count ==3)
            {
                e.Graphics.DrawLine(Pens.Black, ListPoint[0], ListPoint[1]);
                e.Graphics.DrawLine(Pens.Green, ListPoint[1], ListPoint[2]);
                e.Graphics.DrawLine(Pens.Blue, ListPoint[2], ListPoint[0]);
            }
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            Point pt = new Point(e.X, e.Y);
            if(ListPoint.Count == 3)
            {
                ListPoint.Clear(); // 4번이 클릭되면(3개의 점이 그려지면) 포인트 clear
            }
            ListPoint.Add(pt); // 삼각형의 제일 첫번째
            Invalidate();
        }
    }
}

 

MouseEventArgs 속성

  • Button
    • 누른 마우스 버튼의 값을 가져온다
    • MouseButtons 열거형 : Left, Middle, None, Right
  • X, Y
    • 마우스의 x, y 좌표
  • Location
    • 마우스의 위치
  • Delta
    • 마우스 휠의 회전수

 

마우스 드래그

  • 드래그 : 마우스 왼쪽 버튼 클릭한 상태에서 이동(MouseButtons.Left && MouseMove)
  • 마우스의 드래그 영역을 사각형으로 출력
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        Rectangle rectMouse;
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                rectMouse.Width = e.X - rectMouse.X + 1; // 길이정보 1을 + 한 이유는 자기자신포함
                rectMouse.Height = e.Y - rectMouse.Y + 1;

                Invalidate();

            }
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            string str = "left : " + rectMouse.X +
                         "top : " + rectMouse.Y +
                         "right : " + rectMouse.Right +
                         "bottom : " + rectMouse.Bottom;

            e.Graphics.DrawRectangle(Pens.Black, rectMouse);
            e.Graphics.DrawString(str, Font, Brushes.Black, 10, 10);

        }
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            rectMouse.X = e.X;
            rectMouse.Y = e.Y;
            rectMouse.Width = 0;
            rectMouse.Height = 0;
            Invalidate();
        }
    }
}

728x90
반응형

+ Recent posts