当去掉WinForm的边框时候,鼠标移动窗口的方法
2013-12-20 11:42
567 查看
【方法一】窗体本身进行
#region 鼠标移动窗口
// 位置 - 用于记录鼠标位置
Point downPoint;
// 当鼠标按下时,记录鼠标位置
private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
//Point FrmP = new Point(button1.Left, button1.Top);
////ScreenP返回相对屏幕的坐标 Point ScreenP=this.PointToScreen(FrmP);
downPoint = new Point(e.X, e.Y);
}
// 当鼠标移动是,改变窗体位置
private void MainForm_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
Location = new Point(Location.X + e.X - downPoint.X, Location.Y + e.Y - downPoint.Y);
}
#endregion
【方法二】在窗体控件上进行
#region //鼠标坐标
private int xpos;
private int ypos;
#region panel1在表面,鼠标移动窗体
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
xpos = e.X;
ypos = e.Y;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Left += e.X - this.xpos;
this.Top += e.Y - this.ypos;
}
}
#endregion
【方法三】转载于博客园<小锋刚>
窗体的移动:
bool formMove = false;//窗体是否移动
Point formPoint;//记录窗体的位置
private void Form4_MouseDown(object sender, MouseEventArgs e)//鼠标按下
{
formPoint = new Point();
int xOffset;
int yOffset;
if (e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height;
formPoint = new Point(xOffset, yOffset);
formMove = true;//开始移动
}
}
private void Form4_MouseMove(object sender, MouseEventArgs e)//鼠标移动
{
if (formMove == true)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(formPoint.X, formPoint.Y);
Location = mousePos;
}
}
private void Form4_MouseUp(object sender, MouseEventArgs e)//鼠标松开
{
if (e.Button == MouseButtons.Left)//按下的是鼠标左键
{
formMove = false;//停止移动
}
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Left += e.X - this.xpos;
this.Top += e.Y - this.ypos;
}
}
#endregion
【案例】
拖动无边框窗体Form至桌面任何位置
首先建一个Windows应用程序
将Form1的 FormBorderStyle属性设置为None
Point mouseOff;//鼠标移动位置变量
bool leftFlag;//标签是否为左键
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseOff = new Point(-e.X, -e.Y); //得到变量的值
leftFlag = true; //点击左键按下时标注为true;
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (leftFlag)
{
Point mouseSet = Control.MousePosition;
mouseSet.Offset(mouseOff.X, mouseOff.Y); //设置移动后的位置
Location = mouseSet;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (leftFlag)
{
leftFlag = false;//释放鼠标后标注为false;
}
}
#region 鼠标移动窗口
// 位置 - 用于记录鼠标位置
Point downPoint;
// 当鼠标按下时,记录鼠标位置
private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
//Point FrmP = new Point(button1.Left, button1.Top);
////ScreenP返回相对屏幕的坐标 Point ScreenP=this.PointToScreen(FrmP);
downPoint = new Point(e.X, e.Y);
}
// 当鼠标移动是,改变窗体位置
private void MainForm_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
Location = new Point(Location.X + e.X - downPoint.X, Location.Y + e.Y - downPoint.Y);
}
#endregion
【方法二】在窗体控件上进行
#region //鼠标坐标
private int xpos;
private int ypos;
#region panel1在表面,鼠标移动窗体
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
xpos = e.X;
ypos = e.Y;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Left += e.X - this.xpos;
this.Top += e.Y - this.ypos;
}
}
#endregion
【方法三】转载于博客园<小锋刚>
窗体的移动:
bool formMove = false;//窗体是否移动
Point formPoint;//记录窗体的位置
private void Form4_MouseDown(object sender, MouseEventArgs e)//鼠标按下
{
formPoint = new Point();
int xOffset;
int yOffset;
if (e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height;
formPoint = new Point(xOffset, yOffset);
formMove = true;//开始移动
}
}
private void Form4_MouseMove(object sender, MouseEventArgs e)//鼠标移动
{
if (formMove == true)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(formPoint.X, formPoint.Y);
Location = mousePos;
}
}
private void Form4_MouseUp(object sender, MouseEventArgs e)//鼠标松开
{
if (e.Button == MouseButtons.Left)//按下的是鼠标左键
{
formMove = false;//停止移动
}
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Left += e.X - this.xpos;
this.Top += e.Y - this.ypos;
}
}
#endregion
【案例】
拖动无边框窗体Form至桌面任何位置
首先建一个Windows应用程序
将Form1的 FormBorderStyle属性设置为None
Point mouseOff;//鼠标移动位置变量
bool leftFlag;//标签是否为左键
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseOff = new Point(-e.X, -e.Y); //得到变量的值
leftFlag = true; //点击左键按下时标注为true;
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (leftFlag)
{
Point mouseSet = Control.MousePosition;
mouseSet.Offset(mouseOff.X, mouseOff.Y); //设置移动后的位置
Location = mouseSet;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (leftFlag)
{
leftFlag = false;//释放鼠标后标注为false;
}
}
相关文章推荐
- C#WinForm无边框窗体移动方法、模仿鼠标单击标题栏移动窗体位置
- QT去掉边框后窗口拖放控制大小及移动窗口
- 鼠标移动没有标题栏的winform窗口
- FormBorderStyle设为None,移动Winform窗口的两种方法
- Winform窗口去边框实现可以移动
- winform 在mdi窗口重绘界面时,mdi挡住主窗体,导致鼠标拖动缩放窗体大小失效的解决方法随笔
- C#实现Winform无边框移动的方法
- winForm 实现鼠标拖动标题栏不移动窗口 和 拖动用户区移动窗口
- winform实现限制及解除鼠标移动范围的方法
- 关于Winform中类似股票K线图十字交叉线随着鼠标移动的实现方法!
- Qt无边框窗口的移动、拉伸边框、鼠标滚轮缩放大小
- Winform窗口去边框实现可以移动
- Winfrom窗体在去掉窗体边框的情况下使鼠标移动窗体
- winform 中用IrisSkin2 皮肤后,鼠标在窗体移动会有闪烁,GroupBox不停的闪烁 解决方法
- JS实现窗口加载时模拟鼠标移动的方法
- win32 (MFC) 无边框标题栏移动窗口方法
- FormBorderStyle设为None,移动Winform窗口的两种方法
- FormBorderStyle设为None,移动Winform窗口的两种方法
- JS实现窗口加载时模拟鼠标移动的方法
- 鼠标移动没有标题栏的winform窗口