EyesBaby功能实现之窗口拖拽与缩放功能
Winform中窗体拖拽主要由鼠标移动事件(MouseMove)、鼠标按下事件(MouseDown)和鼠标释放事件(MouseUp)完成。
以下详细代码:
private Point mouseOffset; //记录鼠标指针的坐标
/// <summary>
/// 鼠标移动事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseOffset.X, mouseOffset.Y);
// 设置窗口位置等于鼠标移动位置
Location = mousePos;
}
}
/// <summary>
/// 鼠标按下事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
int xOffset;
int yOffset;
if (e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight -
SystemInformation.FrameBorderSize.Height;
mouseOffset = new Point(xOffset, yOffset);
isMouseDown = true;
}
}
/// <summary>
/// 鼠标释放事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
// 修改鼠标状态isMouseDown的值
// 确保只有鼠标左键按下并移动时,才移动窗体
if (e.Button == MouseButtons.Left)
{
isMouseDown = false;
}
}
/// <summary>
/// 鼠标移动事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseOffset.X, mouseOffset.Y);
// 设置窗口位置等于鼠标移动位置
Location = mousePos;
}
}
/// <summary>
/// 鼠标按下事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
int xOffset;
int yOffset;
if (e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight -
SystemInformation.FrameBorderSize.Height;
mouseOffset = new Point(xOffset, yOffset);
isMouseDown = true;
}
}
/// <summary>
/// 鼠标释放事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
// 修改鼠标状态isMouseDown的值
// 确保只有鼠标左键按下并移动时,才移动窗体
if (e.Button == MouseButtons.Left)
{
isMouseDown = false;
}
}
鼠标滚动轴控制窗口大小:
需要实现鼠标滑动滚动轴事件(MouseWheel)。事件参数MouseEventArgs.Delta值分别是120和-120.
代码如下:
/// <summary>
/// 鼠标滑动滚动轴事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Form1_MouseWheel(object sender, MouseEventArgs e)
{
Size t = pictureBox1.Size;
t.Width += e.Delta;
t.Height += e.Delta;
// 控制窗口宽度大于100px
if (e.Delta==120||(this.Size.Height > 100 && this.Size.Width > 200))
this.Size = t;
// 消息提示居中
lblMsg.Location = new Point((this.Width - lblMsg.Width) / 2, (this.Height - lblMsg.Height) / 2);
}
实际上EyesBaby中是通过图片控件来控制窗体的大小和拖动位置。
源代码下载地址:http://eyesbaby.codeplex.com/
安装版下载地址:https://files.cnblogs.com/yizhuqing/EyesBabySetup10.zip
作者:木子清
联系方式:微信 billy_yi QQ: 909501683 多年.NET平台开发经验,擅长物联网各类设备(地磅、轨道秤、RFID等)集成开发。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。