本帖最后由 sgyiliya 于 2012-08-09 14:59:28 编辑

解决方案 »

  1.   

    给你点思路//按下鼠标左键激发事件
            private void lbl1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                this.isdrag = true;
            }        //移动鼠标激发事件
            private void lbl1_MouseMove(object sender, MouseEventArgs e)
            {
                if (!isdrag)
                    return;
                UIElement control = sender as UIElement;
                X = e.GetPosition(canvas1).X - control.DesiredSize.Width / 2;
                Y = e.GetPosition(canvas1).Y - control.DesiredSize.Height / 2;
                control.SetValue(Canvas.LeftProperty, X);
                control.SetValue(Canvas.TopProperty, Y);
            }        //放开鼠标左键激发事件
            private void lbl1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                this.isdrag = false;
                UIElement control = sender as UIElement;
                control.SetValue(Canvas.LeftProperty, point.X - control.DesiredSize.Width / 2);
                control.SetValue(Canvas.TopProperty, point.Y - control.DesiredSize.Height / 2);
                if (X > 300 && X < 375 && Y > 0 && Y < 108)
                {
                    this.UpdateUser();
                }             }
      

  2.   

    private bool Mousedown = false;  //鼠标按下为true
    private int CurX = 0,CurY = 0;
    private System.Windows.Forms.Label label1;/// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void label1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    CurX = e.X;//保存鼠标按下是鼠标在窗口的坐标
    CurY = e.Y;
    Mousedown = true;
    this.label1.Capture = true;
    } private void label1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if(Mousedown)
    {
    //Cursor.Position .X Cursor.Position .Y 是屏幕坐标
    //用屏幕坐标减窗口坐标以保持鼠标总是在窗口的原位置.
    Point sp = new Point(Cursor.Position.X,Cursor.Position.Y);
    Point np = this.PointToClient(sp);
    this.label1.Location = new Point(np.X - CurX,np.Y - CurY);//(Cursor.Position .X - CurX,Cursor.Position .Y - CurY);
    }
    } private void label1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    Mousedown = false;
    this.label1.Capture = false;
    }
      

  3.   

    用了您的代码后,提示:
    UIElement,
    e.GetPosition(canvas1),
    this.UpdateUser(),这些地方无法通过编译,是缺少了哪个using吗?请指教,谢谢!
      

  4.   

    用了您的代码后,提示:
    UIElement,
    e.GetPosition(canvas1),
    this.UpdateUser(),这些地方无法通过编译,是缺少了哪个using吗?请指教,谢谢!
      

  5.   

    试用了几种办法,包括楼上  isjoe  大侠的办法,都只能实现在panelControl1内的移动,而无法突破panelControl1移动到panelControl2上面。
    该怎么办?大侠们多多指教啊,感激不尽!
      

  6.   

    jdc71264 大侠在吗?
    用了您的代码后,提示:
    UIElement,
    e.GetPosition(canvas1),
    this.UpdateUser(),这些地方无法通过编译,是缺少了哪个using吗?请指教,谢谢!
      

  7.   

    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;namespace WindowsFormsApplication4
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        [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_MouseDown(object sender, MouseEventArgs e)
            {
                ReleaseCapture();
                SendMessage(button1.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            }        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
            {
                ReleaseCapture();
                SendMessage(pictureBox1.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            }        private void textBox1_MouseDown(object sender, MouseEventArgs e)
            {
                ReleaseCapture();
                SendMessage(textBox1.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            }
        }
    }
      

  8.   

    额,,,我是给你个思路,我这个是WPF的,你肯定编译不懂过啊,楼上的几位大神都基本差不多,思路基本都是这样子,具体实现你自己好好琢磨吧,伸手就拿不是个好习惯