添加工具条拖动功能
This commit is contained in:
@@ -49,6 +49,10 @@ namespace JoyD.Windows.CS
|
|||||||
private bool _isMoving = false;
|
private bool _isMoving = false;
|
||||||
private Point _startMovePoint;
|
private Point _startMovePoint;
|
||||||
|
|
||||||
|
// 工具条拖动相关变量
|
||||||
|
private ToolStrip _draggedToolStrip = null;
|
||||||
|
private Point _dragStartPoint;
|
||||||
|
|
||||||
public Setting()
|
public Setting()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
@@ -74,6 +78,59 @@ namespace JoyD.Windows.CS
|
|||||||
{
|
{
|
||||||
Console.WriteLine("删除按钮初始化失败: " + ex.Message);
|
Console.WriteLine("删除按钮初始化失败: " + ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 为工具条添加拖动事件
|
||||||
|
toolStrip.MouseDown += ToolStrip_MouseDown;
|
||||||
|
tempDiffToolStrip.MouseDown += ToolStrip_MouseDown;
|
||||||
|
toolStripContainer.ContentPanel.MouseMove += ContentPanel_MouseMove;
|
||||||
|
toolStripContainer.ContentPanel.MouseUp += ContentPanel_MouseUp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 工具条鼠标按下事件 - 开始拖动
|
||||||
|
/// </summary>
|
||||||
|
private void ToolStrip_MouseDown(object sender, MouseEventArgs e)
|
||||||
|
{
|
||||||
|
// 确保点击的是工具条的空白区域或标题栏
|
||||||
|
if (e.Button == MouseButtons.Left && (e.X < toolStrip.PreferredSize.Width && e.Y < 25))
|
||||||
|
{
|
||||||
|
_draggedToolStrip = sender as ToolStrip;
|
||||||
|
_dragStartPoint = e.Location;
|
||||||
|
_draggedToolStrip.Cursor = Cursors.SizeAll;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 内容面板鼠标移动事件 - 处理拖动
|
||||||
|
/// </summary>
|
||||||
|
private void ContentPanel_MouseMove(object sender, MouseEventArgs e)
|
||||||
|
{
|
||||||
|
if (_draggedToolStrip != null)
|
||||||
|
{
|
||||||
|
// 计算新位置
|
||||||
|
Point newLocation = toolStripContainer.ContentPanel.PointToClient(_draggedToolStrip.PointToScreen(e.Location));
|
||||||
|
newLocation.X -= _dragStartPoint.X;
|
||||||
|
newLocation.Y -= _dragStartPoint.Y;
|
||||||
|
|
||||||
|
// 确保位置在有效范围内
|
||||||
|
newLocation.X = Math.Max(0, Math.Min(newLocation.X, toolStripContainer.ContentPanel.Width - _draggedToolStrip.Width));
|
||||||
|
newLocation.Y = Math.Max(0, Math.Min(newLocation.Y, toolStripContainer.ContentPanel.Height - _draggedToolStrip.Height));
|
||||||
|
|
||||||
|
// 设置新位置
|
||||||
|
_draggedToolStrip.Location = newLocation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 内容面板鼠标释放事件 - 结束拖动
|
||||||
|
/// </summary>
|
||||||
|
private void ContentPanel_MouseUp(object sender, MouseEventArgs e)
|
||||||
|
{
|
||||||
|
if (_draggedToolStrip != null)
|
||||||
|
{
|
||||||
|
_draggedToolStrip.Cursor = Cursors.Default;
|
||||||
|
_draggedToolStrip = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user