업데이트: 2007년 11월
이 예제에서는 마우스 단추 이벤트와 MouseButtonState 속성을 사용하여 특정 마우스 단추를 누르거나 놓았는지 여부를 결정하는 방법을 보여 줍니다.
이 예제는 XAML(Extensible Application Markup Language) 파일 및 코드 숨김 파일로 구성되어 있습니다. 전체 샘플을 보려면 마우스 단추 상태 감지 샘플을 참조하십시오.
예제
다음 코드에서는 StackPanel 내부에서 TextBlock으로 구성된 사용자 인터페이스를 만들고 MouseLeftButtonDown 및 MouseLeftButtonUp 이벤트에 대한 이벤트 처리기를 연결합니다.
<StackPanel Height="100" Width="100"
MouseLeftButtonDown="HandleButtonDown"
MouseLeftButtonUp="HandleButtonDown"
Background="#d08080"
DockPanel.Dock="Left"
>
<TextBlock>Click on Me</TextBlock>
</StackPanel>
다음 코드 숨김은 MouseLeftButtonUp 및 MouseLeftButtonDown 이벤트 처리기를 만듭니다. 마우스 왼쪽 단추를 누르면 TextBlock의 크기가 증가되고, 마우스 왼쪽 단추를 놓으면 TextBlock의 크기가 원래 높이와 너비로 돌아갑니다.
Partial Public Class Window1
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub HandleButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
' Casting the source to a StackPanel
Dim sourceStackPanel As StackPanel = CType(e.Source, StackPanel)
' If the button is pressed then make dimensions larger.
If e.ButtonState = MouseButtonState.Pressed Then
sourceStackPanel.Width = 200
sourceStackPanel.Height = 200
' If the button is released then make dimensions smaller.
ElseIf e.ButtonState = MouseButtonState.Released Then
sourceStackPanel.Width = 100
sourceStackPanel.Height = 100
End If
End Sub
End Class
public Window1()
{
InitializeComponent();
}
void HandleButtonDown(object sender, MouseButtonEventArgs e)
{
//Casting the source to a StackPanel
StackPanel sourceStackPanel = e.Source as StackPanel;
//If the button is pressed then make dimensions larger.
if (e.ButtonState == MouseButtonState.Pressed)
{
sourceStackPanel.Width = 200;
sourceStackPanel.Height = 200;
}
//If the button is released then make dimensions smaller.
else if (e.ButtonState == MouseButtonState.Released)
{
sourceStackPanel.Width = 100;
sourceStackPanel.Height = 100;
}
}
}