鼠标可以点窗体内任何一处。进行拖动操作

解决方案 »

  1.   

    你需要处理MouseDown MouseMove MouseUp事件
    在MouseDown时候记录初始位置
    MouseMove调整窗体的位置
    MouseUp时取消窗体的移动
      

  2.   

    就是location加加减减的吧
    MouseDown时候
    if 鼠标左键{
      记录此时x,y;
      onMove = true;
    }MouseMove时候
    if(onMove){
      this.Location = new Point(this.Location.X - 记录的原x + e.X,this.Location.Y - 记录的原y + e.Y);
    }MouseUp时候
    if 鼠标左键{
      onMove = false;
    }大概是这样吧  你试试  我没有测
      

  3.   

    protected override void WndProc(ref Message m)
            {
                if (m.Msg == 0x84 /*WM_NCHITTEST*/)
                {
                    // 分解当前鼠标的坐标
                    int nPosX = (int)m.LParam & 0xFFFF;
                    int nPosY = (int)m.LParam >> 16;
                    if (ClientRectangle.Contains(PointToClient(new Point(nPosX, nPosY))))
                    {
                        m.Result = (IntPtr)2; // HTCAPTION;
                        return;
                    }
                }
                base.WndProc(ref m);
            }
      

  4.   

    http://community.csdn.net/Expert/topic/5235/5235280.xml?temp=.8694269
      

  5.   

    就是
    protected override void WndProc(ref Message m)
      这个是自己写。。还是事件里面的生成的?
      

  6.   

    还有一个方法:private Point mouse_offset = new Point(0,0);private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    this.mouse_offset = new Point(-e.X,-e.Y);
    }private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if(e.Button == MouseButtons.Left)
    {
    Point mousepos = Control.MousePosition;
    mousepos.Offset(this.mouse_offset.X,this.mouse_offset.Y-SystemInformation.CaptionHeight);
    this.Location = mousepos;
    }
    }
      

  7.   

    ////拖动窗体
            //private const int WM_NCHITTEST = 0x84;
            //private const int HTCLIENT = 0x1;
            //private const int HTCAPTION = 0x2;        //protected override void WndProc(ref Message m)
            //{
            //    switch (m.Msg)
            //    {
            //        case WM_NCHITTEST:
            //            base.WndProc(ref m);
            //            if ((int)m.Result == HTCLIENT)
            //                m.Result = (IntPtr)HTCAPTION;
            //            return;
            //           // break;
            //    }
            //    base.WndProc(ref m);
            //} 把这段代码加上就可以 试试