WPF, force capture mouse events, mouse out of control can still perform forced capture mouse events

Source: Internet
Author: User

Original: WPF, Force capture mouse events, mouse out of control can still perform forced capture mouse events

In WPF, mouse events for a control are only triggered when the mouse position is on one of the controls.
For example, there are two controls that register the MouseDown and MouseUp events, press the mouse over control 1, do not release, move to control 2, and then release.
In this process, control 1 only triggers the MouseDown event, while control 2 only triggers the MouseUp event, and the mouse does not receive the corresponding mouse event on the control.
Similarly, if a control registers the MouseMove event, the control will not receive the MouseMove event when the mouse moves outside the control.
But in many cases we need to be able to receive mouse events after the mouse is moved outside the control. For example, holding down the mouse drag, when the mouse is outside the control, the drag operation can still continue.
These conditions require the control to force the mouse to be captured, which involves the use of the Uielment capturemouse function and the Releasemousecapture function.
The role of the CaptureMouse function is to attempt to force capture of the mouse to the control, and the Releasemousecapture function is to release the capture if the control has mouse capture.

Here's an example, for example, we need to animate a rectangle by dragging a mouse over a picture, and the steps are divided into:
1. Click the left mouse button to determine a vertex of the rectangle p;
2. Hold down the left mouse button and drag the mouse, the position of the mouse as the vertex p belongs to the diagonal of another vertex, draw a rectangle;
3. Release the left mouse button to complete the rectangle drawing.
However, when the mouse moves to the edge of the picture, the picture can not receive the mouse MouseMove and MouseUp events, the drawing process will not be completed properly.

To solve this problem, we need to add the following code to the corresponding function of the MouseLeftButtonDown event
((UIElement) e.source). CaptureMouse ();
In the corresponding function of the MouseUp event, add
((UIElement) e.source). Releasemousecapture ();
This way, we can continue to perform captured mouse events even if the mouse is outside the picture.

In order for the rectangle not to draw outside the picture, we can add a limit to the rectangle's border position, and the two vertices of the rectangle must be limited:
Point P = e.getposition (This.imagecontrol);
p.x = Math.max (0, p.x);
P.Y = Math.max (0, P.Y);
p.x = Math.min (This.imageControl.Width, p.x);
P.Y = Math.min (This.imageControl.Height, P.Y);
After you have used the CaptureMouse function to force the mouse to capture the control, be sure not to forget to use the Releasemousecapture function to release the capture after the operation is complete.

Otherwise, mouse actions on other controls will still trigger mouse events for the control that captures the mouse, rather than the mouse event response function of the control that originally clicked on the mouse.

WPF, force capture mouse events, mouse out of control can still perform forced capture mouse events

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.