Le réseau pour les développeurs >
Forums - Accueil
>
Visual C# Language
>
how to use drag and drop function?
how to use drag and drop function?
- me and my frens are doing a project on a knapsack based game.
so we need to use drag and drop function to do a particular task.
so can anybody help us in using an function of drag and drop,like simply creating and object and drag that object in the form using mouse.
Réponses
- Hi,
You can develop your own custom control for this property.
You need to be similar with OnPaint,MouseDown,MouseUp and MouseMove events.
Here is a sample that draws a line on the form and allows user to move it at runtime.
public class MyLine:Control { bool drawBold = false; protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Pen p = null; if (drawBold) { p= new Pen(this.ForeColor, this.Height); } else { p= new Pen(this.ForeColor, this.Height-5); } e.Graphics.DrawLine(p , 0, this.Height/2, this.Width, this.Height/2); } protected override void OnMouseHover(EventArgs e) { base.OnMouseHover(e); drawBold = true; this.Refresh(); } protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); drawBold = false; this.Refresh(); } //to move control Point p = new Point(); protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); p.X = e.X; p.Y = e.Y; } protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (e.Button == MouseButtons.Left) { this.Location = new Point(e.X - p.X + this.Location.X, e.Y - p.Y + this.Location.Y); } } }- Marqué comme réponseYichun_FengMSFT, Modérateurvendredi 13 novembre 2009 04:43
Toutes les réponses
- Hi,
You can develop your own custom control for this property.
You need to be similar with OnPaint,MouseDown,MouseUp and MouseMove events.
Here is a sample that draws a line on the form and allows user to move it at runtime.
public class MyLine:Control { bool drawBold = false; protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Pen p = null; if (drawBold) { p= new Pen(this.ForeColor, this.Height); } else { p= new Pen(this.ForeColor, this.Height-5); } e.Graphics.DrawLine(p , 0, this.Height/2, this.Width, this.Height/2); } protected override void OnMouseHover(EventArgs e) { base.OnMouseHover(e); drawBold = true; this.Refresh(); } protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); drawBold = false; this.Refresh(); } //to move control Point p = new Point(); protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); p.X = e.X; p.Y = e.Y; } protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (e.Button == MouseButtons.Left) { this.Location = new Point(e.X - p.X + this.Location.X, e.Y - p.Y + this.Location.Y); } } }- Marqué comme réponseYichun_FengMSFT, Modérateurvendredi 13 novembre 2009 04:43

