Realization of mouse box selection effect using C # code _c language

Source: Internet
Author: User

Implementation steps:

1. Implementation of the entire mouse box selected several events (down, move, up), when the mouse click to record the beginning of the mouse box selection, the mouse to lift the end of the operation.

2. The mouse coordinates obtained in the mouse-frame selection are computed by the 4-point coordinates of the rectangle selected by the box and 4-point coordinates in a clockwise direction.

3. This rectangle is drawn on the class using the Shape.path class implementation.

The code is as follows:

Copy Code code as follows:

Namespace Hostdemo {
public class Hostcanvas:canvas {
Public Hostcanvas () {
InitializeComponent ();
}

private void InitializeComponent () {
This. Loaded + OnLoad;
This. MouseDown + = OnMouseDown;
This. MouseMove + = OnMouseMove;
This. MouseUp + = OnMouseUp;
Locus = new Path ();
Locus. Fill = new SolidColorBrush (Color.FromArgb (1, 255, 255, 255));
Locus. Stroke = brushes.red;
Locus. StrokeThickness = 1;
Locus. Ismanipulationenabled = true;
}

void OnMouseUp (object sender, System.Windows.Input.MouseButtonEventArgs e) {
Ispath = false;
}

void OnMouseMove (object sender, System.Windows.Input.MouseEventArgs e) {
if (Ispath) {
Endpoint = E.getposition (this);
Locus. Data = Drawingrect (startpoint,endpoint);
}
}

void OnMouseDown (object sender, System.Windows.Input.MouseButtonEventArgs e) {
if (!this. Children.contains (locus)) this. Children.add (locus);
if (locus. Data!= null) locus. Data = null;
StartPoint = E.getposition (this);
Ispath = true;
}

void OnLoad (object sender, System.Windows.RoutedEventArgs e) {
This. Background = new SolidColorBrush (Color.FromArgb (35, 255, 255, 255));
}

Private PathGeometry Drawingrect (Point beginpoint, point Closepoint) {
PathGeometry result = new PathGeometry ();
PathFigure figure = new PathFigure ();
Figure. IsClosed = true;
Figure. StartPoint = Beginpoint;
PathSegmentCollection pathsegmentcollection = new PathSegmentCollection ();
PathFigureCollection pathfigurecollection = new PathFigureCollection ();
LineSegment m1 = new LineSegment ();
M1. Point = new Point (Closepoint). X, Beginpoint. Y);
LineSegment m2 = new LineSegment ();
M2. Point = Closepoint;
LineSegment m3 = new LineSegment ();
M3. Point = new Point (Beginpoint). X, Closepoint. Y);
Pathsegmentcollection.add (M1);
Pathsegmentcollection.add (m2);
Pathsegmentcollection.add (m3);
Figure. segments = PathSegmentCollection;
Pathfigurecollection.add (figure);
Result. Figures = PathFigureCollection;

return result ();
}

Private Path locus;
private bool Ispath = false;
Private point StartPoint;
Private point endpoint;
}
}


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.