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

猜你喜欢

转载自www.cnblogs.com/PER10/p/11541547.html