比如拖动一个textBox
using System.Runtime.InteropServices;
[DllImport("user32.dll",EntryPoint="SendMessage")]
public static extern int SendMessage(int hWnd,int wMsg,int wParam,int lParam);
[DllImport("user32.dll",EntryPoint="ReleaseCapture")]
public static extern int ReleaseCapture();
public const int WM_SysCommand = 0x0112;
public const int SC_MOVE = 0xF012;
private void textBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.textBox1.Handle.ToInt32(),WM_SysCommand,SC_MOVE,0);
}

解决方案 »

  1.   

    可以通过定义窗体的RESIZE事件来实现  需计算
      

  2.   

    海宁的方法好像会随着Windows系统设置的拖动效果的改变而改变,就是说改了“拖动时显示窗口内容”的设置,程序运行时的效果也会改变。不过这种方法用起来很方便。
    也可以继承IMessageFilter接口,截获鼠标消息之后再去处理控件的效果。
      

  3.   

    再提供一种思路,但是我没调试通,现在是只能在全屏状态下好使 ^_^//用ControlPaint.DrawReversibleFrame(rt,Color.White ,FrameStyle.Thick );画虚线框
    //(在同一位置画两次即可消除虚线框,即采用的是异或方式画图)
    //mousedown时捕捉鼠标,设置标志bMouseDown,同时调用上面语句画虚线框
    //在mousemove中,如果标志bMouseDown为true,在原来位置画虚线框(消除原来的虚线框),然后在新的位置重新画虚线框,
    //在mouseup中,如果标志bMouseDown为true,清除标志bMouseDown,取消捕捉鼠标,并再画一次虚线框,以便消除最后一条虚线框
    //在mouseup中还要做一件事情,修改文本框位置到最终要移动到的位置private bool flag = false;
    private Rectangle h;
    private void textBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    this.flag = true;
    Rectangle r = new Rectangle(this.Location.X  + this.textBox1.Location.X,this.Location.Y  + this.textBox1.Location.Y + this.textBox1.Size.Height,this.textBox1.Size.Width,this.textBox1.Size.Height);
    h = r;
    ControlPaint.DrawReversibleFrame(r,Color.White ,FrameStyle.Thick );
    }private void textBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if(this.flag) 
    {
    ControlPaint.DrawReversibleFrame(h,Color.White ,FrameStyle.Thick );
    Rectangle r = new Rectangle(this.Location.X  + this.textBox1.Location.X +e.X,this.Location.Y + this.textBox1.Location.Y +e.Y + this.textBox1.Size.Height,this.textBox1.Size.Width,this.textBox1.Size.Height);
    h = r;
    ControlPaint.DrawReversibleFrame(r,Color.White ,FrameStyle.Thick );

    System.Diagnostics.Debug.WriteLine(h.X.ToString() + " " + this.h.Y.ToString());
    }
    }private void textBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    this.flag = false;
    System.Diagnostics.Debug.WriteLine(h.X.ToString() + " " + this.h.Y.ToString());
    ControlPaint.DrawReversibleFrame(h,Color.White ,FrameStyle.Thick );
    textBox1.Location=new Point(h.X,h.Y);
    }
      

  4.   

    最后一句改为:textBox1.Location=new Point(h.X,h.Y - this.textBox1.Height);
    只能在全屏状态下好使
      

  5.   

    重要好使了^_^
    最后一句改为
    textBox1.Location= new Point( Control.MousePosition.X - this.Location.X ,Control.MousePosition.Y  - this.Location.Y - this.textBox1.Height);
      

  6.   

    比中窗体上有一个label,可以参考如下做一下:
    private void label1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    p=new Point(e.X,e.Y);
    }private void label1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if (e.Button==MouseButtons.Left )
    {
    if (ModifierKeys == Keys.Control)
    {
    Cursor=Cursors.SizeAll;
    this.label1.Size = new Size(this.label1.Width+e.X-p.X,this.label1.Height+e.Y-p.Y);
    p = new Point(e.X,e.Y);
    }
    else
    {
    this.label1.Location=new Point(e.X-p.X+this.label1.Left,e.Y-p.Y+this.label1.Top);
    }
    }
    else
    {
    this.label1.Cursor=Cursors.Hand;
    }
    }
      

  7.   

    TO 海宁:
    用类似
      ReleaseCapture();
      SendMessage(this.textBox1.Handle.ToInt32(),WM_SysCommand,SC_MOVE,0);
    的方法,
    能否实现在运行时,当控件获得焦点时,在控件四周出现边框用于调整大小,就像在.Net的窗体设计器里调整控件大小一样。
      

  8.   

    不知道,看看 hbxtlhx(下着春雨的天) 的代码吧
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace WindowsApplication4
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private CusTextBox textBox1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;

    public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } public class CusTextBox: System.Windows.Forms.TextBox
    {
    private const int WM_NCHITTEST = 0x84;
    private const int WM_SIZING = 532; private const int HTERROR = (-2);
    private const int HTTRANSPARENT = (-1);
    private const int HTNOWHERE = 0;
    private const int HTCLIENT =  1;
    private const int HTCAPTION = 2;
    private const int HTSYSMENU = 3;
    private const int HTGROWBOX = 4;
    private const int HTSIZE = HTGROWBOX;
    private const int HTMENU = 5;
    private const int HTHSCROLL = 6;
    private const int HTVSCROLL = 7;
    private const int HTMINBUTTON = 8;
    private const int HTMAXBUTTON = 9;
    private const int HTLEFT = 10;
    private const int HTRIGHT = 11;
    private const int HTTOP = 12;
    private const int HTTOPLEFT = 13;
    private const int HTTOPRIGHT = 14;
    private const int HTBOTTOM = 15;
    private const int HTBOTTOMLEFT = 16;
    private const int HTBOTTOMRIGHT = 17;
    private const int HTBORDER = 18;
    private const int HTREDUCE = HTMINBUTTON;
    private const int HTZOOM = HTMAXBUTTON;
    private const int HTSIZEFIRST = HTLEFT;
    private const int HTSIZELAST = HTBOTTOMRIGHT;
    /* WINVER >= 0x0400 */
    private const int HTOBJECT = 19;
    private const int HTCLOSE = 20;
    private const int HTHELP = 21;
    /* WINVER >= 0x0400 */ private const int BorderWidth = 3;

    private int CaptionHeight = 17; //可以调整窗体的大小和移动窗体的位置
    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
    switch(m.Msg)
    {
    case WM_NCHITTEST:
    base.WndProc(ref m);
    if (DesignMode)
    {
    return;
    }
    if ((int)m.Result == HTCLIENT)
    {
    m.HWnd = this.Handle;
    int pLeft=0;
    int pTop=0; bool IsControl = true;
    if (IsControl)
    {
    if ((this.Parent as Form).FormBorderStyle==FormBorderStyle.None)
    {
    pLeft=this.Parent.Left;
    pTop=this.Parent.Top;
    }
    else
    {
    pLeft=this.Parent.Left+2;
    CaptionHeight = this.Parent.Height-this.Parent.ClientRectangle.Height-2 * BorderWidth;
    pTop=this.Parent.Top + CaptionHeight + BorderWidth-2;
    }
    } if ((Cursor.Position.X<=pLeft + this.Left + BorderWidth) && (Cursor.Position.Y <= pTop + this.Top + BorderWidth))
    m.Result = (IntPtr)HTTOPLEFT;//左上
    else if ((Cursor.Position.X>=pLeft + this.Left + this.Width-BorderWidth) && (Cursor.Position.Y<=pTop + this.Top +BorderWidth))
    m.Result = (IntPtr)HTTOPRIGHT;//右上
    else if ((Cursor.Position.X <= pLeft + this.Left + BorderWidth) && (Cursor.Position.Y>=pTop + this.Top + this.Height-BorderWidth))
    m.Result = (IntPtr)HTBOTTOMLEFT;//左下
    else if ((Cursor.Position.X>=pLeft + this.Left + this.Width-BorderWidth) && (Cursor.Position.Y>=pTop + this.Top + this.Height-BorderWidth))
    m.Result = (IntPtr)HTBOTTOMRIGHT;//右下
    else if (Cursor.Position.X<=pLeft + this.Left + BorderWidth-1)
    m.Result = (IntPtr)HTLEFT;//左
    else if (Cursor.Position.X>=pLeft + this.Left + this.Width-BorderWidth)
    m.Result = (IntPtr)HTRIGHT;//右
    else if (Cursor.Position.Y<=pTop + this.Top + BorderWidth-1)
    m.Result = (IntPtr)HTTOP;//上
    else if (Cursor.Position.Y>=pTop + this.Top + this.Height-BorderWidth)
    m.Result = (IntPtr)HTBOTTOM;//下
    else //if (Cursor.Position.Y<=pTop + this.Top + CaptionHeight + BorderWidth )//在靠近上部可以托拽控件
    {
    //双击控件会全屏(不知道原因)
    m.Result = (IntPtr)HTCAPTION;//移动控件
    // if (Cursor.Position.X<=pLeft + this.Left + BorderWidth + CaptionHeight)
    // {
    // m.Result = (IntPtr)HTSYSMENU;
    // }
    // else
    // {
    // m.Result = (IntPtr)HTCAPTION;
    // }
    }
    }
    return;
    default:
    base.WndProc(ref m);
    break;
    }

    }
    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.textBox1 = new CusTextBox();
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(96, 88);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "textBox1";
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(536, 317);
    this.Controls.Add(this.textBox1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    }
    }
    }
      

  9.   

    .Net控件有一个Anchor属性,设置以下就可,别把.net想的那么白吃