在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
private Point mouseOffset; //记录鼠标指针的坐标
创建该窗体 MouseDown事件的相应处理程序,注示部分为坐标的补偿private bool isMouseDown = false; //记录鼠标按键是否按下
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
创建该窗体的 MouseMove事件的相应处理程序{ 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; } }
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
创建该窗体的MouseUp事件的相应处理程序{ if (isMouseDown) { Point mousePos = Control.MousePosition; mousePos.Offset(mouseOffset.X, mouseOffset.Y); Location = mousePos; } }
private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{ // 修改鼠标状态isMouseDown的值 // 确保只有鼠标左键按下并移动时,才移动窗体 if (e.Button == MouseButtons.Left) { isMouseDown = false; } } |
请发表评论