背水一战 Windows 10 (68) - 控件(控件基类): UIElement - Pointer 相关事件, Tap 相关事件, Key 相关事件, Focus 相关事件
2021-05-06 21:30
标签:c++ html5 hand demo enc 介绍 namespace ati 1.0 [源码下载] Controls/BaseControl/UIElementDemo/PointerDemo.xaml.cs Controls/BaseControl/UIElementDemo/TapDemo.xaml.cs Controls/BaseControl/UIElementDemo/KeyDemo.xaml.cs Controls/BaseControl/UIElementDemo/FocusDemo.xaml.cs 背水一战 Windows 10 (68) - 控件(控件基类): UIElement - Pointer 相关事件, Tap 相关事件, Key 相关事件, Focus 相关事件 标签:c++ html5 hand demo enc 介绍 namespace ati 1.0 原文地址:http://www.cnblogs.com/webabcd/p/7654429.html
作者:webabcd
介绍
背水一战 Windows 10 之 控件(控件基类 - UIElement )
示例
1、演示 Pointer 相关事件
Controls/BaseControl/UIElementDemo/PointerDemo.xamlPage
x:Class="Windows10.Controls.BaseControl.UIElementDemo.PointerDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.BaseControl.UIElementDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
Grid Background="Transparent">
Grid Margin="10 0 10 10">
Rectangle Name="rectangle" Width="400" Height="100" Fill="Orange" HorizontalAlignment="Left" VerticalAlignment="Top" />
ScrollViewer Margin="0 110 0 10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
TextBlock Name="lblMsg" TextWrapping="Wrap" />
ScrollViewer>
Grid>
Grid>
Page>
/*
* UIElement - UIElement(继承自 DependencyObject, 请参见 /Controls/BaseControl/DependencyObjectDemo/)
* PointerEntered - 指针进入时触发的事件
* PointerExited - 指针离开时触发的事件
* PointerPressed - 指针按下时触发的事件
* PointerReleased - 指针释放时触发的事件
* PointerCanceled - 接触中的指针异常地失去接触时触发的事件(比如修改分辨率,用户注销等)
* 所以要注意:PointerPressed 和 PointerReleased 也许并不会成对出现,因为如果触发了 PointerCanceled 则不会再触发 PointerReleased
* PointerMoved - 指针移动时触发的事件
* PointerWheelChanged - 滚轮操作时触发的事件
* PointerCaptureLost - 指针捕获丢失时触发的事件
* CapturePointer(Pointer value) - 捕获此 UIElement 上的指针,即在 UIElement 之外也可以响应 PointerReleased 事件
* ReleasePointerCapture(Pointer value) - 释放此 UIElement 上的指定指针的捕获
* ReleasePointerCaptures() - 释放此 UIElement 上的全部指针的捕获
* IReadOnlyList
2、演示 Tap 相关事件
Controls/BaseControl/UIElementDemo/TapDemo.xamlPage
x:Class="Windows10.Controls.BaseControl.UIElementDemo.TapDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.BaseControl.UIElementDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
Grid Background="Transparent">
StackPanel Margin="10 0 10 10">
Rectangle Name="rectangle" Width="400" Height="100" Fill="Orange" HorizontalAlignment="Left" Margin="5" />
TextBlock Name="lblMsg" TextWrapping="Wrap" Margin="5" />
StackPanel>
Grid>
Page>
/*
* UIElement - UIElement(继承自 DependencyObject, 请参见 /Controls/BaseControl/DependencyObjectDemo/)
* IsTapEnabled, IsDoubleTapEnabled, IsRightTapEnabled, IsHoldingEnabled - 是否允许监听相关的事件
* Tapped, DoubleTapped, RightTapped, Holding - Tap 相关事件
*
* TappedRoutedEventArgs - Tap 路由事件的事件参数
* Handled - 是否将事件标记为已处理
* PointerDeviceType - 指针设备的类型(Touch, Pen, Mouse)
* GetPosition(UIElement relativeTo) - 返回当前指针相对于指定元素的位置
*
* DoubleTappedRoutedEventArgs - DoubleTap 路由事件的事件参数
* Handled - 是否将事件标记为已处理
* PointerDeviceType - 指针设备的类型(Touch, Pen, Mouse)
* GetPosition(UIElement relativeTo) - 返回当前指针相对于指定元素的位置
*
* RightTappedRoutedEventArgs - RightTap 路由事件的事件参数
* Handled - 是否将事件标记为已处理
* PointerDeviceType - 指针设备的类型(Touch, Pen, Mouse)
* GetPosition(UIElement relativeTo) - 返回当前指针相对于指定元素的位置
*
* HoldingRoutedEventArgs - Holding 路由事件的事件参数
* Handled - 是否将事件标记为已处理
* PointerDeviceType - 指针设备的类型(Touch, Pen, Mouse)
* GetPosition(UIElement relativeTo) - 返回当前指针相对于指定元素的位置
* HoldingState - Holding 状态(Windows.UI.Input.HoldingState 枚举)
* Started, Completed, Canceled
*
*
* 本例用于演示 UIElement 的 Tap 相关事件的应用
*
*
* 注:关于 Manipulate Pointer Tap 的区别如下
* 1、Manipulate 是最底层,Pointer 在中间,Tap 是最高层,所以会先走 Manipulate 事件,再走 Pointer 事件,最后走 Tap 事件
* 2、如果高层的事件被触发,最相对于它的底层的事件也会被触发,反之则不一定
* 3、使用原则是能在高层处理的事件尽量在高层处理(开发会简单些)
*/
using System;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
namespace Windows10.Controls.BaseControl.UIElementDemo
{
public sealed partial class TapDemo : Page
{
public TapDemo()
{
this.InitializeComponent();
rectangle.IsTapEnabled = true; // 默认值就是 true
rectangle.IsDoubleTapEnabled = true; // 默认值就是 true
rectangle.IsRightTapEnabled = true; // 默认值就是 true
rectangle.IsHoldingEnabled = true; // 默认值就是 true
rectangle.Tapped += rectangle_Tapped;
rectangle.DoubleTapped += rectangle_DoubleTapped;
rectangle.RightTapped += rectangle_RightTapped;
rectangle.Holding += rectangle_Holding; // 鼠标操作时不会触发 Holding 事件
}
void rectangle_Holding(object sender, HoldingRoutedEventArgs e)
{
lblMsg.Text += "Holding";
lblMsg.Text += Environment.NewLine;
}
void rectangle_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
lblMsg.Text += "RightTapped";
lblMsg.Text += Environment.NewLine;
}
void rectangle_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
lblMsg.Text += "DoubleTapped";
lblMsg.Text += Environment.NewLine;
}
void rectangle_Tapped(object sender, TappedRoutedEventArgs e)
{
lblMsg.Text += "Tapped";
lblMsg.Text += Environment.NewLine;
}
}
}
3、演示 Key 相关事件
Controls/BaseControl/UIElementDemo/KeyDemo.xamlPage
x:Class="Windows10.Controls.BaseControl.UIElementDemo.KeyDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.BaseControl.UIElementDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
Grid Background="Transparent">
TextBlock Name="lblMsg" Margin="5" />
TextBox Name="textBox" Margin="5 25 5 5" TextWrapping="Wrap" KeyDown="textBox_KeyDown" KeyUp="textBox_KeyUp" />
Grid>
Page>
/*
* UIElement - UIElement(继承自 DependencyObject, 请参见 /Controls/BaseControl/DependencyObjectDemo/)
* KeyDown - 按键按下时触发的事件
* KeyUp - 按键抬起时触发的事件
*
*
* KeyRoutedEventArgs - 按键的事件参数
* Handled - 是否将事件标记为已处理
* Key - 按键枚举(比如 A, B, C, D, LeftControl 之类的,详细参见 VirtualKey 枚举)
* OriginalKey - 按键枚举(如果输入设备未映射到 Key 则可以通过此属性获取)
* KeyStatus - 按键的状态(一个 CorePhysicalKeyStatus 类型的对象,有好多字段,详细参见文档)
*
*
* 注:全局键盘事件监听请参见:/Controls/BaseControl/DependencyObjectDemo/CoreDispatcherDemo.xaml.cs
*
*
* 本例用于演示 UIElement 的 Key 相关事件的应用
*/
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
namespace Windows10.Controls.BaseControl.UIElementDemo
{
public sealed partial class KeyDemo : Page
{
public KeyDemo()
{
this.InitializeComponent();
}
// 通过输入法输入是不会触发此事件的
private void textBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
lblMsg.Text = $"IsExtendedKey:{e.KeyStatus.IsExtendedKey}, IsKeyReleased:{e.KeyStatus.IsKeyReleased}, IsMenuKeyDown:{e.KeyStatus.IsMenuKeyDown}, RepeatCount:{e.KeyStatus.RepeatCount}, ScanCode:{e.KeyStatus.ScanCode}, WasKeyDown:{e.KeyStatus.WasKeyDown}";
textBox.Text += e.Key.ToString();
e.Handled = true;
}
// 通过输入法输入是不会触发此事件的
private void textBox_KeyUp(object sender, KeyRoutedEventArgs e)
{
lblMsg.Text = $"IsExtendedKey:{e.KeyStatus.IsExtendedKey}, IsKeyReleased:{e.KeyStatus.IsKeyReleased}, IsMenuKeyDown:{e.KeyStatus.IsMenuKeyDown}, RepeatCount:{e.KeyStatus.RepeatCount}, ScanCode:{e.KeyStatus.ScanCode}, WasKeyDown:{e.KeyStatus.WasKeyDown}";
e.Handled = true;
}
}
}
4、演示 Focus 相关事件
Controls/BaseControl/UIElementDemo/FocusDemo.xamlPage
x:Class="Windows10.Controls.BaseControl.UIElementDemo.FocusDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.BaseControl.UIElementDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
Grid Background="Transparent">
StackPanel Margin="5">
Button Name="button1" Content="button 1" Margin="5" />
Button Name="button2" Content="button 2" Margin="5" />
TextBlock Name="lblMsg" Margin="5" />
StackPanel>
Grid>
Page>
/*
* UIElement - UIElement(继承自 DependencyObject, 请参见 /Controls/BaseControl/DependencyObjectDemo/)
* GotFocus - 获取焦点时触发的事件(通过 api 获取焦点,或者通过鼠标获取焦点,或者通过 Tab 键获取焦点都会触发 GotFocus 事件)
* LostFocus - 丢失焦点时触发的事件
*
*
* 本例用于演示 UIElement 的 Focus 相关事件的应用
*/
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace Windows10.Controls.BaseControl.UIElementDemo
{
public sealed partial class FocusDemo : Page
{
public FocusDemo()
{
this.InitializeComponent();
button1.GotFocus += Button1_GotFocus;
button1.LostFocus += Button1_LostFocus;
button2.GotFocus += Button2_GotFocus;
button2.LostFocus += Button2_LostFocus;
this.Loaded += FocusDemo_Loaded;
}
private void FocusDemo_Loaded(object sender, RoutedEventArgs e)
{
// 通过 api 获取焦点
button2.Focus(FocusState.Programmatic);
}
private void Button1_GotFocus(object sender, RoutedEventArgs e)
{
lblMsg.Text += "Button1_GotFocus";
lblMsg.Text += Environment.NewLine;
}
private void Button1_LostFocus(object sender, RoutedEventArgs e)
{
lblMsg.Text += "Button1_LostFocus";
lblMsg.Text += Environment.NewLine;
}
private void Button2_GotFocus(object sender, RoutedEventArgs e)
{
lblMsg.Text += "Button2_GotFocus";
lblMsg.Text += Environment.NewLine;
}
private void Button2_LostFocus(object sender, RoutedEventArgs e)
{
lblMsg.Text += "Button2_LostFocus";
lblMsg.Text += Environment.NewLine;
}
}
}
OK
[源码下载]
文章标题:背水一战 Windows 10 (68) - 控件(控件基类): UIElement - Pointer 相关事件, Tap 相关事件, Key 相关事件, Focus 相关事件
文章链接:http://soscw.com/index.php/essay/83398.html