110,567
社区成员
发帖
与我相关
我的任务
分享
private bool btnBool = false;
private Point btnStart;
private Point btnEnd;
/// <summary>
/// 按钮移动事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_MouseMove(object sender, MouseEventArgs e)
{
if (btnBool)
{
//移动后的坐标
btnEnd = Cursor.Position;
//计算坐标值
GetButtonLoction();
//移动完按钮后把现在光标的坐标付给开始坐标
btnStart = Cursor.Position;
}
}
Point mouse_offset;
/// <summary>
/// 点击按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_MouseDown(object sender, MouseEventArgs e)
{
Button bt = sender as Button;
bt.FlatAppearance.BorderSize = 1;
btnBool = true;
//开始坐标
btnStart = Cursor.Position;
CtrlForm.Cursor = Cursors.SizeAll;
}
/// <summary>
/// 单击释放按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_MouseUp(object sender, MouseEventArgs e)
{
btnBool = false;
//光标释放坐标
btnEnd = Cursor.Position;
CtrlForm.Cursor = Cursors.Default;
}
/// <summary>
/// 改变按钮坐标位置
/// </summary>
private void GetButtonLoction()
{
//坐标差值
Point p = new Point(btnEnd.X - btnStart.X, btnEnd.Y - btnStart.Y);
for (int i = 0; i < listButton.Count; i++)
{
Button bt = listButton[i];
if (bt.FlatAppearance.BorderSize > 0)
{
bt.Location = new Point(bt.Location.X + p.X, bt.Location.Y + p.Y);
bt.Refresh();
}
}
}