wpf自定义控件(WPF-7.鼠标输入事件)
wpf自定义控件(WPF-7.鼠标输入事件)
2024-09-30 05:15:45  作者:半生轻浮  网址: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);


  • 新手玩海岛奇兵先升什么(给刚玩海岛奇兵玩家的升级建议)
  • 2024-10-01给刚玩海岛奇兵玩家的升级建议《海岛奇兵》比较耗时间,越到后面升级越慢越难故而,有一个好的升级方案,有利于自己事半功倍,少走弯路那么,我们该怎么去升级呢?玩游戏,首要的目标,就是要攒够充足的资源,资源是一切一切的基础因此,我建议你。
  • 路桥施工设计手册(路桥工程精益建造指导手册)
  • 2024-10-01路桥工程精益建造指导手册本《手册》是一本指导性手册,具有可实施性、可操作性、可复制性、可推广性,手册涉及的管理方法、管理模型、管理工具以文字说明、流程、表格、简图、照片等形式呈现,文字简洁、通俗易懂、使用方便本篇选自《路桥工。
  • 大s幸福三重奏看到蟑螂(大S看北方食物就想吐)
  • 2024-10-01大S看北方食物就想吐特别提示:原创文章,禁止抄袭,抄袭必究,全网维权!副标题:大S汪小菲矛盾早有端倪,嫌弃北京回台湾,汪小菲十年两地往返700趟导语:大S因汪小菲吐槽她家乡而突然宣布离婚,这一波操作有可能只是为了堵住台媒。
  • 登录密码怎么重置(还可以这样重置登录密码)
  • 2024-10-01还可以这样重置登录密码若使用本地账户,不小心忘记了登录密码,但当前仍可以通过Pin码或是WindowsHello登录到系统,此时想要重置登录密码,怎么办呢?您可以参考以下操作哦:1、首先,在登录界面,选择Pin码,或是通过。
  • 为什么有的女生不喜欢染头发?有这几个特点的女生打车去染浅发
  • 2024-10-01为什么有的女生不喜欢染头发?有这几个特点的女生打车去染浅发大家好!每到夏天,浅发色就会突然流行起来,Rose女士的金发戳中了多少人的心巴!不得不说,浅发真的太为普通人增色了那什么样的人适合浅发,我一一总结了,有下面几个特点的辣妹,马上打车去找Tony漂发!发。
  • 我市发展安全应急产业的思考(保障新产业新技术)
  • 2024-10-01保障新产业新技术面对新产业、新技术、新工艺、新业态发展过程中产生的新问题、新挑战,如何在保安全的底线下,促发展?近年来,松江区通过织密一张网,创新监管模式,先行先试城市安全理念,打出了一套维护安全发展的“组合拳”,为。
  • 鹿晗八月三号几点直播(今晚八点有惊喜)
  • 2024-10-01今晚八点有惊喜电竞玩家兼老板鹿晗boss突然上线!Lstars电子竞技俱乐部官博宣布今晚八点强势集结Lstars,鹿boss、A总助阵,将与@Lstars_Yc以及@Lstars_Wang共同带来精彩的直播同时官方。
  • 英雄联盟最早版本的乌鸦(英雄联盟美测服头像框更新)
  • 2024-10-01英雄联盟美测服头像框更新这一期也是最近的资讯汇总,主要内容包括美测服1-500级的头像框全部进行更新乌鸦即将迎来中型重做,之前还有奥拉夫和岩雀中型重做的消息最后是赏金之旅通行证已加入了胖嗡嗡龙的兑换,还有个鸭子表情将在4月1。
  • 迅雷下载速度0怎么回事(解决方法说明)
  • 2024-10-01解决方法说明开启迅雷找到设置,点击“设置中心”,找到“下载设置”找到“全局下载速度”的数值设置到最大找到“下载速度”,勾选“开启镜像服务器加速”和“开启迅雷P2P加速”,退出迅雷再重启即可提高速度。
  • 粤剧名家经典唱段联唱(粤剧名家相遇大岭村)
  • 2024-10-01粤剧名家相遇大岭村粤剧展演现场,右一为吴非凡广州日报讯(全媒体记者肖桂来摄影报道)1月18日下午,在中国历史文化名村——广州番禺大岭村,悠扬粤韵萦绕在古园古巷之间,不少村民循着锣鼓声赶来,一场热闹的粤剧展演正在举行粤剧。