업데이트: 2007년 11월
이 예제에서는 화면 위에서 마우스 포인터를 이동할 때 개체의 크기를 변경하는 방법을 보여 줍니다.
예제에는 UI(사용자 인터페이스)를 만드는 XAML(Extensible Application Markup Language) 파일과 이벤트 처리기를 만드는 코드 숨김 파일이 포함됩니다. 전체 샘플을 보려면 마우스 포인터로 개체 이동 샘플을 참조하십시오.
예제
다음 XAML에서는 StackPanel 내부에 Ellipse로 구성된 UI를 만들고 MouseMove 이벤트에 대한 이벤트 처리기를 연결합니다.
<Window x:Class="WCSamples.Window1"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://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 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
// 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;
}