방법: 커서 형식 변경

이 예제에서는 특정 요소 및 애플리케이션에 대한 마우스 포인터의 Cursor를 변경하는 방법을 보여줍니다.

이 예제는 XAML(Extensible Application Markup Language) 파일 및 코드 숨김 파일로 구성됩니다.

예제

사용자 인터페이스가 생성되는데, 이것은 원하는 Cursor를 선택하기 위한 ComboBox, 커서 변경이 단일 요소에만 적용되는지 아니면 전체 애플리케이션에 적용되는지 여부를 결정하는 한 쌍의 RadioButton 개체 그리고 새 커서가 적용되는 요소인 Border로 구성됩니다.

<StackPanel>
  <Border Width="300">
    <StackPanel Orientation="Horizontal"
                HorizontalAlignment="Center">
      <StackPanel Margin="10">
        <Label HorizontalAlignment="Left">Cursor Type</Label>
        <ComboBox Width="100"
                  SelectionChanged="CursorTypeChanged"
                  HorizontalAlignment="Left"
                  Name="CursorSelector">
          <ComboBoxItem Content="AppStarting" />
          <ComboBoxItem Content="ArrowCD" />
          <ComboBoxItem Content="Arrow" />
          <ComboBoxItem Content="Cross" />
          <ComboBoxItem Content="HandCursor" />
          <ComboBoxItem Content="Help" />
          <ComboBoxItem Content="IBeam" />
          <ComboBoxItem Content="No" />
          <ComboBoxItem Content="None" />
          <ComboBoxItem Content="Pen" />
          <ComboBoxItem Content="ScrollSE" />
          <ComboBoxItem Content="ScrollWE" />
          <ComboBoxItem Content="SizeAll" />
          <ComboBoxItem Content="SizeNESW" />
          <ComboBoxItem Content="SizeNS" />
          <ComboBoxItem Content="SizeNWSE" />
          <ComboBoxItem Content="SizeWE" />
          <ComboBoxItem Content="UpArrow" />
          <ComboBoxItem Content="WaitCursor" />
          <ComboBoxItem Content="Custom" />
        </ComboBox>
      </StackPanel>
      <!-- The user can select different cursor types using this ComboBox -->
      <StackPanel Margin="10">
        <Label HorizontalAlignment="Left">Scope of Cursor</Label>
        <StackPanel>
          <RadioButton Name="rbScopeElement" IsChecked="True"
                       Checked="CursorScopeSelected">Display Area Only</RadioButton>
          <RadioButton Name="rbScopeApplication"
                       Checked="CursorScopeSelected">Entire Appliation</RadioButton>
        </StackPanel>
      </StackPanel>
    </StackPanel>
  </Border>
  <!-- When the mouse pointer is over this Border -->
  <!-- the selected cursor type is shown -->
  <Border Name="DisplayArea" Height="250" Width="400"
          Margin="20" Background="AliceBlue">
    <Label HorizontalAlignment="Center">
      Move Mouse Pointer Over This Area
    </Label>
  </Border>
</StackPanel>

다음 코드 숨김은 커서 형식이 ComboBox에서 변경될 때 호출되는 SelectionChanged 이벤트 처리기를 생성합니다. switch 문은 커서 이름을 필터링하고 DisplayArea라는 이름의 Border에서 Cursor 속성을 설정합니다.

커서 변경 내용이 "전체 애플리케이션"으로 설정된 경우, OverrideCursor 속성은 Border 컨트롤의 Cursor 속성으로 설정됩니다. 이렇게 하면 커서가 전체 애플리케이션에 대해 변경됩니다.

