zoukankan      html  css  js  c++  java
  • 实现控件的随意拖动

    因为客户要求程序要在浏览器上运行,但是这些信息(这个程序只在政府某部门内部使用)并不需要公开,所以我们选择使用Windows应用程序,并将该程序嵌入到网页中。。。。
        就我个人做的这部分简单的说下,我负责的是在地图上画标注(“标注”在这里指的是小图标,以后都使用这个定义),对应于不同的情况,需要画出不同的标注,对于该标注需要有拖拽功能并且需要将最后的位置记录下来,以便下次载入时,会显示在上次修改的位置,修改该图标对应的信息,删除该标注,查看该标注的当前信息以及历史信息等。这里先介绍下,控件的拖拽,其原理就是让控件的位置随鼠标一起移动,鼠标移动多少像素那么控件就移动多少,那么接下来事情就很简单了。为了以后方便复用,我封装了一个类,拖拽控件直接调用这个类就成了!

    调用的代码如下:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;

    namespace WinControlLib
    {
        
    public partial class UserControl4 : UserControl
       
    {

            
    public UserControl4()
            
    {
                InitializeComponent();

                DragControlClass obj
    =new DragControlClass(button1);

            }





        }

    }

    核心部分是下面这个类:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;

    namespace WinControlLib
    {
        
    public class DragControlClass
        
    {
            
    //待拖动的控件
            private Control m_Control;
            
    //鼠标按下时的x,y坐标
            private int m_X;
            
    private int m_Y;
            
    public DragControlClass(Control control) 
            
    {
                m_Control 
    = control;
                m_Control.MouseDown 
    += new MouseEventHandler(control_MouseDown);
                m_Control.MouseMove 
    += new MouseEventHandler(contro_MouseMove);
            }

            
    private void control_MouseDown(object sender, MouseEventArgs e)
           
    {
                m_X 
    = e.X;
                m_Y 
    = e.Y;
            }

            
    private void contro_MouseMove(object sender, MouseEventArgs e)
            
    {
                
    if (e.Button == MouseButtons.Left)
                
    {
                    
    int x = e.X - m_X;
                    
    int y = e.Y - m_Y;
                    
    this.m_Control.Left += x;
                    
    this.m_Control.Top += y;

                }

            }

        }

    }


    虽然这个类代码不多,却可以节省我们很多的时间,减少很多的重复工作量,这个类可以拖拽继承自System.Windows.Form.Control的控件 ,先说到这里,希望对大家有用。
  • 相关阅读:
    html5-本地数据库的操作
    html5_storage存取实例
    html5-表单常见操作
    js操作注意事项
    php扩展地址下载
    php serialize序列化对象或者数组
    php_memcahed 使用方法
    php_memcahed telnet远程操作方法
    php_memcahed 安装
    Liunx centos 系统 修改hostname
  • 原文地址:https://www.cnblogs.com/zbo/p/1511772.html
Copyright © 2011-2022 走看看