CursorType光标类型

clipboard.png

1、实现效果

  1. 设置鼠标样式在全局还某一区域
  2. 自定义鼠标样式

2、关注词

  1. Mouse.OverrideCursor
  2. new Cursor()

3、实现流程
自定义鼠标加载:

// Setting CustomCursor to the CustomCursor.cur file.
// This assumes the file CustomCursor.cur has been added to the project
// as a resource.  One way to accomplish this to add the following 
// ItemGroup section to the project file
//
//  <ItemGroup>
//    <Content Include="CustomCursor.cur">
//       <CopyToOutputDirectory>Always</CopyToOutputDirectory>
//    </Content>
//  </ItemGroup>
_customCursor = new Cursor(Directory.GetCurrentDirectory() +
                           @"\" +
                           "CustomCursor.cur");

根据ComboBoxItem的项选择对应鼠标样式
设置显示区域.Cursor值,同时更新是否全局鼠标样式

case "WaitCursor":
    DisplayArea.Cursor = Cursors.Wait;
    break;
case "Custom":
    DisplayArea.Cursor = _customCursor;
    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;
}

鼠标显示区域切换:

  1. 若Mouse.OverrideCursor = null,则默认全局应用设定样式
  2. 如Mouse.OverrideCursor = DisplayArea.Cursor;则指定元素重写鼠标样式
private void CursorScopeSelected(object sender, RoutedEventArgs e)
{
    var source = e.Source as RadioButton;

    if (source != null)
    {
        if (source.Name == "rbScopeElement")
        {
            // Setting the element only scope flag to true
            _cursorScopeElementOnly = true;

            // Clearing out the OverrideCursor.  
            Mouse.OverrideCursor = null;
        }
        if (source.Name == "rbScopeApplication")
        {
            // Setting the element only scope flag to false
            _cursorScopeElementOnly = false;

            // Forcing the cursor for all elements. 
            Mouse.OverrideCursor = DisplayArea.Cursor;
        }
    }
}

ProgramaticFocusControl操作控件焦点程序

clipboard.png

1、实现效果:

  1. 控件焦点只在指定区域开始,通过方向键、Tab移动控件焦点,同时设置焦点时控件颜色改变。
  2. 更加导航方向改变下一个角度元素的属性

2、关键词:

  1. Keyboard.Focus+Keyboard.FocusedElement
  2. FocusNavigationDirection+TraversalRequest

3、静态组织:
界面不需焦点区域(左侧)设置UIElment.Focusable=false
设置触发器实现:有焦点,则背景色改变。
4、实现流程:
加载时设置指定元素角度开始

private void OnLoaded(object sender, RoutedEventArgs e)
{
    // Sets keyboard focus on the first Button in the sample.
    Keyboard.Focus(firstButton);
}

焦点方向改变时(RadioButton事件):

  1. 转换string值为焦点导航方向 枚举值
private void OnFocusSelected(object sender, RoutedEventArgs e)
{
    var source = e.Source as RadioButton;

    if (source != null)
    {
        _focusMoveValue = (FocusNavigationDirection) Enum.Parse(
            typeof (FocusNavigationDirection), (string) source.Content);
    }
}

寻找将要应用下一个焦点的元素,并修改此元素属性

  1. 获取当前焦点元素Keyboard.FocusedElement
  2. 获取当前元素的下一个焦点元素(方向导航的)UIElement.PredictFocus(方向)。
  3. 若为Control元素,改变其属性
private void OnPredictFocus(object sender, RoutedEventArgs e)
{
    DependencyObject predictionElement = null;

    var elementWithFocus = Keyboard.FocusedElement as UIElement;

    if (elementWithFocus != null)
    {
        // Only these four directions are currently supported
        // by PredictFocus, so we need to filter on these only.
        if ((_focusMoveValue == FocusNavigationDirection.Up) ||
            (_focusMoveValue == FocusNavigationDirection.Down) ||
            (_focusMoveValue == FocusNavigationDirection.Left) ||
            (_focusMoveValue == FocusNavigationDirection.Right))
        {
            // Get the element which would receive focus if focus were changed.
            predictionElement = elementWithFocus.PredictFocus(_focusMoveValue);

            var controlElement = predictionElement as Control;

            // If a ContentElement.
            if (controlElement != null)
            {
                controlElement.Foreground = Brushes.DarkBlue;
                controlElement.FontSize += 10;
                controlElement.FontWeight = FontWeights.ExtraBold;

                // Fields used to reset the UI when the mouse 
                // button is released.
                _focusPredicted = true;
                _predictedControl = controlElement;
            }
        }
    }
}

焦点移往下一个元素:

  1. 获取FocusNavigationDirection和移焦请求TraversalRequest
  2. 获取当前焦点元素。
  3. 执行MoveFocus(request)
private void OnMoveFocus(object sender, RoutedEventArgs e)
{
    // Creating a FocusNavigationDirection object and setting it to a
    // local field that contains the direction selected.
    var focusDirection = _focusMoveValue;

    // MoveFocus takes a TraveralReqest as its argument.
    var request = new TraversalRequest(focusDirection);

    // Gets the element with keyboard focus.
    var elementWithFocus = Keyboard.FocusedElement as UIElement;

    // Change keyboard focus.
    elementWithFocus?.MoveFocus(request);
}

恢复下一个焦点元素的属性改变:

// Resets the UI after PredictFocus changes the UI.
private void OnPredictFocusMouseUp(object sender, RoutedEventArgs e)
{
    if (_focusPredicted)
    {
        _predictedControl.Foreground = Brushes.Black;
        _predictedControl.FontSize -= 10;
        _predictedControl.FontWeight = FontWeights.Normal;

        _focusPredicted = false;
    }
}

李志玮
22 声望34 粉丝

求索~~


引用和评论

0 条评论