WPF中的鼠标事件详解

WPF中的鼠标事件详解

Uielement和ContentElement都定义了十个以Mouse开头的事件,8个以PreviewMouse开头的事件,MouseMove,PreviewMouseMove,MouseEnter,Mouseleave的事件处理器类型都是MouseEventHandler类型。这些事件都具备对应得MouseEventargs对象。(没有pre的enter和leave)。

  当鼠标穿过一个Element时,mousemove会发生很多次,但是mouseenter和mouseleave只会发生一次,分别在鼠标进入element区域以及离开element区域是发生。

  UIElement和ContentElement定义了两个只读属性,ISmouseOver:如果鼠标在Element上,这个属性为true,如果鼠标不仅在这个Element上,且不在其任何子控件上,那么IsMouseDirectOver也为true。

  当处理MouseMove ,MouseEnter,Mouseleave事件时我们还可以获取正在按下的鼠标按键式哪一个:Leftbutton,middlebutton,RightButton,以及两个扩充按键XButton1和XButton2。(这五个都是MouseEventargs的属性),他门的值是MouseButtonState枚举的一个,只有两种状态,Pressed和Released.

  对于MouseMove事件,你还有可能想要获取当前鼠标的位置,可以使用MouseEventargs的GetPostion方法。通过事件的MouseEventargs的ChangeButton属性可以得知是哪个鼠标按钮被按下。

  MouseWheel和PreviewWheel事件,主要是处理鼠标滚轮事件,MousewheelEventargs有一个属性Delta属性,它记录鼠标滚轮的刻度,现在的鼠标每滚一下刻度是120,转向用户的时候就是-120.可以利用systemprarameters。IsMouseWheelPresent得知鼠标是否有滚轮。

  Mouse类的静态方法也可以获取鼠标的位置和状态,也具有静态方法可以添加或删除鼠标事件处理器。MouseDevice类有一些实例方法可以用来获取鼠标位置和按钮状态。

  鼠标在屏幕上使用一个小小的位图来显示的,此图标称作鼠标光标,在Wpf中,光标就是Cursor类型的对象,可以将Cursor对象设置到FrameworkElement的Cursor属性,就可以将指定的鼠标图标关联到某个element,当然你也可以重载onQueryCursor方法或者给QueryCursor事件添加处理器,鼠标移动就会触发这个事件,QueryCursorEventargs伴随这个事件,他有一个Cursor属性,可以供我们使用。

  捕获鼠标:在我们鼠标按下后一旦鼠标移出element的区域,就收不到鼠标事件了,但是有时候这个并不是我们想要的结果,所有需要在鼠标进入这个了element的时候获取鼠标,这样鼠标就算离开了这个区域也会获取到鼠标消息。UIelement和contentelement都定义了CaputerMouse方法,Mouse类也提供了Caputer静态方法,让我们可以捕获鼠标,一旦捕获成功就会返回true,在我们的事件处理完成后应该释放这个捕获,使用ReleaseMouseCaputer.

  如果使用了鼠标捕获,就必须安装LostmouseCaputer事件的处理器,做一些必要的收尾工作。下面我们写一个小程序来使用这些事件:

using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;


