C# 如何在空间运行时调整控件位置和大小
最近需要实现这个功能,结合网上搜索代码,实现了该功能。
代码如下:
代码
private void L*****_MouseMove(object sender, MouseEventArgs e)
{
//((Control)sender).Cursor = Cursors.Hand;
//bChangeSizeMode = false;
if (!bMouseDown)
{
int right = ((Control)sender).Width;
int bottom = ((Control)sender).Height;
if (e.X > (right - 10) && e.Y > (bottom - 10))
{
((Control)sender).Cursor = Cursors.SizeNWSE;
bChangeSizeMode = true;
}
else if (e.X > (right - 10))
{
((Control)sender).Cursor = Cursors.VSplit;
bChangeSizeMode = true;
}
else if (e.Y > (bottom - 10))
{
((Control)sender).Cursor = Cursors.HSplit;
bChangeSizeMode = true;
}
else
{
((Control)sender).Cursor = Cursors.SizeAll;
bChangeSizeMode = false;
}
}
if (bMouseDown && bMoveMode)
{
if (bChangeSizeMode)
{
if (e.Button == MouseButtons.Left)
{
if (((Control)sender).Cursor == Cursors.VSplit)
{
((Control)sender).Width = e.X;
}
else if (((Control)sender).Cursor == Cursors.HSplit)
{
((Control)sender).Height = e.Y;
}
else
{
((Control)sender).Width = e.X;
((Control)sender).Height = e.Y;
}
((Control)sender).Tag = ((Control)sender).Width + ":" + ((Control)sender).Height + ":" + ((Control)sender).Left + ":" + ((Control)sender).Top + ":" + ((Control)sender).Font.Size;
}
bLocationChanged = true;
}
else
{
//((Control)sender).Cursor = Cursors.Hand;//设置拖动时鼠标箭头
if (e.Button == MouseButtons.Left)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseOffset.X, mouseOffset.Y);//设置偏移
((Control)sender).Location = ((Control)sender).Parent.PointToClient(mousePos);
((Control)sender).Tag = ((Control)sender).Width + ":" + ((Control)sender).Height + ":" + ((Control)sender).Left + ":" + ((Control)sender).Top + ":" + ((Control)sender).Font.Size;
}
bLocationChanged = true;
}
}
}