Set cursor area : Cursor « Windows Presentation Foundation « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Windows Presentation Foundation » Cursor 
24.77.1.Set cursor area
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="cursors" Height="450" Width="600" Loaded="OnLoaded">
  <Window.Resources>
    <Style TargetType="{x:Type RadioButton}">
      <Setter Property="Margin" Value="3" />
    </Style>
    <Style TargetType="{x:Type Label}">
      <Setter Property="FontSize" Value="14" />
      <Setter Property="HorizontalAlignment" Value="Center" />
    </Style>
    <Style TargetType="{x:Type Border}">
      <Setter Property="BorderBrush" Value="LightBlue" />
      <Setter Property="BorderThickness" Value="2" />
      <Setter Property="Margin" Value="10" />
    </Style>
  </Window.Resources>

  <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" />
          </ComboBox>
        </StackPanel>
        <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>
    <Border Name="DisplayArea" Height="250" Width="400" Margin="20" Background="AliceBlue">
      <Label HorizontalAlignment="Center">
        Move Mouse Pointer Over This Area
      </Label>
    </Border>
  </StackPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Input;
using System.IO;
using System.Collections;


namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        Cursor CustomCursor;
       
        public Window1()
        {
            CustomCursor = new Cursor(Directory.GetCurrentDirectory() +Path.DirectorySeparatorChar + "CustomCursor.cur");
        }
        public void CursorTypeChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox source = e.Source as ComboBox;

            if (source != null)
            {
                ComboBoxItem selectedCursor = source.SelectedItem as ComboBoxItem;
                
                switch (selectedCursor.Content.ToString())
                {
                    case "AppStarting":
                        DisplayArea.Cursor = Cursors.AppStarting;
                        break;
                    case "ArrowCD":                        
                        DisplayArea.Cursor = Cursors.ArrowCD;
                        break;
                    default:
                        break;
                }

                if (cursorScopeElementOnly == false)
                {
                    Mouse.OverrideCursor = DisplayArea.Cursor;
                }
            }
        }
        public void CursorScopeSelected(object sender, RoutedEventArgs e)
        {
            RadioButton source = e.Source as RadioButton;

            if (source != null)
            {
                if (source.Name == "rbScopeElement")
                {
                    cursorScopeElementOnly = true;
                    Mouse.OverrideCursor = null;
                }
                if (source.Name == "rbScopeApplication")
                {
                   cursorScopeElementOnly = false;
                   Mouse.OverrideCursor = DisplayArea.Cursor;
                }
            }
        }
        public void OnLoaded(object sender, RoutedEventArgs e)
        {
            ((ComboBoxItem)CursorSelector.Items[0]).IsSelected = true;
        }
        private bool cursorScopeElementOnly = true;
    }
}
WPF Set Cursor Area
24.77.Cursor
24.77.1.Set cursor areaSet cursor area
24.77.2.Changing the cursor of the Border control by setting the Cursor propertyChanging the cursor of the Border control by setting the Cursor property
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.