namespace WPFDemo
{
class DrawCircles : Window
{
Canvas canvas;
Boolean IsDrawing;
Ellipse elips;
Point ptcenter;
Boolean IsDragging;
Boolean IsChanging;
FrameworkElement elDragging;
Ellipse ChangeElips;
Point ptmousestart, ptElementStart;

[STAThread]
static void Main()
{
Application app = new Application();
app.Run(new DrawCircles());
}

public DrawCircles()
{
Title = "Draw Circles";
Content = canvas = new Canvas();
Line line = new Line();
line.Width = 1;
line.X1 = 0;
line.X2 = canvas.ActualWidth/2;
line.Y1 = canvas.ActualHeight / 2;
line.Y2 = 0;
canvas.Children.Add(line);
line.Stroke = SystemColors.WindowTextBrush;
}
protected overridevoid OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
if (IsDragging)
{
return;
}
//创建一个Ellipse对象,并且把它加入到Canvas中
ptcenter = e.GetPosition(canvas);
elips = new Ellipse();
elips.Stroke = SystemColors.WindowTextBrush;
//elips.StrokeThickness = 0.1;
elips.Width = 0;
elips.Height = 0;
elips.MouseEnter += new MouseEventHandler(elips_MouseEnter);
elips.MouseLeave += new MouseEventHandler(elips_MouseLeave);
canvas.Children.Add(elips);
Canvas.SetLeft(elips, ptcenter.X);
Canvas.SetTop(elips, ptcenter.Y);
//获取鼠标
CaptureMouse();
IsDrawing = true;

}

void elips_MouseLeave(object sender, MouseEventArgs e)
{
ChangeElips = sender as Ellipse;
ChangeElips.Stroke = SystemColors.WindowTextBrush;
ChangeElips = null;
if ( IsChanging)
{
IsChanging = false;
}
//throw new NotImplementedException();
}

void elips_MouseEnter(object sender, MouseEventArgs e)
{
ChangeElips = sender as Ellipse;
ChangeElips.Stroke = SystemColors.WindowFrameBrush;
IsChanging = true;
//throw new NotImplementedException();
}
protected overridevoid OnMouseRightButtonDown(MouseButtonEventArgs e)
{
base.OnMouseRightButtonDown(e);
if (IsDrawing)
{
return;
}
//得到点击的事件 为未来做准备
ptmousestart = e.GetPosition(canvas);
elDragging = canvas.InputHitTest(ptmousestart) as FrameworkElement;
if (elDragging != null)
{
ptElementStart = new Point(Canvas.GetLeft(elDragging),
Canvas.GetTop(elDragging));
IsDragging = true;
}

}

protected overridevoid OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
if (e.ChangedButton == MouseButton.Middle)
{
Shape shape = canvas.InputHitTest(e.GetPosition(canvas)) as Shape;
if (shape != null)
{
shape.Fill = (shape.Fill == Brushes.Red ?
Brushes.Transparent : Brushes.Red);

}
}
}

protected overridevoid OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
Point ptmouse = e.GetPosition(canvas);

if (IsDrawing)
{
double draddius = Math.Sqrt(Math.Pow(ptcenter.X - ptmouse.X, 2) +
Math.Pow(ptcenter.Y - ptmouse.Y, 2));
Canvas.SetLeft(elips, ptcenter.X - draddius);
Canvas.SetTop(elips, ptcenter.Y - draddius);
elips.Width = 2 * draddius;
elips.Height = 2 * draddius;

}
//移动椭圆
else if (IsDragging)
{
Canvas.SetLeft(elDragging, ptElementStart.X + ptmouse.X - ptmousestart.X);
Canvas.SetTop(elDragging, ptElementStart.Y + ptmouse.Y - ptmousestart.Y);
}
}

protected overridevoid OnMouseUp(MouseButtonEventArgs e)
{
base.OnMouseUp(e);
if (IsDrawing&&e.ChangedButton == MouseButton.Left)
{
elips.Stroke = Brushes.Blue;
elips.StrokeThickness =elips.Width/8;
elips.Fill = Brushes.Red;
IsDrawing = false;
ReleaseMouseCapture();
}
else if (IsDragging&&e.ChangedButton == MouseButton.Right)
{
IsDragging = false;
}
}
protected overridevoid OnTextInput(TextCompositionEventArgs e)
{
base.OnTextInput(e);
if (e.Text.IndexOf('\x18')!=-1)
{
if (IsDrawing)
{
ReleaseMouseCapture();
}
else if (IsDragging)
{
Canvas.SetLeft(elDragging, ptElementStart.X);
Canvas.SetTop(elDragging, ptElementStart.Y);
IsDragging = false;
}
}
}
protected overridevoid OnLostMouseCapture(MouseEventArgs e)
{
base.OnLostMouseCapture(e);
if (IsDrawing)
{
canvas.Children.Remove(elips);
IsDrawing = false;
}
}

