C#如何通过鼠标控制无标头窗体!

解决方案 »

  1.   

    搂主可以使用WinAPI,
    也可以在窗口的MouseMove和MouseDown事件中,通过鼠标偏移量来修改窗口的Location即可
      

  2.   

    注册3个事件,Form.MouseDown,Form.MouseMove,Form.MouseUp,
    使用1个bool成员变量isMouseLeftButtonDown判断是否鼠标左键按下。
    使用一个Point成员变量mouseDownPoint判断Mouse按下时的坐标。初始值为(0,0)在MouseDown中,判断是否鼠标左键按下,按下则设置isMouseLeftButtonDown = true;
    mouseDownPoint = e.Location;在MouseMove中,如果isMouseLeftButtonDown为true,根据鼠标当前位置e.Location和mouseDownPoint的位移量,设置Form的Location位移在MouseUp中,设置isMouseLeftButtonDown = false; mouseDownPoint = new Point(0, 0);
      

  3.   

    发送SC_Move消息就可以了
    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 WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            [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 Form1_MouseDown(object sender, MouseEventArgs e)
            {
                ReleaseCapture();
                SendMessage(this.Handle.ToInt32(), WM_SysCommand, SC_MOVE, 0);
            }        private void Form1_Load(object sender, EventArgs e)
            {        }
        }
    }
      

  4.   

    方法还有哦:
     Point oldPoint = new Point(0, 0);
            bool mouseDown = false;       
            public Form1()
            {
                InitializeComponent();
                MouseDown += new MouseEventHandler(Form1_MouseDown);
                MouseUp += new MouseEventHandler(Form1_MouseUp);
                MouseMove += new MouseEventHandler(Form1_MouseMove);
            }
           
            void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if (mouseDown)
                {
                    this.Left += (e.X - oldPoint.X);
                    this.Top += (e.Y - oldPoint.Y);
                }
            }        void Form1_MouseUp(object sender, MouseEventArgs e)
            {
                mouseDown = false;
            }        void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                oldPoint = e.Location;
                mouseDown = true;
            }
      

  5.   

    还有更简单的,欺骗一下鼠标消息原理参考我的blog
    http://blog.csdn.net/jinjazz/archive/2008/07/10/2635200.aspxusing System;
    using System.Windows.Forms;
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m); if (m.Msg == 0x84)
                {
                    switch (m.Result.ToInt32())
                    {
                        case 1:                        m.Result = new IntPtr(2); break;
                    }
                }
            }
        }
    }
      

  6.   


    把五楼的代码直接粘贴在窗体类中就可以了。
    就粘贴在这段代码的上方好了
          //
          ....粘贴在这里
          //
         public Form1()
           {
             InitializeComponent();
           }
      

  7.   

    我刚刚上传了实现拖动无标题栏窗体的源码。。希望对你有帮助啊!下载地址:http://download.csdn.net/source/850765
      

  8.   


    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 MoveNoTitleForm
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        [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, MouseEventArgs e)
            {
                ReleaseCapture();
                SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            }
        }
    }