当前位置:编程学习 > C#/ASP.NET >>

鼠标点击按钮拖动窗体

 

public partial class Form1 : Form { 

//记录窗体的X坐标 

private int startX; 

//记录窗体的Y坐标 

private int startY; 

 

  public Form1() { 

    InitializeComponent(); 

    } 

    /// <summary> 

    /// 鼠标点击左键 

    /// </summary> 

    /// <param name="sender"></param> 

    /// <param name="e"></param> 

    private void button1_MouseDown(object sender, MouseEventArgs e) { 

        //判断点击的是不是左键 

        if(e.Button == MouseButtons.Left) { 

            //得到窗体的X值 

            startX = e.X; 

            //得到窗体的Y值 

            startY = e.Y; 

        } 

    } 

    /// <summary> 

    /// 鼠标移动 

    /// </summary> 

    /// <param name="sender"></param> 

    /// <param name="e"></param> 

    private void button1_MouseMove(object sender, MouseEventArgs e) { 

        //判断点击的是不是左键 

        if(e.Button == MouseButtons.Left) { 

            //重新绘制窗体X 

            this.Left += e.X - startX; 

            //重新绘制窗体Y 

            this.Top += e.Y - startY; 

        } 

    } 

}   

 

摘自 :幸福的猪的专栏

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,