protected overridevoid OnMouseWheel(MouseWheelEventArgs e)
{
base.OnMouseWheel(e);
if (IsChanging &&ChangeElips!=null)
{//如果当前有选定的元素就只将当前元素的大小变化
Point pt = new Point(Canvas.GetLeft(ChangeElips) + ChangeElips.Width / 2, Canvas.GetTop(ChangeElips) + ChangeElips.Height / 2);
double draddius = (e.Delta*1.0 / 1200 + 1) * ChangeElips.Height;

Canvas.SetLeft(ChangeElips, pt.X - draddius/2);
Canvas.SetTop(ChangeElips, pt.Y - draddius/2);
ChangeElips.Height = draddius;
ChangeElips.Width = draddius;
}
else if (ChangeElips==null)
{//如果没有选定的元素就所有的元素一起变化大小
double canvax = canvas.ActualWidth;
double canvay = canvas.ActualHeight;
foreach (UIElement elisp in canvas.Children)
{
Ellipse els = elisp as Ellipse;
if (els!=null)
{
double draddius = (e.Delta * 1.0 / 1200 + 1) * els.Height;
double x1 = Canvas.GetLeft(els);
double y1 = Canvas.GetTop(els);
double draddiusx = (e.Delta * 1.0 / 1200) * (x1 - canvax / 2) + x1;
double
draddiusY = (e.Delta * 1.0 / 1200) * (y1 - canvay / 2) + y1;
Canvas.SetLeft(els,draddiusx);
Canvas.SetTop(els,draddiusY);
els.Height = draddius;
els.Width = draddius;
els.StrokeThickness = els.Width /8;
}
}
}
}
}

}

  上面的程序中主要是绘制 移动图形以及图形自身大小的变化。这些只是2D图形的变化。

本文来自Wang_top的博客,原文地址:http://www.cnblogs.com/wangtaiping/archive/2011/04/04/2004971.html

杨航收集技术资料,分享给大家



本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388747.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数据统计 测试方法_统计测试:了解如何为数据选择最佳测试!

数据统计 测试方法This post is not meant for seasoned statisticians. This is geared towards data scientists and machine learning (ML) learners & practitioners, who like me, do not come from a statistical background.Ť他的职位是不是意味着经验丰富的统计人…

前端介绍-35

前端介绍-35 # 前端## 一、什么是前端 前端即网站前台部分,运行在PC端,移动端等浏览器上展现给用户浏览的网页。随着互联网技术的发展,HTML5,CSS3,前端框架的应用,跨平台响应式网页设计能够适应各种屏幕…

spring的几个通知(前置、后置、环绕、异常、最终)

