private Point mouse_offset;private void Control_MouseDown(object sender,System.Windows.Forms.MouseEventArgs e)
{
      mouse_offset = new Point(-e.X,-e.Y);
}private void Control_MouseMove(object sender,System.Windows.Forms.MouseEventArgs e)
{
   if (e.Button == MouseButtons.Left)
   {
      Point mousePos = Control.MousePosition;
      mousePos.Offset(mouse_offset.X, mouse_offset.Y);
      ((Control)sender).Location = ((Control)sender).Parent.PointToClient(mousePos);
   }
}

解决方案 »

  1.   

    To Tigatron(Illidian):
     该代码可实现控件移动,但是却不能画控件的移动柄。
      

  2.   


    经过不断实践,本人发现一种实现方法,与大家共享,如果有何建议,请勿吝啬,跟贴或发Email给我都可,Email:[email protected]
      

  3.   

    tracker.csusing System;
    using System.Drawing;
    using System.Windows.Forms;namespace Second
    {
    /// <summary>
    /// Tracker &micro;&Auml;&Otilde;&ordf;&Ograve;&ordf;&Euml;&micro;&Atilde;÷&iexcl;&pound;
    /// </summary>
    public class Tracker
    {
    public enum Hits {Nothing = 0, LeftTop, Top, RightTop, Right, RightBottom, Bottom, LeftBottom, Left, Middle};
    public enum MouseEvent {Down, Up, Move};
    public Tracker()
    {
    //
    // TODO: &Ocirc;&Uacute;&acute;&Euml;&acute;&brvbar;&Igrave;í&frac14;&Oacute;&sup1;&sup1;&Ocirc;ì&ordm;&macr;&Ecirc;&yacute;&Acirc;&szlig;&frac14;&shy;
    TrackRect = new Rectangle(rect.Left, rect.Top, rect.Width, rect.Height);
    //
    } /// <summary>
    /// Get all rects of eight grab-handlers
    /// </summary>
    /// <returns>array of rects</returns>
    private Rectangle[] GetHandleRects()
    {

    Rectangle[] rects = new Rectangle[HandlerCount]; rects[0] = new Rectangle(rect.Left - HandleSize, rect.Top - HandleSize, HandleSize, HandleSize);  //LeftTop
    rects[1] = new Rectangle((rect.Left + rect.Right - HandleSize) / 2, rect.Top - HandleSize, HandleSize, HandleSize); //Top
    rects[2] = new Rectangle(rect.Right, rect.Top - HandleSize, HandleSize, HandleSize); //RightTop
    rects[3] = new Rectangle(rect.Right, (rect.Top + rect.Bottom - HandleSize) / 2, HandleSize, HandleSize); //Right
    rects[4] = new Rectangle(rect.Right, rect.Bottom, HandleSize, HandleSize); //RightBottom
    rects[5] = new Rectangle((rect.Left + rect.Right - HandleSize) / 2, rect.Bottom, HandleSize, HandleSize); //Bottom
    rects[6] = new Rectangle(rect.Left - HandleSize, rect.Bottom, HandleSize, HandleSize); //LeftBottom
    rects[7] = new Rectangle(rect.Left - HandleSize, (rect.Top + rect.Bottom - HandleSize) / 2, HandleSize, HandleSize); //Left return rects;
    } /// <summary>
    /// Draw tracker
    /// </summary>
    /// <param name="g">Graphics</param>
    public virtual void Draw(Graphics g)
    {
    Rectangle area = new Rectangle(rect.Left - HandleSize, rect.Top - HandleSize, rect.Width + 2 * HandleSize, rect.Height + 2 * HandleSize);
    ControlPaint.DrawBorder(g, area, Color.Blue, ButtonBorderStyle.Dotted);
    ControlPaint.DrawBorder(g, rect, Color.Blue, ButtonBorderStyle.Dotted);
    //ControlPaint.DrawButton(g, rect, ButtonState.Normal); Rectangle[] rects = GetHandleRects();
    foreach(Rectangle rectangle in rects)
    {
    ControlPaint.DrawGrabHandle(g, rectangle, true, true);
    } if(!TrackRect.Equals(rect))
    {
    ControlPaint.DrawBorder(g, TrackRect, Color.Blue, ButtonBorderStyle.Dotted);
    }
    } /// <summary>
    /// Get handle  been hitted
    /// </summary>
    /// <param name="point">the point hitted</param>
    /// <returns>the handle  been hitted</returns>
    public Hits HitTest(Point point)
    {
    if( rect.Contains(point) )
    return Hits.Middle;
    Rectangle[] rects = GetHandleRects();
    for(Hits i = Hits.LeftTop; (int)i <= rects.Length; i++)
    {
    if( rects[(int)i - 1].Contains(point))
    return i;
    } return Hits.Nothing;
    } /// <summary>
    /// set the current cursor depend on handle been hitted
    /// </summary>
    /// <param name="form">the form track on</param>
    /// <param name="hit">the handle been hitted</param>
    public void SetCursor(Form form, Hits hit)
    {
    switch(hit)
    {
    case Hits.LeftTop:
    case Hits.RightBottom:
    form.Cursor = Cursors.SizeNWSE;
    break;
    case Hits.Top:
    case Hits.Bottom:
    form.Cursor = Cursors.SizeNS;
    break;
    case Hits.RightTop:
    case Hits.LeftBottom:
    form.Cursor = Cursors.SizeNESW;
    break;
    case Hits.Left:
    case Hits.Right:
    form.Cursor = Cursors.SizeWE;
    break;
    case Hits.Middle:
    form.Cursor = Cursors.SizeAll;
    break;
    default:
    form.Cursor = Cursors.Arrow;
    break;
    } }