How to: Change the Cursor Type


This example shows how to change the Cursor of the mouse pointer for a specific element and for the application.

This example consists of a Extensible Application Markup Language (XAML) file and a code behind file.

The user interface is created, which consists of a ComboBox to select the desired Cursor, a pair of RadioButton objects to determine if the cursor change applies to only a single element or applies to the entire application, and a Border which is the element that the new cursor is applied to.

    <Border Width="300">
      <StackPanel Orientation="Horizontal"
        <StackPanel Margin="10">
          <Label HorizontalAlignment="Left">Cursor Type</Label>
          <ComboBox Width="100"
            <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" />
        <!-- The user can select different cursor types using this ComboBox -->
        <StackPanel Margin="10">
          <Label HorizontalAlignment="Left">Scope of Cursor</Label>
            <RadioButton Name="rbScopeElement" IsChecked="True"
                         Checked="CursorScopeSelected">Display Area Only</RadioButton>
            <RadioButton Name="rbScopeApplication"
                         Checked="CursorScopeSelected">Entire Appliation</RadioButton>
    <!-- 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

The following code behind creates a SelectionChanged event handler which is called when the cursor type is changed in the ComboBox. A switch statement filters on the cursor name and sets the Cursor property on the Border which is named DisplayArea.

If the cursor change is set to "Entire Application", the OverrideCursor property is set to the Cursor property of the Border control. This forces the cursor to change for the whole application.

        ' 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

Input Overview