1、没有异常的 2、有异常的 1、被代理类接口Person.java 1 package com.xiaostudy;2 3 /**4 * desc 被代理类接口5 * 6 * author xiaostudy7 *8 */9 public interface Person { 10 11 public void add(); 12 public void update(); 13 public void delete();…

每个Power BI开发人员的Power Query提示

If someone asks you to define the Power Query, what should you say? If you’ve ever worked with Power BI, there is no chance that you haven’t used Power Query, even if you weren’t aware of it. Therefore, one could easily say that Power Query is the “he…

c# PDF 转换成图片

1.新建项目 2.新增一个新文件夹“lib”(主要是为了存放引用的dll) 3.将“gsdll32.dll 、PDFLibNet.dll 、PDFView.dll”3个dll添加到文件夹中 4.项目添加“PDFLibNet.dll 、PDFView.dll”2个类库的引用,并将gsdll32.dll 拷贝到项目生产根…

java finally在return_Java finally语句到底是在return之前还是之后执行?

点击上方“方志朋”,选择“置顶或者星标”你的关注意义重大!网上有很多人探讨Java中异常捕获机制try...catch...finally块中的finally语句是不是一定会被执行?很多人都说不是,当然他们的回答是正确的,经过我试验&#…

oracle 死锁

为什么80%的码农都做不了架构师?>>> ORA-01013: user requested cancel of current operation 转载于:https://my.oschina.net/8808/blog/2994537

面试题:二叉树的深度

题目描述:输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。 思路:递归 //递归 public class Solution {public int TreeDepth(Tre…

a/b测试_如何进行A / B测试?

a/b测试The idea of A/B testing is to present different content to different variants (user groups), gather their reactions and user behaviour and use the results to build product or marketing strategies in the future.A / B测试的想法是将不同的内容呈现给不同…

hibernate h2变mysql_struts2-hibernate-mysql开发案例 -解道Jdon

Hibernate专题struts2-hibernate-mysql开发案例与源码源码下载本案例展示使用Struts2,Hibernate和MySQL数据库开发一个个人音乐管理器Web应用程序。,可将您的音乐收藏添加到数据库中。功能有:显示一个添加记录的表单和所有的音乐收藏的列表。…

P5024 保卫王国

传送门 我现在还是不明白为什么NOIPd2t3会是一道动态dp…… 首先关于动态dp可以看这里 然后这里就是把把矩阵给改一改,改成这个形式\[\left[dp_{i-1,0},dp_{i-1,1}\right]\times \left[\begin{matrix}\infty&ldp_{i,1}\\ldp_{i,0}&ldp_{i,1}\end{matrix}\ri…

提取图像感兴趣区域_从图像中提取感兴趣区域

提取图像感兴趣区域Welcome to the second post in this series where we talk about extracting regions of interest (ROI) from images using OpenCV and Python.欢迎来到本系列的第二篇文章,我们讨论使用OpenCV和Python从图像中提取感兴趣区域(ROI)。 As a rec…

解决java compiler level does not match the version of the installed java project facet

ava compiler level does not match the version of the installed java project facet错误的解决 因工作的关系,Eclipse开发的Java项目拷来拷去,有时候会报一个很奇怪的错误。明明源码一模一样,为什么项目复制到另一台机器上,就会…

php模板如何使用,ThinkPHP如何使用模板

到目前为止,我们只是使用了控制器和模型,还没有接触视图,下面来给上面的应用添加视图模板。首先我们修改下 Action 的 index 操作方法,添加模板赋值和渲染模板操作。PHP代码classIndexActionextendsAction{publicfunctionindex(){…

理解Windows窗体和WPF中的跨线程调用

你曾开发过Windows窗体程序,可能会注意到有时事件处理程序将抛出InvalidOperationException异常,信息为“ 跨线程调用非法:在非创建控件的线程上访问该控件”。这种Windows窗体应用程序中 跨线程调用时的一个最为奇怪的行为就是,有…

什么是嵌入式系统

在我们的日常生活中,我们经常使用许多使用嵌入式系统技术设计的电气和电子电路和套件。计算机,手机,平板,笔记本电脑,数字电子系统以及其他电子和电子设备都是使用嵌入式系统设计的。 什么是嵌入式系统?将硬…

面向数据科学家的实用统计学_数据科学家必知的统计数据

面向数据科学家的实用统计学Beginners usually ignore most foundational statistical knowledge. To understand different models, and various techniques better, these concepts are essential. These work as baseline knowledge for various concepts involved in data …

字符串、指针、引用、数组基础

1.字符串:字符是由单引号所括住的单个字母、数字或符号。若将单引号改为双引号,该字符就会变成字符串。它们之间主要的差别是:双引号的字符串“A”会比单引号的字符串’A’在字符串的最后补上一个结束符’\0’(Null字符&#xff0…

suse安装php,SUSE下安装LAMP

安装Apache可以看到编译安装Apache出错,rpm包安装gcc (首先要安装GCC)makemake install修改apache端口cd /home/sxit/apache2vi conf/httpd.confListen 8000启动 apache/home/root/apache2/bin/apachectl start(stop restart)http://localhost:8000安装一下PHP开发…

自己动手写事件总线(EventBus)

2019独角兽企业重金招聘Python工程师标准>>> 本文由云社区发表 事件总线核心逻辑的实现。 <!--more--> EventBus的作用 Android中存在各种通信场景&#xff0c;如Activity之间的跳转&#xff0c;Activity与Fragment以及其他组件之间的交互&#xff0c;以及在某…