Mouse Input and the WPF - Mouse State
(Page 4 of 4 )
As well as defining events, the Mouse class defines some static properties and methods that you can use to discover information about the mouse or modify its state.
TheGetPosition method lets you discover the position of the mouse. As Example 4-9 shows, you must pass in a user interface element. It will return the mouse position relative to the specified element, taking into account any transformations that may be in effect.
Example 4-9. Retrieving the mouse position
Point positionRelativeToEllipse = Mouse.GetPosition(myEllipse);
TheCapturemethod allows an element to capture the mouse. Mouse capture means that all mouse input events are sent to the capturing element, even if the mouse is currently outside of that element.* Example 4-10 captures the mouse to an ellipse when a mouse button is pressed, enabling it to track the movement of the mouse even if it moves outside of the ellipse. In fact, it will continue to receiveMouseMove events even if the mouse moves outside of the window. This is useful for drag operations, as the user will expect an item being dragged to follow the mouse for as long as the mouse button is pressed. The capture is released by passing null to theCapturemethod.
Example 4-10. Mouse capture
public Window1() {
InitializeComponent();
myEllipse.MouseDown += myEllipse_MouseDown;
myEllipse.MouseMove += myEllipse_MouseMove;
myEllipse.MouseUp += myEllipse_MouseUp; }
void myEllipse_MouseDown(object sender, MouseButtonEventArgs e) {
Mouse.Capture(myEllipse);
}
void myEllipse_MouseUp(object sender, MouseButtonEventArgs e) {
Mouse.Capture(null);
}
void myEllipse_MouseMove(object sender, MouseEventArgs e) {
Debug.WriteLine(Mouse.GetPosition(myEllipse));
}
TheMouseclass provides aCaptured property that returns the element that has currently captured the mouse; it returns null if the mouse is not captured. You can also discover which element in your application, if any, the mouse is currently over, by using the staticMouse.DirectlyOverproperty.
Mouse provides five properties that reflect the current button state. Each returns aMouseButtonStateenumeration value, which can be eitherPressedorReleased. Three of these properties—LeftButton,MiddleButton, andRightButton—are self-explanatory. The other two—XButton1 andXButton2 —are perhaps less obvious. These are for the extra buttons provided on some mice, typically found on the side. The locations of these so-called extended buttons are not wholly consistent—one of the authors’ mice has these two buttons on the lefthand side, and another has one on each side. This explains the somewhat abstract property names.
Mousealso provides anOverrideCursorproperty that lets you set a mouse cursor to be shown throughout your whole application, as shown in Example 4-11. This overrides any element-specific mouse cursor settings. You could use this to temporarily show an hourglass cursor when performing some slow work.
Example 4-11. Temporary mouse cursor override
private void StartSlowWork() {
Mouse.OverrideCursor = Cursors.AppStarting;
...
}
private void SlowWorkCompleted() {
Mouse.OverrideCursor = null;
}
Please check back next week for the continuation of this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from Programming WPF, Second Edition, written by Chris Sells and Ian Griffiths (O'Reilly, 2007; ISBN: 0596510373). Check it out today at your favorite bookstore. Buy this book now.
|
|