日期:2014-05-18  浏览次数:21004 次

如何实现鼠标拖动一个控件移动?
Window我们可以用DragMove()函数实现,如果是控件呢?怎么实现拖动?

------解决方案--------------------
用鼠标的坐标赋值给控件的location来实现
------解决方案--------------------
点击按钮时监控鼠标,鼠标移动到哪里就跟着移动。

按钮释放,停止移动。
------解决方案--------------------
MouseDown中置标记isdown=true,MouseMove中,如果isdown,则根据鼠标位置控制控件的left和top,MouseUp事件中,isdown=false
------解决方案--------------------
拖动一个按钮

定义
C# code

private System.Windows.Forms.Button button1;
        private bool Mousedown = false;  //鼠标按下为true
        private int CurX = 0,CurY = 0;

------解决方案--------------------
C# code
      [DllImport("user32.dll")]
        static extern bool ReleaseCapture();
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, UInt32 wParam, UInt32 lParam);

        private readonly UInt32 WM_SYSCOMMAND = 0x112;
        private readonly UInt32 SC_MOVE = 0xF010;
        private readonly UInt32 HTCAPTION = 2;
        
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("1");
        }

        private void button1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(button1.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            }
        }

------解决方案--------------------
探讨

C# code
[DllImport("user32.dll")]
static extern bool ReleaseCapture();
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, U……

------解决方案--------------------
http://download.csdn.net/detail/wangyue4/2874530
c# 实现任意控件的拖拽
我自己写的
------解决方案--------------------
探讨

http://download.csdn.net/detail/wangyue4/2874530
c# 实现任意控件的拖拽
我自己写的