发送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) { } } }
方法还有哦: 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); }
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); } } }
也可以在窗口的MouseMove和MouseDown事件中,通过鼠标偏移量来修改窗口的Location即可
使用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);
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)
{ }
}
}
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;
}
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;
}
}
}
}
}
把五楼的代码直接粘贴在窗体类中就可以了。
就粘贴在这段代码的上方好了
//
....粘贴在这里
//
public Form1()
{
InitializeComponent();
}
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);
}
}
}