c#:拖动功能
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#:拖动功能,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2332字,纯文字阅读大概需要4分钟。
内容图文
需求:放在图层上一个图片,要实现鼠标可以选中,并实现拖放功能。
代码实现:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace workflowPro { public partial class Form1 : Form { private int offsetX = 0; privateint offsetY = 0; public List<Control> panelControls = new List<Control>(); public Dictionary<Control, List<List<Point>>> rules = new Dictionary<Control, List<List<Point>>>(); public Form1() { InitializeComponent(); PictureBox pictureBox = new PictureBox(); pictureBox.Image = System.Drawing.Bitmap.FromFile(AppDomain.CurrentDomain.BaseDirectory + "\\201208241209467792.png"); pictureBox.Height = 80; pictureBox.Width = 80; pictureBox.MouseDown += mouseDown; pictureBox.MouseUp += mouseUp; pictureBox.MouseMove += mouseMove; panelControls.Add(pictureBox); offsetX = this.panel1.Location.X; offsetY = this.panel1.Location.Y; this.panel1.Controls.Add(pictureBox); } private Point mouseDownPoint = new Point(); private Control selectedControl = new Control(); private Control moveToControl = new Control(); void mouseDown(object sender, MouseEventArgs e) { this.selectedControl = sender as Control; if (e.Button == System.Windows.Forms.MouseButtons.Left) { this.lblPosition.Text = "(" + e.X + "," + e.Y + ")|"; mouseDownPoint = e.Location; } } void mouseMove(object sender, MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Left && this.selectedControl != null) { Point point = this.PointToClient(this.selectedControl.PointToScreen(new Point(e.X - mouseDownPoint.X, e.Y - mouseDownPoint.Y))); this.selectedControl.Location = point; this.selectedControl.Location.Offset(offsetX, -offsetY); this.lblPosition.Text = "|(" + point.X + "," + point.Y + ")"; } } void mouseUp(object sender, MouseEventArgs e) { this.selectedControl = null; } privatevoid MoveToWho(MouseEventArgs e) { foreach (Control control in panelControls) { int x1 = control.Location.X + offsetX; int y1 = control.Location.Y + offsetY; int x2 = x1 + control.Width; int y2 = y1 + control.Height; if (e.X > x1 && e.X < x2 && e.Y > y1 && e.Y < y2) { this.moveToControl = control; } } } } }
原文:http://www.cnblogs.com/yy3b2007com/p/5742586.html
内容总结
以上是互联网集市为您收集整理的c#:拖动功能全部内容,希望文章能够帮你解决c#:拖动功能所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。