wpf自定义控件(WPF-7.鼠标输入事件)

发布日期:2024-12-22 05:17:32     作者:半生轻浮     手机:https://m.xinb2b.cn/sport/glj453774.html     违规举报

摘要

鼠标输入也是一种常见的WPF事件类型,主要通过鼠标的操作来触发事件。 常见的鼠标事件有MouseEnter和MouseLeave,分别是在鼠标移动到组件上和离开组件时触发的,这两个事件都是直接事件,尽在某个元素上触发,不会传播到别的元素上。 除了这两种比较简单的直接时间,也包括一些冒泡路由事件和隧道路由事件,比如:PreviewMouseMove、MouseMove等

正文

鼠标单击

鼠标单击 鼠标单击分为鼠标左键和鼠标右键的单击,常见的鼠标单击事件会触发以下事件: PreviewMouseLeftButtonDown

PreviewMouseRightButtonDown

MouseLeftButtonDown

MouseRightButtonDown

PreviewMouseLeftButtonUp

PreviewMouseRightButtonUp

MouseLeftButtonUp

MouseRightButtonUp

Preview 一般是隧道

捕获鼠标

如果我们希望只能某个组件独占鼠标,那么我们利用鼠标捕获,让该组件捕获鼠标,从而鼠标点击其他组件时无法进入到响应函数中。

<Window x:Class="_7.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:_7" mc:Ignorable="d" Title="MainWindow" Height="350" Width="600" MouseDown="Window_MouseDown" MouseMove="Window_MouseMove" > <StackPanel> <Button x:Name="btnCapture" Content="捕获" Click="btnCapture_Click" Margin="20"></Button> <Label Name="lblPostion" Margin="20" Background="AntiqueWhite"></Label> <Rectangle Stroke="Aqua" Width="100" Height="100" Margin="20" x:Name="rect"> </Rectangle> </StackPanel></Window>

private void Window_MouseDown(object sender, MouseButtonEventArgs e){ lblPostion.Content = e.GetPosition(rect);}private void Window_MouseMove(object sender, MouseEventArgs e){ lblPostion.Content = e.GetPosition(rect);}private void btnCapture_Click(object sender, RoutedEventArgs e){ Mouse.Capture(this.rect);}

拖拽

鼠标拖放就是将窗口内某个元素拖离原来位置并放置到窗口的其他位置上,一般拖放分为三个步骤:

鼠标单击选择要拖放的元素;鼠标左键按住不放移动元素使其位置发生变化;鼠标松开左键将元素放置在某个位置。 AllowDrop="True"

<Grid> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Label Content="Hello world" Name="lbl1" Grid.Row="0" AllowDrop="True" BorderThickness="1" BorderBrush="Blue" MouseDown="lbl1_MouseDown"></Label> <Label Content="" Name="lbl2" Grid.Row="1" Foreground="Red" BorderThickness="1" BorderBrush="Red" Drop="lbl2_Drop" AllowDrop="True"></Label></Grid>

private void lbl1_MouseDown(object sender, MouseButtonEventArgs e){ Label lbl=(Label)sender; DragDrop.DoDragDrop(lbl,lbl.Content,DragDropEffects.Copy);}private void lbl2_Drop(object sender, DragEventArgs e){ lbl2.Content = e.Data.GetData(DataFormats.Text);}

做一个拖拽的例子

MouseMove

MouseLeftButtonUp

MouseLeftButtonDown

当Down,捕获鼠标

当Up,释放鼠标

当Move,修改Top,Left,

.SetValue(Canvas.LeftProperty, point.X - mouseOffset.X);

.SetValue(Canvas.TopProperty, point.Y - mouseOffset.Y);

<Canvas x:Name="cav"> <Rectangle x:Name="rect1" Fill="LightCoral" Width="100" Height="100"></Rectangle> <Rectangle x:Name="rect2" Fill="LightBlue" Width="100" Height="100" Canvas.Right="0" Canvas.Top="0" ></Rectangle> <Rectangle x:Name="rect3" Fill="LightGray" Width="100" Height="100" Canvas.Right="0" Canvas.Bottom="0"></Rectangle> <Rectangle x:Name="rect4" Fill="LightGreen" Width="100" Height="100" Canvas.Left="0" Canvas.Bottom="0"></Rectangle> <Label Content="AAA" ></Label></Canvas>

写一个简单拖拽类

internal class DragClass{ bool isDrag = false; Point mouseOffset; public Canvas cav { get; set; } public System.Windows.UIElement ui { get; set; } public DragClass(Canvas cav, System.Windows.UIElement ui) { this.cav = cav; this.ui = ui; this.ui.MouseLeftButtonDown = Ui_MouseLeftButtonDown; this.ui.MouseLeftButtonUp = Ui_MouseLeftButtonUp; this.ui.MouseMove = Ui_MouseMove; } private void Ui_MouseMove(object sender, MouseEventArgs e) { if (isDrag) { Point point = e.GetPosition(this.cav); ((System.Windows.UIElement)sender).SetValue(Canvas.TopProperty, point.Y - mouseOffset.Y); ((System.Windows.UIElement)sender).SetValue(Canvas.LeftProperty, point.X - mouseOffset.X); } } private void Ui_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (isDrag) { ((System.Windows.UIElement)sender).ReleaseMouseCapture(); isDrag = false; } } private void Ui_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { isDrag = true; mouseOffset = e.GetPosition((System.Windows.IInputElement)sender); ((System.Windows.UIElement)sender).CaptureMouse(); }}

DragClass drap1 = new DragClass(this.cav, this.rect1);DragClass drap2 = new DragClass(this.cav, this.rect2);DragClass drap3 = new DragClass(this.cav, this.rect3);DragClass drap4 = new DragClass(this.cav, this.rect4);


 
 
本文地址:https://xinb2b.cn/sport/glj453774.html,转载请注明出处。

推荐图文
推荐运动知识
网站首页  |  关于我们  |  联系方式  |  使用协议  |  版权隐私  |  网站地图  |  违规举报  |  蜀ICP备18010318号-4  |  百度地图  | 
Processed in 0.039 second(s), 1 queries, Memory 0.61 M