C# Winform自定义标题栏:拖动标题栏窗口随之移动

我自定义的标题栏如下(蓝色部分,是一个Panel控件):

实现拖动标题栏窗口随之移动的方法——在Form.cs中加入:

public BaseForm_hasBar_large()
        {
            InitializeComponent();
            this.TopBar.MouseDown += TopBar_MouseDown;
            this.TopBar.MouseMove += TopBar_MouseMove;
        }

#region 点击panel控件移动窗口
        private Point downPoint;
        private void TopBar_MouseDown(object sender, MouseEventArgs e)
        {
            //MessageBox.Show("Left");
            downPoint = new Point(e.X, e.Y);
        }
        private void TopBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - downPoint.X,
                    this.Location.Y + e.Y - downPoint.Y);
            }
        }
        #endregion

 

posted on 2019-09-18 13:27  PER10  阅读(1460)  评论(0编辑  收藏  举报

导航