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.pswp.cn/news/388747.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388747.shtml
英文地址,請注明出處:http://en.pswp.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;以及在某…