C#实现无边框winfrom界面

winfrom界面其实也可以扁平化,让外观提升一个level,整体为360风格。先上图:
在这里插入图片描述

首先在界面属性中,将FormBorderStyle设置为None,然后界面变为正常的Panel。此时需要添加顶栏拖动、最小化和关闭按钮。


        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        #region 窗体操作

        /// <summary>
        /// 窗体拖动
        /// </summary>
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

        /// <summary>
        /// 绘制边框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bodyPanel_Paint(object sender, PaintEventArgs e)
        {
            Pen p = new Pen(Color.FromArgb(109, 155, 241));
            e.Graphics.DrawRectangle(p, 0, 0, this.bodyPanel.Width - 1, this.bodyPanel.Height - 1);
        }

        /// <summary>
        /// 最小化窗体
        /// </summary>
        private void minPictureBox_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        /// <summary>
        /// 关闭窗体
        /// </summary>
        private void closePictureBox_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void minPictureBox_MouseEnter(object sender, EventArgs e)
        {
            this.minPictureBox.Image = global::GSOperateMgrTool.Properties.Resources.最小化鼠标经过;
        }
        private void minPictureBox_MouseLeave(object sender, EventArgs e)
        {
            this.minPictureBox.Image = global::GSOperateMgrTool.Properties.Resources.最小化;
        }
        private void closePictureBox_MouseEnter(object sender, EventArgs e)
        {
            this.closePictureBox.Image = global::GSOperateMgrTool.Properties.Resources.关闭鼠标经过;
        }
        private void closePictureBox_MouseLeave(object sender, EventArgs e)
        {
            this.closePictureBox.Image = global::GSOperateMgrTool.Properties.Resources.关闭;
        }
        /// <summary>
        ///设置最小化和关闭图片tip提示
        /// </summary>
        private void SetToolTip()
        {
            //定义ToolTip对象
            ToolTip toolTip = new ToolTip();
            toolTip.AutoPopDelay = 5000;
            toolTip.InitialDelay = 100;
            toolTip.ReshowDelay = 500;
            toolTip.ShowAlways = true;
            toolTip.SetToolTip(this.minPictureBox,"最小化");
            toolTip.SetToolTip(this.closePictureBox, "关闭");
        }

猜你喜欢

转载自blog.csdn.net/u014715882/article/details/83215003