CaptureUnCaptureMouse捕获鼠标

clipboard.png

关注点:
1、 获取 捕获鼠标按钮的名字 的显示

  1. 两种方法:一是直接获取GotMouseCapture路由事件中确定事件引发者e.source的名字
  2. 二是新建一个IInputElement,获取鼠标捕获的元素Mouse.Captured;得到按钮的引用即可
// GotMouseCapture event handler
// MouseEventArgs.source is the element that has mouse capture
private void ButtonGotMouseCapture(object sender, MouseEventArgs e)
{
    var source = e.Source as Button;

    if (source != null)
    {
        // Update the Label that displays the sample results.
        lblHasMouseCapture.Content = source.Name;
    }
    // Another way to get the element with Mouse Capture
    // is to use the static Mouse.Captured property.
    else
    {
        // Mouse.Capture returns an IInputElement.
        IInputElement captureElement;

        captureElement = Mouse.Captured;

        // Update the Label that displays the element with mouse capture.
        lblHasMouseCapture.Content = captureElement.ToString();
    }
}

2、 点击Capture Mouse按钮时,使RaidoButton指示的鼠标 执行 被鼠标捕获状态。此时鼠标变大??就像鼠标放于按钮上状态。。。答:根据按钮上鼠标捕获定义,意味着捕获鼠标就是默认鼠标在按钮上拖移状态。

private void OnCaptureMouseRequest(object sender, RoutedEventArgs e)
{
    Mouse.Capture(_elementToCapture);
}

private IInputElement _elementToCapture;

private void OnRadioButtonSelected(object sender, RoutedEventArgs e)
{
    var source = e.Source as RadioButton;

    if (source != null)
    {
        switch (source.Content.ToString())
        {
            case "1":
                _elementToCapture = Button1;
                break;
            case "2":
                _elementToCapture = Button2;
                break;
            case "3":
                _elementToCapture = Button3;
                break;
            case "4":
                _elementToCapture = Button4;
                break;
        }
    }
}

CommandSourceControlUsingSystemTime使用系统时间做比较值的命令源控件

clipboard.png

1、实现效果

  1. 滑块的滚动、点击的增减值设置
  2. 同步显示当前时间秒数
  3. 执行命令绑定方法,及判断命令查询状态

2、关键词

  1. CommandBinding
  2. Slider.DecreaseSmall.Execute
  3. Timer.Elasped

3、静态组织
window的命令绑定设置:

  1. Command绑定到window的一个静态自定义路由命令
  2. 命令附加执行方法Executed,自定义方法附加到此句柄
  3. 命令能否执行CanExecute,附加判断方法,返回bool
<!-- Command Binding for the Custom Command -->
<Window.CommandBindings>
    <CommandBinding Command="{x:Static local:MainWindow.CustomCommand}"
                Executed="CustomCommandExecuted"
                CanExecute="CustomCommandCanExecute" />
</Window.CommandBindings>

完整滑块状态设置:

<Border BorderBrush="DarkBlue"
    BorderThickness="1"
    Height="75"
    Width="425">
    <StackPanel Orientation="Horizontal">
        <Button Command="Slider.DecreaseSmall"
        CommandTarget="{Binding ElementName=secondSlider}"
        Height="25"
        Width="25"
        Content="-"/>
        <Label VerticalAlignment="Center">0</Label>
        <Slider Name="secondSlider"
        Minimum="0"
        Maximum="60"
        Value="15"
        TickFrequency="5"
        Height="50"
        Width="275" 
        TickPlacement="BottomRight"
        LargeChange="5"
        SmallChange="5" 
        AutoToolTipPlacement="BottomRight" 
        AutoToolTipPrecision="0"
        MouseWheel="OnSliderMouseWheel"
        MouseUp="OnSliderMouseUp" />
        <Label VerticalAlignment="Center">60</Label>
        <Button Command="Slider.IncreaseSmall"
        CommandTarget="{Binding ElementName=secondSlider}"
        Height="25"
        Width="25"
        Content="+"/>
    </StackPanel>
</Border>

4、动态流程
设置计时器:显示随时间秒数更新的时间数字

  1. 新建计时器类Timer,引发时间间隔事件Elapsed
  2. 设置时间间隔及激活计时器
  3. 线程调度执行同步委托
  4. Label显示现在时间秒数,同时强制更新注册命令状态
// System Timer setup.
var timer = new Timer();
timer.Elapsed += timer_Elapsed;
timer.Interval = 1000;
timer.Enabled = true;

// System.Timers.Timer.Elapsed handler
// Places the delegate onto the UI Thread's Dispatcher
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    // Place delegate on the Dispatcher.
    Dispatcher.Invoke(DispatcherPriority.Normal,
        new TimerDispatcherDelegate(TimerWorkItem));
}

// Method to place onto the UI thread's dispatcher.
// Updates the current second display and calls 
// InvalidateRequerySuggested on the CommandManager to force the
// Command to raise the CanExecuteChanged event.
private void TimerWorkItem()
{
    // Update current second display.
    lblSeconds.Content = DateTime.Now.Second;

    // Forcing the CommandManager to raie the RequerySuggested event.
    CommandManager.InvalidateRequerySuggested();
}

判断命令能否执行:

// CanExecute Event Handler.
//
// True if the current seconds are greater than the target value that is 
// set on the Slider, which is defined in the XMAL file.
private void CustomCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    if (secondSlider != null)
    {
        e.CanExecute = DateTime.Now.Second > secondSlider.Value;
    }
    else
    {
        e.CanExecute = false;
    }
}

滑块中轮滚动事件,引起滑块值增减固定值

  1. 获取滑块引用
  2. 判断滚动方向值,增减滑块值Slider.DecreaseSmall.Execute
private void OnSliderMouseWheel(object sender, MouseWheelEventArgs e)
{
    var source = e.Source as Slider;
    if (source != null)
    {
        if (e.Delta > 0)
        {
            // Execute the Slider DecreaseSmall RoutedCommand.
            // The slider.value propety is passed as the command parameter.
            Slider.DecreaseSmall.Execute(
                source.Value, source);
        }
        else
        {
            // Execute the Slider IncreaseSmall RoutedCommand.
            // The slider.value propety is passed as the command parameter.
            Slider.IncreaseSmall.Execute(
                source.Value, source);
        }
    }
}

ps:此示例的e.Prameter为空,不显示秒数。


李志玮
22 声望34 粉丝

求索~~


引用和评论

0 条评论