c#:拖動功能

來源:互聯網
上載者:User

標籤:

需求:放在圖層上一個圖片,要實現滑鼠可以選中,並實現拖放功能。

代碼實現:

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;        private int 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;        }        private void 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;                }            }        }    }}

 

c#:拖動功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.