一个拖动无标题栏窗体的方法
2009-05-06 14:56 苏飞 阅读(1406) 评论(5) 编辑 收藏 举报
//X轴为当前的位置加上窗体的长度
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
//Y轴为当前的位置加上窗体的高度
yOffset = -e.Y - SystemInformation.CaptionHeight -
SystemInformation.FrameBorderSize.Height;
//综合一下坐标
mouseOffset = new Point(xOffset, yOffset);
//表示按下了左键
isMouseDown = true;
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Invoicing_Tube
{
public partial class login : Form
{
public login()
{
InitializeComponent();
}
//定义一下坐标
private Point mouseOffset;
//判断一下是否按下了左键默认的为False
private bool isMouseDown = false;
private void login_MouseDown(object sender, MouseEventArgs e)
{
try
{
//X轴
int xOffset;
//Y轴
int yOffset;
//当按下左键时
if (e.Button == MouseButtons.Left)
{
//X轴为当前的位置加上窗体的长度
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
//Y轴为当前的位置加上窗体的高度
yOffset = -e.Y - SystemInformation.CaptionHeight -
SystemInformation.FrameBorderSize.Height;
//综合一下坐标
mouseOffset = new Point(xOffset, yOffset);
//表示按下了左键
isMouseDown = true;
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString(), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void login_MouseMove(object sender, MouseEventArgs e)
{
try
{
//如果按下了左键
if (isMouseDown)
{
//前当的坐标==鼠标的位置
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseOffset.X, mouseOffset.Y);
Location = mousePos;
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString(), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void login_MouseUp(object sender, MouseEventArgs e)
{
try
{
if (e.Button == MouseButtons.Left)
{
isMouseDown = false;
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString(), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Invoicing_Tube
{
public partial class login : Form
{
public login()
{
InitializeComponent();
}
//定义一下坐标
private Point mouseOffset;
//判断一下是否按下了左键默认的为False
private bool isMouseDown = false;
private void login_MouseDown(object sender, MouseEventArgs e)
{
try
{
//X轴
int xOffset;
//Y轴
int yOffset;
//当按下左键时
if (e.Button == MouseButtons.Left)
{
//X轴为当前的位置加上窗体的长度
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
//Y轴为当前的位置加上窗体的高度
yOffset = -e.Y - SystemInformation.CaptionHeight -
SystemInformation.FrameBorderSize.Height;
//综合一下坐标
mouseOffset = new Point(xOffset, yOffset);
//表示按下了左键
isMouseDown = true;
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString(), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void login_MouseMove(object sender, MouseEventArgs e)
{
try
{
//如果按下了左键
if (isMouseDown)
{
//前当的坐标==鼠标的位置
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseOffset.X, mouseOffset.Y);
Location = mousePos;
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString(), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void login_MouseUp(object sender, MouseEventArgs e)
{
try
{
if (e.Button == MouseButtons.Left)
{
isMouseDown = false;
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString(), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
本人的博客不再维护从2013年就不再维护了
需要我帮助的朋友请到我的个人论坛 http://www.sufeinet.com 进行讨论,感谢大家对我的支持!