Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionusing System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace drag { public partial class Form1 : Form { private bool _dragging = false; private Point _curLoc = new Point(-1, -1); TextBox []t=new TextBox[100]; Button bt = new Button(); GraphicsPath line; public Form1() { InitializeComponent(); line = new GraphicsPath(); bt.Location = new Point(200, 200); bt.Text = "click"; bt.Click += new EventHandler(bt_Click); this.Controls.Add(bt); } void creationLabel(int nb,String []name) { for (int i = 0; i < nb; i++) { TextBox l = new TextBox(); t[i] = l; l.Text = name[i]; t[i].BackColor = Color.Black; t[i].MouseDown += new MouseEventHandler(l_MouseDown); t[i].MouseMove += new MouseEventHandler(l_MouseMove); t[i].MouseUp += new MouseEventHandler(l_MouseUp); t[i].MouseClick += new MouseEventHandler(l_MouseClick); this.Controls.Add(l); } } void bt_Click(object sender, EventArgs e) { String []names= new String[10]; names[0] = "I"; names[1] = "eat"; names[2] = "apple"; creationLabel(3,names); } private void l_MouseDown(object sender, MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Left) { _dragging = true; _curLoc = new Point(-e.X, -e.Y); ((TextBox)sender).BackColor = Color.Black; ((TextBox)sender).BringToFront(); } } private void l_MouseClick(object sender, MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Left) { richTextBox1.Text = "[I] --> X: "+t[0].Location.X + " Y: " + t[0].Location.Y+" [eat] --> X"+t[1].Location.X+" Y: "+t[1].Location.Y; line.AddLine(new Point(t[0].Location.X, t[0].Location.Y), new Point(t[1].Location.X, t[1].Location.Y)); } } private void l_MouseMove(object sender, MouseEventArgs e) { if (_dragging) { Point mousePos = this.PointToClient(Control.MousePosition); mousePos.Offset(_curLoc.X, _curLoc.Y); ((TextBox)sender).Location = mousePos; } } private void l_MouseUp(object sender, MouseEventArgs e) { _dragging = false; ((TextBox)sender).BackColor = Color.White; } private void button2_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.DrawPath(Pens.Black, line); if (_dragging) { line.AddLine(new Point(407, 80), new Point(331, 80)); } { line.AddLine(new Point(407, 80), new Point(410, 90)); } } } }
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.DrawPath(Pens.Black, line); if (_dragging) { line.AddLine(new Point(407, 80), new Point(331, 80)); } { line.AddLine(new Point(407, 80), new Point(410, 90)); }
class maLigne { /// <summary> /// Classe gérant les coordonnees pour dessiner une ligne /// </summary> public maLigne() { Origine = new Point(-1,-1);//valeurs par défaut Fin = new Point(-1,-1); } public Point Origine { get; set; } public Point Fin { get; set; } /// <summary> /// Retourne si la ligne est prête à être dessinée /// </summary> public bool Pret { get { return !BesoinOrigine && (Fin.X != -1) && (Fin.Y != -1); } } /// <summary> /// Retourne si la ligne a besoin de definir son origine /// </summary> public bool BesoinOrigine { get { return (Origine.X -1) && (Origine.Y -1); } } /// <summary> /// Décalle la fin de la ligne précédente vers l'origine de la suivante /// </summary> public void Decalle() { Origine = Fin; Fin = new Point(-1,-1); } }
maLigne ligne = new maLigne();
private void l_MouseUp(object sender, MouseEventArgs e) { timer1.Enabled = false; _dragging = false; TextBox maTextBox = (TextBox)sender; maTextBox.BackColor = Color.White; if (ligne.BesoinOrigine) ligne.Origine = new Point(maTextBox.Left, maTextBox.Top);//le point d'origine n'est pas défini, alors on le crée else//le point d'origine est défini, alors je trace ma ligne { ligne.Fin = new Point(maTextBox.Left, maTextBox.Top); this.Refresh();//je force le formulaire à se redessiner, comme la ligne est prête elle sera ajoutée ligne.Decalle();//je décale pour la ligne suivante this.Refresh();//la ligne n'apparait pas mais je refait refresh pour qu'elle s'affcihe } }
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.DrawPath(Pens.Black, line); if (ligne.Pret) line.AddLine(ligne.Origine, ligne.Fin);//si la ligne est prête je l'ajoute }