我正在创建一个标签控件,允许从标签的顶部和底部调整大小。它也只在移动时垂直滑动。我为它的运动和可调整大小的标签的底部创建了边界,但是我很难弄清楚我在顶部做错了什么。只要将鼠标从顶部拖出边界之外,它就会触发450像素的预设长度。我正在使用.SetBounds和Math.Min。
using System;
using System.Drawing;
using System.Windows.Forms;
namespace EventTest
{
public partial class Form1 : Form
{
Point label1_MousePoint_01 = new Point();
Point label1_Start_01 = new Point();
Point label1_End_01 = new Point();
int label1_MouseSwitch_01;
public Form1()
{
InitializeComponent();
label1.MouseDown += Label1_MouseDown;
label1.MouseUp += Label1_MouseUp;
label1.MouseMove += Label1_MouseMove;
}
// Mouse Move Event
private void Label1_MouseMove(object sender, MouseEventArgs e)
{
if ((e.X < label1.Width && e.X > 0 && e.Y > 0 && e.Y < 4) || (e.X < label1.Width && e.X > 0 && e.Y > label1.Height - 4 && e.Y < label1.Height))
{
Cursor.Current = Cursors.SizeNS;
}
// Mouse Event Top Move
if (label1_MouseSwitch_01 == 1)
{
label1.SetBounds(0, Math.Max(e.Y + label1.Location.Y - label1_MousePoint_01.Y, 40), 120, Math.Min(label1.Height - e.Y + label1_MousePoint_01.Y, 450));
}
// Mouse Event Bottom Move
else if (label1_MouseSwitch_01 == 2)
{
label1.Height = Math.Min(660 - label1.Location.Y, e.Location.Y);
}
// Mouse Event Grab and Move
else if (label1_MouseSwitch_01 == 3)
{
int ny = Math.Min(Math.Max((label1.Location.Y - label1_MousePoint_01.Y + e.Y), 40), 660 - label1.Height);
label1.Location = new Point(0, ny);
}
}
// Mouse Up Event
private void Label1_MouseUp(object sender, MouseEventArgs e)
{
label1_MouseSwitch_01 = 0;
if (label1.Height < 20)
{
label1.Height = 20;
}
}
// Mouse Down Event
private void Label1_MouseDown(object sender, MouseEventArgs e)
{
label1_Start_01.Y = label1.Height;
label1_End_01.Y = label1.Top;
// Set Mouse Switch
if (e.Button == MouseButtons.Left)
{
label1_MousePoint_01 = e.Location;
if (e.Y > 0 && e.Y < 4)
{
Cursor.Current = Cursors.SizeNS;
label1_MouseSwitch_01 = 1;
}
else if (e.Y > label1.Height - 4 && e.Y < label1.Height)
{
Cursor.Current = Cursors.SizeNS;
label1_MouseSwitch_01 = 2;
}
else if (e.Y > 4 && e.Y < label1.Height - 4)
{
label1_MouseSwitch_01 = 3;
}
}
}
// Snap To Every 20 Pixels
public int RoundEr(int i)
{
return ((int)Math.Round(i / 20.0)) * 20;
}
}
}