본문 바로가기

부장님의 이야기

[WPF-Find Different] : #2 마우스클릭 이벤트

 

 

 

 

 

 

 

오늘 해본 것은 "마우스의 위치를 파악"하는 코드 작성이다.

 

위치 구하는 케이스는 2가지이다.

- 마우스를 클릭한 경우

- 마우슬 드래그한 경우

 

 

 

 

 

 

 

 

 

 

 

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Application 2" Height="350" Width="325"
        MouseDown="Window_MouseDown"
        AllowDrop="True" DragOver="Window_DragOver">
    <StackPanel>
        <Label Name="lblInfo1" Content="Info 1"/>
        <Label Name="lblInfo2" Content="Info 2"/>
    </StackPanel>
</Window>

 

        MouseDown="Window_MouseDown"                            
        AllowDrop="True"
        DragOver="Window_DragOver"

MouseDown 이벤트가 일어난 경우 "~~"의 함수가 실행된다.

 

AllowDrop :

컨트롤에서 끌어서 놓기 작업을 수행할 수 있으면 true이고,

그렇지 않으면 false입니다. 기본값은 false입니다.

 

 

 

 

 

 

 

 

 

 

 

 

- MouseDown이벤트 발생시 실행할 것

	private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            DragDrop.DoDragDrop((DependencyObject)e.Source, "Sample", DragDropEffects.Copy);
            System.Windows.Point p1 = Mouse.GetPosition(this);
            lblInfo1.Content = string.Format("Mouse.GetPosition: {0}, {1}", p1.X, p1.Y);
        }

맨 첫줄 나도 이해가 안간다... 아는 분 댓글달아주세요ㅠㅠ

(드래그엔드롭을 시작한다는 의미)

기본적으로 제공하는 구조체! 너무 신기했음!

현재 마우스위치를 받아서 저장해준다.

놀랍게도 x와 y를 스스로 구분해서 저장해줌

 

lblInfo1은 라벨에 laName="lblInfo1"를 이용해서 지정해준 이름

 

String.Format을 통해 형식 지정

 

        private void Window_DragOver(object sender, DragEventArgs e)
        {
            System.Windows.Point p2 = e.GetPosition(this);
            lblInfo2.Content = string.Format("DragEventArgs.GetPosition: {0}, {1}", p2.X, p2.Y);
        }

위와 동일

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


결과!

 

 

 

 

참고

https://wpf.2000things.com/tag/getposition/