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;
}
}