이 예제에서는 마우스 포인터가 화면에서 이동할 때 개체의 크기를 변경하는 방법을 보여 줍니다.
이 예에는 UI(사용자 인터페이스)를 만드는 XAML(Extensible Application Markup Language) 파일과 이벤트 처리기를 만드는 코드 숨김 파일이 포함되어 있습니다.
예시
다음 XAML은 StackPanel의 내부에서 Ellipse로 구성된 UI를 만들고 MouseMove 이벤트에 대한 이벤트 처리기를 연결합니다.
<Window x:Class="WCSamples.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="mouseMoveWithPointer"
Height="400"
Width="500"
>
<Canvas MouseMove="MouseMoveHandler"
Background="LemonChiffon">
<Ellipse Name="ellipse" Fill="LightBlue"
Width="100" Height="100"/>
</Canvas>
</Window>
다음 코드 숨김은 MouseMove 이벤트 처리기를 만듭니다. 마우스 포인터가 움직이면 Ellipse의 높이와 너비가 증가하고 줄어듭니다.
// raised when the mouse pointer moves.
// Expands the dimensions of an Ellipse when the mouse moves.
private void MouseMoveHandler(object sender, MouseEventArgs e)
{
// Get the x and y coordinates of the mouse pointer.
System.Windows.Point position = e.GetPosition(this);
double pX = position.X;
double pY = position.Y;
// Sets the Height/Width of the circle to the mouse coordinates.
ellipse.Width = pX;
ellipse.Height = pY;
}
' raised when the mouse pointer moves.
' Expands the dimensions of an Ellipse when the mouse moves.
Private Sub OnMouseMoveHandler(ByVal sender As Object, ByVal e As MouseEventArgs)
'Get the x and y coordinates of the mouse pointer.
Dim position As System.Windows.Point
position = e.GetPosition(Me)
Dim pX As Double
pX = position.X
Dim pY As Double
pY = position.Y
'Set the Height and Width of the Ellipse to the mouse coordinates.
ellipse1.Height = pY
ellipse1.Width = pX
End Sub
참고하십시오
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET Desktop feedback