Detailed mouse events in WPF __WPF

Source: Internet
Author: User
Tags pow
detailed mouse events in WPFBoth UIElement and ContentElement define 10 events that begin with mouse, 8 events that begin with Previewmouse, Mousemove,previewmousemove,mouseenter, The MouseLeave event processor types are MouseEventHandler types. These events have a corresponding MouseEventArgs object. (no pre's enter and leave).

MouseMove occurs many times when the mouse crosses an element, but MouseEnter and mouseleave occur only once, respectively, when the mouse enters the element area and leaves the element area.

UIElement and ContentElement define two read-only properties, IsMouseOver: If the mouse is on an element, this property is true if the mouse is not on this element and not on any of its child controls. Then Ismousedirectover is also true.

When handling MouseMove, Mouseenter,mouseleave events We can also get the mouse button which is being pressed: Leftbutton,middlebutton,rightbutton, As well as two extension keys XButton1 and XButton2. (These five are all attributes of MouseEventArgs), the value of his gate is one of mousebuttonstate enumerations, only two states, pressed and released.

For the MouseMove event, you may also want to get the current mouse position and use the MouseEventArgs Getpostion method. The MouseEventArgs Changebutton property of the event lets you know which mouse button is being pressed.

MouseWheel and Previewwheel events, mainly dealing with mouse wheel events, MouseWheelEventArgs has a property delta property that records the scale of the mouse wheel, and now every time the mouse rolls the scale is 120, When you turn to the user, it is 120. You can use Systemprarameters. Ismousewheelpresent know whether the mouse has a scroll wheel.

The static method of the mouse class can also get the location and state of the mouse, as well as a static method to add or remove the mouse event handler. The Mousedevice class has some instance methods that can be used to get the mouse position and button state.

The mouse is displayed on the screen using a small bitmap, called the mouse cursor, in WPF where the cursor is the object of the cursor type, and the cursor object can be set to the cursor property of the FrameworkElement. You can associate the specified mouse icon with an element, and of course you can overload the Onquerycursor method or add a processor to the Querycursor event, and the mouse movement triggers the event, QueryCursorEventArgs accompanying this event, He has a cursor attribute that we can use.

Capture Mouse: When we mouse down once the mouse moved out of the area of the element, we can not receive mouse events, but sometimes this is not the result we want, all need to enter this element in the mouse to get the mouse, so that the mouse even left the area will get the mouse message. UIElement and ContentElement Both define the Caputermouse method, and the mouse class also provides a Caputer static method that allows us to capture the mouse and return true once the capture succeeds, and should release the capture after our event processing is complete. Use Releasemousecaputer.

If you use mouse capture, you must install the processor of the Lostmousecaputer event and do some necessary finishing work. Here we write a small program to use these events: 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
}
Create a Ellipse object and add it to the 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);
Get mouse
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
}
Get clicked Events ready for the future
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;

}
Move Ellipse
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)
{//If the currently selected element only changes the size of the current element
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)
{//If none of the selected elements change size with all elements
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;
}
}
}
}
}

}

The above program is mainly to draw the movement of graphics and the size of the shape of the changes. These are just changes in 2D graphics.

This article from Wang_top's blog, the original address: http://www.cnblogs.com/wangtaiping/archive/2011/04/04/2004971.html

Yang Ai collect technical information and share it with us

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.