private void CursorTypeChanged(object sender, SelectionChangedEventArgs e)
{
    ComboBox source = e.Source as ComboBox;

    if (source != null)
    {
        ComboBoxItem selectedCursor = source.SelectedItem as ComboBoxItem;

        // Changing the cursor of the Border control
        // by setting the Cursor property
        switch (selectedCursor.Content.ToString())
        {
            case "AppStarting":
                DisplayArea.Cursor = Cursors.AppStarting;
                break;
            case "ArrowCD":
                DisplayArea.Cursor = Cursors.ArrowCD;
                break;
            case "Arrow":
                DisplayArea.Cursor = Cursors.Arrow;
                break;
            case "Cross":
                DisplayArea.Cursor = Cursors.Cross;
                break;
            case "HandCursor":
                DisplayArea.Cursor = Cursors.Hand;
                break;
            case "Help":
                DisplayArea.Cursor = Cursors.Help;
                break;
            case "IBeam":
                DisplayArea.Cursor = Cursors.IBeam;
                break;
            case "No":
                DisplayArea.Cursor = Cursors.No;
                break;
            case "None":
                DisplayArea.Cursor = Cursors.None;
                break;
            case "Pen":
                DisplayArea.Cursor = Cursors.Pen;
                break;
            case "ScrollSE":
                DisplayArea.Cursor = Cursors.ScrollSE;
                break;
            case "ScrollWE":
                DisplayArea.Cursor = Cursors.ScrollWE;
                break;
            case "SizeAll":
                DisplayArea.Cursor = Cursors.SizeAll;
                break;
            case "SizeNESW":
                DisplayArea.Cursor = Cursors.SizeNESW;
                break;
            case "SizeNS":
                DisplayArea.Cursor = Cursors.SizeNS;
                break;
            case "SizeNWSE":
                DisplayArea.Cursor = Cursors.SizeNWSE;
                break;
            case "SizeWE":
                DisplayArea.Cursor = Cursors.SizeWE;
                break;
            case "UpArrow":
                DisplayArea.Cursor = Cursors.UpArrow;
                break;
            case "WaitCursor":
                DisplayArea.Cursor = Cursors.Wait;
                break;
            case "Custom":
                DisplayArea.Cursor = CustomCursor;
                break;
            default:
                break;
        }

        // If the cursor scope is set to the entire application
        // Use OverrideCursor to force the cursor for all elements
        if (cursorScopeElementOnly == false)
        {
            Mouse.OverrideCursor = DisplayArea.Cursor;
        }
    }
}
' When the Radiobox changes, a new cursor type is set
Private Sub CursorTypeChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)

    Dim item As String = CType(e.Source, ComboBox).SelectedItem.Content.ToString()

    Select Case item
        Case "AppStarting"
            DisplayArea.Cursor = Cursors.AppStarting
        Case "ArrowCD"
            DisplayArea.Cursor = Cursors.ArrowCD
        Case "Arrow"
            DisplayArea.Cursor = Cursors.Arrow
        Case "Cross"
            DisplayArea.Cursor = Cursors.Cross
        Case "HandCursor"
            DisplayArea.Cursor = Cursors.Hand
        Case "Help"
            DisplayArea.Cursor = Cursors.Help
        Case "IBeam"
            DisplayArea.Cursor = Cursors.IBeam
        Case "No"
            DisplayArea.Cursor = Cursors.No
        Case "None"
            DisplayArea.Cursor = Cursors.None
        Case "Pen"
            DisplayArea.Cursor = Cursors.Pen
        Case "ScrollSE"
            DisplayArea.Cursor = Cursors.ScrollSE
        Case "ScrollWE"
            DisplayArea.Cursor = Cursors.ScrollWE
        Case "SizeAll"
            DisplayArea.Cursor = Cursors.SizeAll
        Case "SizeNESW"
            DisplayArea.Cursor = Cursors.SizeNESW
        Case "SizeNS"
            DisplayArea.Cursor = Cursors.SizeNS
        Case "SizeNWSE"
            DisplayArea.Cursor = Cursors.SizeNWSE
        Case "SizeWE"
            DisplayArea.Cursor = Cursors.SizeWE
        Case "UpArrow"
            DisplayArea.Cursor = Cursors.UpArrow
        Case "WaitCursor"
            DisplayArea.Cursor = Cursors.Wait
        Case "Custom"
            DisplayArea.Cursor = CustomCursor
    End Select

    ' if the cursor scope is set to the entire application
    ' use OverrideCursor to force the cursor for all elements
    If (cursorScopeElementOnly = False) Then
        Mouse.OverrideCursor = DisplayArea.Cursor
    End If


End Sub

참고 항목