vs2005 C# 如何实现拖动无边框窗体,在网上看到很多代码,但是按照上面的代码来做,还是不行,还请各位弟兄多多帮忙,兄弟感激不尽,先行谢过!

解决方案 »

  1.   

    [DllImport("user32.dll")]
    public extern static long SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    public const int WM_SYSCOMMAND = 0x0112;
    [DllImport("user32.dll")]
    public extern static bool ReleaseCapture();//这个事件是要点住拖动的控件的事件,不是直接点form上写成点住控件的事件
    private void Form_MouseDown(object sender, MouseEventArgs e)
    {
          ReleaseCapture();
          SendMessage(this.Handle, WM_SYSCOMMAND, 0xF017, 0);
    }
      

  2.   

    说说哪看不懂吧
    API不会用?还是事件不会加?
      

  3.   

    很抱歉,API不会用,谢谢,以前我看过,但是不知怎么用,因此才来请教各位,谢谢上面二位弟兄.
      

  4.   

    能帮我看下这段代码,那个地方错了吗?谢谢.
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace movewindows
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }         private System.ComponentModel.Container components = null;        private bool moveFlag = false;
            private int x = 0;
            //private System.Windows.Forms.Button button1;
            private int y =0;
         // 重写private void InitializeComponent()方法:代码如下:
        
            protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
            {
                if(moveFlag && (e.Button == MouseButtons.Left))
                    this.SetBounds( Left + e.X - x, Top + e.Y - y, this.Width, this.Height);
                base.OnMouseMove(e);
            }        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
            {
                if( !moveFlag && e.Clicks >= 1)
                    moveFlag = true;
                x = e.X;
                y = e.Y;
                base.OnMouseDown(e);
            }        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
            {
                if( moveFlag )
                    moveFlag = false;
                base.OnMouseUp(e);
            }
           
            private void button1_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }提示是The type 'movewindows.Form1' already contains a definition for 'components'
      

  5.   


           Point MyOffest;//        private void FrmShow_MouseDown(object sender, MouseEventArgs e)
            {
                MyOffest = new Point(-e.X, -e.Y);
            }        private void FrmShow_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    Point MyPos = Control.MousePosition;
                    MyPos.Offset(MyOffest.X, MyOffest.Y);
                    this.DesktopLocation = MyPos;
                }
            }
      

  6.   

    TO白荷,
    我把你的代码加上去,运行后移动不了窗体,帮看看错在哪里,我的窗体名称是Form1.
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace movewindows
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        Point MyOffest;//        private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                MyOffest = new Point(-e.X, -e.Y);
            }        private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    Point MyPos = Control.MousePosition;
                    MyPos.Offset(MyOffest.X, MyOffest.Y);
                    this.DesktopLocation = MyPos;
                }
            }       
            private void button1_Click(object sender, EventArgs e)
            {
                this.Close();
            }        private void Form1_Load(object sender, EventArgs e)
            {        }
        }
    }
      

  7.   

    [DllImport("user32.dll")] 
    public extern static long SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); 
    public const int WM_SYSCOMMAND = 0x0112; 
    [DllImport("user32.dll")] 
    public extern static bool ReleaseCapture(); //这个事件是要点住拖动的控件的事件,不是直接点form上写成点住控件的事件 
    private void Form_MouseDown(object sender, MouseEventArgs e) 

          ReleaseCapture(); 
          SendMessage(this.Handle, WM_SYSCOMMAND, 0xF017, 0); 
    }
      

  8.   

    to kevin_cheung 
    您好,您说的和上面二楼任兄说的一样,但是我不知道API应该在哪里调用,帮下忙,谢谢!
      

  9.   

    直接在mouse_move里添加if(this.is_MouseDown)
    {
         this.Top=MousePosition.Y-btpt.Y;
         this.Left=MousePosition.X-btpt.X;
    }在moudeDown 中添加this.btpt.X=e.X;
    this.btpt.Y=e.Y;
    this.is_MouseDown=true;
      

  10.   

    API不用调用
    直接这么写就行
    再引进
    using System.Runtime.InteropServices;
      

  11.   

    using System.Runtime.InteropServices;API不用调用 直接这么写就成
    把上面的using+上就可以了
      

  12.   

    简单点就是处理2个事件。
    MouseDown和MouseMove
    MouseDown时候记录当前窗体在显示屏幕上的坐标。
    MouseMove的时候,不断获取新坐标并把坐标值赋给窗体的Location
      

  13.   

    To:cancerser
    你说的加上这个using System.Runtime.InteropServices; 这加上了,还是不行,你再帮下忙,看是错在哪里,真的不好意思,一直麻烦你,兄弟先谢谢你了.using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;using System.Runtime.InteropServices; 
    namespace movewindows
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }     //你的代码就加在这里
            [DllImport("user32.dll")]
            public extern static long SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
            public const int WM_SYSCOMMAND = 0x0112;
            [DllImport("user32.dll")]
            public extern static bool ReleaseCapture();        //这个事件是要点住拖动的控件的事件,不是直接点form上写成点住控件的事件 
            private void Form_MouseDown(object sender, MouseEventArgs e)
            {
                ReleaseCapture();
                SendMessage(this.Handle, WM_SYSCOMMAND, 0xF017, 0);
            } 
     
     
           
            private void button1_Click(object sender, EventArgs e)
            {
                this.Close();
            }        private void Form1_Load(object sender, EventArgs e)
            {        }       
        }
    }
      

  14.   

    To: assky124
    你的代码我试过也不行,提示btpt.X和btpt.Y 还有is_MouseDown没有定义.
      

  15.   

    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Text; 
    using System.Windows.Forms; namespace movewindows 

        public partial class Form1 : Form 
        { 
            public Form1() 
            { 
                InitializeComponent(); 
            }         Point MyOffest;//         private void Form1_MouseDown(object sender, MouseEventArgs e) 
            { 
                MyOffest = new Point(-e.X, -e.Y); 
            }         private void Form1_MouseMove(object sender, MouseEventArgs e) 
            { 
                if (e.Button == MouseButtons.Left) 
                { 
                    Point MyPos = Control.MousePosition; 
                    MyPos.Offset(MyOffest.X, MyOffest.Y); 
                    this.DesktopLocation = MyPos; 
                } 
            }       
            private void button1_Click(object sender, EventArgs e) 
            { 
                this.Close(); 
            }         private void Form1_Load(object sender, EventArgs e) 
            { 
                this.MouseDown +=new MouseEventHandler(Form1_MouseDown);//你没有添加事件
                this.MouseMove +=new MouseEventHandler(Form1_MouseMove);
            } 
        } 
    }
      

  16.   


    btpt;鼠标按下时的点的屏幕坐标
    // 声明为 Point btpt = new Point();