1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
public partial class selectionrect : PictureBox
{
public selectionrect()
{
InitializeComponent();
//this.SetStyle(ControlStyles.UserPaint |
// ControlStyles.AllPaintingInWmPaint |
// ControlStyles.OptimizedDoubleBuffer, true);
}
public String position = null;
private Point _poitdepart=Point.Empty;
private Point _poitarrive=Point.Empty;
private Rectangle rect=Rectangle.Empty;
Point inter = Point.Empty;
private bool selct = false;
private Pen pen = new Pen(Color.Yellow);
protected override void OnMouseDown(MouseEventArgs e)
{
//base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
selct = true;
_poitdepart = new Point(e.X, e.Y);
_poitarrive=Point.Empty;
rect=Rectangle.Empty;
inter.X = _poitdepart.X;
inter.Y = _poitdepart.Y;
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
//base.OnMouseMove(e);
if (selct)
{
_poitarrive = new Point(e.X, e.Y);
position = _poitarrive.ToString();
// _poitdepart;
//voir si le poit d'arrivé est positif sinon inverser
//pour que le rectangle soit bien dessiné ;)
if (_poitarrive.X<=inter.X || _poitarrive.Y <=inter.Y)
{
//permuter pour que le rect n'as pas de paramètres négatifs ;)
_poitdepart.X = e.X;
_poitdepart.Y = e.Y;
_poitarrive.X = inter.X;
_poitarrive.Y = inter.Y;
}
//maintenat calcul du rectangle ;)
rect = new Rectangle(_poitdepart.X, _poitdepart.Y, _poitarrive.X - _poitdepart.X, _poitarrive.Y - _poitdepart.Y);
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
//base.OnMouseUp(e);
//on finit le dessin par désactiver le dessin des rectangles ;)
selct = false;
//MessageBox.Show(_poitdepart.ToString()+"\n"+_poitarrive.ToString()+"\n"+rect.ToString());
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
//if (selct)
{
//on dessine un rectangle avec les coordonnées du rectangel de selection ;)
Graphics g = pe.Graphics;
g.DrawRectangle(pen, rect);
g.DrawString("position" + position, new Font(FontFamily.GenericSerif, 12), Brushes.Red, new PointF(0, 0));
g.DrawString("arrive" + _poitarrive.ToString(), new Font(FontFamily.GenericSerif, 12), Brushes.Red, new PointF(0, 20));
g.DrawString("depart" + _poitdepart.ToString(), new Font(FontFamily.GenericSerif, 12), Brushes.Red, new PointF(0, 40));
g.DrawString("inter" + inter.ToString(), new Font(FontFamily.GenericSerif, 12), Brushes.Red, new PointF(0, 60));
g.DrawString("rectangle" + rect.ToString(), new Font(FontFamily.GenericSerif, 12), Brushes.Red, new PointF(0, 80));
this.Invalidate();
}
}
} |
Partager