当前位置:   article > 正文

Winform窗口去边框实现可以移动_winform窗体边框取消后拖拽

winform窗体边框取消后拖拽

Windows自带的边框很影响我们个性化定制窗口UI的美观程序。
首选选择窗口,属性里的FormBorderStyle设置为None。
边框去掉之后 会发现窗口无法最大最小化和移动。
1 在标题栏处新建三个panel,分别为标题栏,最大最小化按钮和关闭按钮Pannel

将后面几个panel设置BackgroundImage为相应图标
分别为panel_title panel_min panel_close
为panel_title添加MouseDown和MouseMove事件代码为

private Point mousePoint = new Point();
private void panel_title_MouseDown(object sender, MouseEventArgs e)
        {
            base.OnMouseDown(e);
            this.mousePoint.X = e.X;
            this.mousePoint.Y = e.Y;
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
private void panel_title_MouseMove(object sender, MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (e.Button == MouseButtons.Left)
            {
                this.Top = Control.MousePosition.Y - mousePoint.Y;
                this.Left = Control.MousePosition.X - mousePoint.X;
            }
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

为panel_min添加click事件

 this.WindowState = FormWindowState.Minimized;
  • 1

为panel_close添加close事件

private void panel4_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("是否退出?", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                //this.Dispose();
                Application.Exit();
            }
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

这样,窗口就可以拖动移动了

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/480902?site
推荐阅读
相关标签
  

闽ICP备14008679号