SelectedControls 用ArrayList代替
在picturebox的mouse事件里
bool bMouseDown ;
Dragger drag;
void MouseDown(object sender,MouseEventArgs e)
{
  bMouseDown = true;
  Control c = sender as Control;
  ArrayList s = new ArrayList();
  s.add(c);
  drag = new Dragger(c.Parent,s,new Point(e.X,e.Y));
}
void MouseDown(object sender,MouseEventArgs e)
{
  if (bMouseDown)
      drag.DragTo(new Point(e.X,e.Y));
}
void MouseDown(object sender,MouseEventArgs e)
{
  if (bMouseDown)
  {
     bMouseDown = false;
     drag.End(new Point(e.x,e.y));
     drag = null;
   }}
public class Dragger
{
Control parent;
SelectedControls items;
Point location,current;

public Dragger(Control theParent, SelectedControls theItemsToDrag, Point theStartingPoint)
{
parent = theParent;
items = theItemsToDrag;
location = theStartingPoint;
current = location;
  
// parent.Capture = true;
Cursor.Clip = parent.RectangleToScreen(parent.ClientRectangle);
} public void DragTo(Point thePoint)
{
this.Draw(current );
this.Draw(thePoint);
current = thePoint;
} void Draw(Point p)
{
int xDelta = p.X - location.X;
int yDelta = p.Y - location.Y;
Rectangle rt ;
if (xDelta == 0 && yDelta == 0)
return;

foreach (Control c in items)
{
rt = c.Bounds ;
rt.Offset(xDelta, yDelta);
rt = parent.RectangleToScreen(rt);
ControlPaint.DrawReversibleFrame(rt,Color.White ,FrameStyle.Thick );
}
}
public void End(Point p)
{
this.Draw(p);
int xDelta = p.X - location.X ;
int yDelta = p.Y - location.Y;
Rectangle rt ; foreach(Control c in items)
{
rt = c.Bounds; rt.Offset(xDelta,yDelta);
c.Bounds = rt;
}
Cursor.Clip = Rectangle.Empty;
parent.Capture = false;
}
}

解决方案 »

  1.   

    为什么会有三个void MouseDown(object sender,MouseEventArgs e)?
    还有,能不能讲一下算法?这么大段的代码看起来很费劲,程序很快要交了,我没那么多时间阿
      

  2.   

    参考这个:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Runtime.InteropServices;  namespace Example013_无标题窗体的拖动
    {
    public class Form1 : System.Windows.Forms.Form
    {
    private System.ComponentModel.Container components = null;
    public Form1()
    {
    InitializeComponent();
    }
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }
    #region Windows Form Designer generated code
    private void InitializeComponent()
    {
    System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.BackgroundImage = ((System.Drawing.Bitmap)(resources.GetObject("$this.BackgroundImage")));
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    this.Name = "Form1";
    this.Text = "Form1";
    this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
    }
    #endregion

    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } [DllImport("user32.dll")]
    public static extern bool ReleaseCapture();
    [DllImport("user32.dll")]
    public static extern bool SendMessage(IntPtr hwnd,int wMsg,int wParam,int lParam);
     
    public const int WM_SYSCOMMAND=0x0112;
    public const int SC_MOVE=0xF010;
    public const int HTCAPTION=0x0002;

    private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    ReleaseCapture();
    SendMessage(this.Handle,WM_SYSCOMMAND,SC_MOVE+HTCAPTION, 0); 
    }
    }
    }
      

  3.   

    不好意思
    从上到下是MouseDown,MouseMove,MouseUp三个事件因为昨天时间匆忙,而且是在网页上直接写的,所以就出错了其实就是计算两次鼠标事件间的偏移量,然后根据控件原来的位置,计算得出现在的位置在拖动过程中并没有移动控件,只是画出一些方框随着鼠标移动,在MouseUp事件中才真正移动控件
      

  4.   

    using System.Runtime.InteropServices;
    public const int WM_SYSCOMMAND=0x0112;
    public const int SC_MOVE=0xF010;
    private System.Windows.Forms.Label label1;
    public const int HTCAPTION=0x0002;
    [DllImport("user32.dll", EntryPoint="SendMessageA")]
    private static extern int SendMessage (IntPtr hwnd, int wMsg, int wParam, int lParam);
    [DllImport("user32.dll")]
    private static extern int ReleaseCapture ();
    private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    ReleaseCapture();
    SendMessage(this.pictureBox1.Handle,WM_SYSCOMMAND,SC_MOVE+HTCAPTION, 0); }
      

  5.   

    http://community.csdn.net/Expert/topic/3563/3563219.xml?temp=.9384272