首页 > 代码库 > Winform 去掉 最大化 最小化 关闭按钮(不是关闭按钮变灰)终极解决办法

Winform 去掉 最大化 最小化 关闭按钮(不是关闭按钮变灰)终极解决办法

不墨迹, 如图 :

技术分享

网上 看了,好多 给的 答案 乱码七糟,都是扯淡,于是乎 自己 写,代码如下:
窗体的大小暂时设置为:(598, 362) 涂红的数据根据你的窗体大小改动

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;

namespace WinDemo
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Button 按钮重绘事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Paint(object sender, PaintEventArgs e)
        {
            GraphicsPath myPath = new GraphicsPath();
            Rectangle rect = new Rectangle(0,0,574,362);//后面2个数据调整窗体大小
            myPath.AddRectangle(rect);
            this.Region = new Region(myPath);
        }

        [DllImport("user32.dll")]
        static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

        [DllImport("User32.dll")]

        private static extern IntPtr GetWindowDC(IntPtr hWnd);

        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            const int WM_NCPAINT = 0x85;
            base.WndProc(ref m);

            if (m.Msg == WM_NCPAINT)
            {

                IntPtr hdc = GetWindowDC(m.HWnd);
                if ((int)hdc != 0)
                {
                    Graphics g = Graphics.FromHdc(hdc);
                    Pen pen1 = new Pen(Color.FromArgb(64,64,64));
                    Pen pen2 = new Pen(Color.FromArgb(128, 128, 128));
                    Pen pen3 = new Pen(Color.FromArgb(212, 208, 200));
                    g.DrawLine(pen1, 573, 0, 573, 360);//最外边
                    g.DrawLine(pen2, 572, 1, 572, 359);//最外边第二条白色
                    g.DrawLine(pen3, 571, 2, 571, 359);
                    g.DrawLine(pen3, 571, 2, 571, 359);
                    g.Flush();
                    ReleaseDC(m.HWnd, hdc);
                }
            }
        }
        private void Form5_MouseCaptureChanged(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen pen1 = new Pen(Color.FromArgb(64, 64, 64));
            Pen pen2 = new Pen(Color.FromArgb(128, 128, 128));
            Pen pen3 = new Pen(Color.FromArgb(212, 208, 200));
            g.DrawLine(pen1, 573, 0, 573, 360);//最外边
            g.DrawLine(pen2, 572, 1, 572, 359);//最外边第二条白色
            g.DrawLine(pen3, 571, 2, 571, 359);
            g.DrawLine(pen3, 571, 2, 571, 359);
            g.Flush();
        }
    }
}

 

原文连接:http://zydcomputer.blog.163.com/blog/static/12427188120121102021528/

Winform 去掉 最大化 最小化 关闭按钮(不是关闭按钮变灰)终极解决办法