c#:拖动功能

需求:放在图层上一个图片,要实现鼠标可以选中,并实现拖放功能。

代码实现:

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;
                }
            }
        }
    }
}
时间: 2024-12-14 00:46:11

c#:拖动功能的相关文章

C#中winform去掉边框后实现拖动功能

在winform中,当我们把窗体的属性中 FormBorderStyle 指示窗体的边框和标题栏的外观和行为的属性设置为None时,我们会发现拖动窗体的功能也随之消失,这时候我们可以使用MouseDown事件来注册拖动移动窗体的功能,利用Windows的API,实现代码如下 [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] p

winform中拖动功能实现技巧

实现的需求,我通过拖动选中的用户行放到左边的机构节点上,从而实现用户改变组织机构的关系 如下图 贴代码 private DataGridViewSelectedRowCollection sourceRowCollection = null; private int rowIndexFromMouseDown; /// <summary> /// 用户拖动到机构里改变所属机构关系 /// </summary> /// <param name="sender"

[TimLinux] JavaScript 模态框可拖动功能实现——jQuery版

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>jQuery - Drag</title> <style> .modal { position: fixed; left: 0; top: 0; width: 100%; height: 100%; background-color: rgba(100, 100, 100, 0.5);

原生JS实现拖动功能

代码: 1 function drag(id){ 2 3 var obj = document.getElementById(id), 4 resultX = 0, 5 resultY = 0; 6 7 function getPos(t){ 8 var offsetLeft = 0, 9 offsetTop = 0, 10 offsetParent = t; 11 12 while(offsetParent){ 13 offsetLeft+=offsetParent.offsetLeft; 1

[转]用jwplayer+Nginx搭建视频点播服务器,解决拖动加载慢的问题

flv视频可以采用两种方式发布: 一.普通的HTTP下载方式 二.基于Flash Media Server或Red5服务器的rtmp/rtmpt流媒体方式. 多数知名视频网站都采用的是前一种方式. 两种方式对比如下:Http :生成关键帧后可拖动播放. 下载完成后不再消耗服务器资源Rtmp/rtmpt: 任意拖动播放 .无缓存,每次播放都会消耗服务器资源 这里说一下怎么用nginx 搭建http 方式的视频点播服务器,主要分为三步: 一.      nginx安装及配置1.    下载nginx

Highcharts图表的注解功能

Highcharts图表的注解功能 在图表中,往往须要对图表总体或者部分元素进行对应注解.帮助浏览者阅读图表.尽管标签组labels能够实现类似的功能.可是其功能相对简单.要实现复杂的注解功能,用户能够借助第三方插件Annotations实现. 图表注解 加入的注解.能够有多种形式的边框,而且能够设置拖动功能.图标浏览者还能够通过注解工具栏手动加入注解. PS:具体教程已经加入到<网页图表Highcharts实践教程基础篇>v1.2.3中.

WPF学习- AllowDrop 用户控件启用拖放功能

知识点: 创建自定义用户控件(UserControl) 使用户控件成为拖动源 使用户控件成为放置目标 使面板能够接收从用户控件放置的数据 创建项目: 1.新建WPF项目(Wpf-AllowDrop) 2.在MainWindow.xaml的 Grid控件添加源码 <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Sta

给Jquery添加alert,prompt方法,类似系统的Alert,Prompt,可以响应键盘,支持拖动

我们在调用系统的Alert,prompt的弹出提示时,不同的系统会有不同的提示框,视觉效果不统一,而且不好看,功能单一,现在我们通过Jquery模拟Alert,prompt,现实统一视觉效果,而且内容丰富的弹出提示. Jquery可以扩展自己的功能,如果对Jquery开发插件不熟悉的人可以到官方网去看看文档,比较简单易懂. Js代码   /* *  本插件基于JQUERY *  Jquery版本: 1.7.2 *        Date:2012-06-28 *      Author:King

Android 拖拽功能的使用实例

图片的拖拉功能是处理图片进一个有用且常用的功能,由于手机屏幕尺寸的限制,往往无法在手机上一次性的显示一张比较大的图片,也就是 说,我们在手机上一次性只能看到图片的一部分,此时就可以使用图片的拖动功能来拖动图片,进而查看图片相应的部分. 下面通过一个例子来学习实现图片的拖拉功能: 实例运行如下:1.这初始化的界面.2.为向左拖动后的效果 3.为向上拖动的效果         知识点: 在anndroid应用程序开发中,我们经常使用ImageView时经常会用到scaleType属性,如: <Ima