
键盘事件的类型分为以下几种键盘事件
键盘事件的执行顺序:
PrevieKeyDown
KeyDown
PreviewTextInput
TextInput
PreviewKeyUp
KeyUp
以下示例代码用于验证:
分别向文本框添加PreviewKeyDown / KeyDown / PreviewTextInput / PreviewKeyUp / KeyUp / TextChanged事件
请输入文本:

该事件对应于其自己的程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
//清空ListBox的内容
private void btn_clear_Click(object sender, RoutedEventArgs e)
{
this.list_box.Items.Clear();
}
//文本框按下事件
private void txt_input_KeyEnvent(object sender, KeyEventArgs e)
{
string message = "事件:"+e.RoutedEvent+"-键:"+e.Key;
this.list_box.Items.Add(message);
}
// 文本输入事件
private void txt_input_TextInput(object sender, TextCompositionEventArgs e)
{
string message = "事件:" + e.RoutedEvent + "-值:" + e.Text;
this.list_box.Items.Add(message);
}
private void txt_input_TextChanged(object sender, TextChangedEventArgs e)
{
string message = "事件:" + e.RoutedEvent ;
this.list_box.Items.Add(message);
}
}
}
运行程序并查看结果:

在文本框中输入“ s”。从列表框的输出中,您可以看到先执行“按下事件”,然后执行“文本输入事件”,然后执行“键释放事件”,相应的通道事件始终在相应的冒泡事件之前
以上示例有助于了解隧道事件,冒泡事件和键盘事件
鼠标事件
鼠标单击
捕获鼠标
鼠标拖放

鼠标事件是:
MouseMove事件MouseMove
在界面上定义一个矩形,将鼠标移动事件绑定到该矩形,并不时在TextBox中显示鼠标的坐标,以便您可以清楚地看到鼠标移动事件的效果
鼠标移动事件处理程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfApp1
{
///
/// Window1.xaml 的交互逻辑
///
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
//鼠标移动事件处理程序
private void Event_MouseMove(object sender, MouseEventArgs e)
{
//GetPosition返回相对指定元素的鼠标位置
Point pt = e.GetPosition(this);
//移动鼠标的时候,将鼠标的坐标显示出来
this.lblInfo.Text = ("You are at ("+pt.X+","+pt.Y+") in window coordinates");
}
}
}
运行效果:

鼠标单击事件
PreviewMouseLeftButtonDown

PreviewMouseRightButtonDown
MouseLeftButtonDown
MouseRightButtonDown
PreviewMouseLeftButtonUp
PreviewMouseRightButtonUp
MouseLeftButtonUp
MouseRightButtonUp
捕获鼠标
捕获鼠标后,鼠标将无法再单击容器的其他元素
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfApp1
{
///
/// Window1.xaml 的交互逻辑
///
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Event_MouseMove(object sender, MouseEventArgs e)
{
//GetPosition返回相对指定元素的鼠标位置
Point pt = e.GetPosition(this);
//移动鼠标的时候,将鼠标的坐标显示出来
this.lblInfo.Text = ("You are at ("+pt.X+","+pt.Y+") in window coordinates");
}
//单击事件处理程序
private void Event_Click(object sender, RoutedEventArgs e)
{
//Mouse.Capture()将鼠标输入捕获到指定元素
//当鼠标被捕获以后,鼠标便无法再点击窗口上其他元素
Mouse.Capture(this.rect);
this.cmdCapture.Content = "鼠标已经被捕获";
}
}
}如图所示,当捕获鼠标时,最大化和最小化窗口的单击将不起作用


鼠标拖动
鼠标拖动是指从一个元素按下鼠标左键,然后将其拖动到另一个元素,释放鼠标左键,然后将相应的内容移动到第二个元素。
TextBox控件默认支持鼠标拖动,请参见示例:
TextBox Hello
后端代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPF_CODE
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
//第一个lable,当鼠标按下时,启动拖动效果
private void lab_fir_MouseDown(object sender, MouseButtonEventArgs e)
{
//获取事件激发者,即第一个lable
Label lb = (Label)sender;
//DragDrop.DoDragDrop指启动拖放操作
//参数分别是启动拖动的对象,要操作的类型,拖动类型
DragDrop.DoDragDrop(lb, lb.Content, DragDropEffects.Move);
}
//第二个lable,当拖拽事件在它身上发生时
private void lab_sec_Drop(object sender, DragEventArgs e)
{
//从DragEventArgs里获取到拖拽的内容进行加载
((Label)sender).Content = e.Data.GetData(DataFormats.Text);
}
}
}

效果是:
文本框的内容可以拖动到第一和第二个标签
第一个标签的内容可以拖到文本框,第二个标签
第二个标签可以拖到文本框中
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/bofangqi/article-327300-1.html
还可以合伙用你老婆
所以他要显示他的力量
含盐
可中国不是苏联