asp.net
wpf中的鼠标事件详解 -凯发ag旗舰厅登录网址下载
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
杨航收集技术资料,分享给大家
总结
以上是凯发ag旗舰厅登录网址下载为你收集整理的wpf中的鼠标事件详解的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。
- 上一篇:
- 下一篇: