c#拖动无标题窗体 1.引入命名空间 using System.Runtime.InteropServices; 2. [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; 3.在空间的_MouseDown中加入如下代码: 如: private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); }
private bool IsMouseDown;private void Login_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
int Xoffset;
int Yoffset;
if(e.Button==MouseButtons.Left)
{
Xoffset=-e.X-SystemInformation.FrameBorderSize.Width;
Yoffset=-e.Y-SystemInformation.FrameBorderSize.Height;
MouseSet=new Point(Xoffset,Yoffset);
IsMouseDown=true;
}
} private void Login_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(IsMouseDown)
{
Point MousePos=Control.MousePosition;
MousePos.Offset(MouseSet.X,MouseSet.Y);
Location=MousePos; }
} private void Login_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
IsMouseDown=false;
}
}这样不行吗
1设置form的一些属性
this.FormBorderStyle = FormBorderStyle.None; //去掉边框
this.TopMost = true; //设为最上一层
2写移动函数
参考
fengyuhong() ( )的函数(其实就是c#做不规则窗体那一篇文章的移动函数,怪不得我怎么越看越觉得眼熟悉呢,哈哈 ^-^,不过会找资料是一种能力,而且是程序员必须具备的能力哈)
using System.Runtime.InteropServices;
2.
[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;
3.在空间的_MouseDown中加入如下代码:
如:
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}