无标题栏Winform实现标题栏功能(四个角、四个边拖拽改变大小等)

public partial class Form1 : Form
    {
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
        [DllImport("User32.dll", EntryPoint = "PostQuitMessage")]
        private static extern IntPtr PostQuitMessage(int msg);
        [DllImport("User32.dll", EntryPoint = "PostMessage")]
        private static extern IntPtr PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
        const int Guying_HTLEFT = 10;
        const int Guying_HTRIGHT = 11;
        const int Guying_HTTOP = 12;
        const int Guying_HTTOPLEFT = 13;
        const int Guying_HTTOPRIGHT = 14;
        const int Guying_HTBOTTOM = 15;
        const int Guying_HTBOTTOMLEFT = 0x10;
        const int Guying_HTBOTTOMRIGHT = 17;
        bool isMouseDown = false;
        public Form1()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
            this.Paint += Form1_Paint;
            this.SizeChanged += Form1_SizeChanged;
            this.MouseMove += Form1_MouseMove;
            this.MouseDown += Form1_MouseDown;
            this.MouseUp += Form1_MouseUp;
            this.Click += Form1_Click;
            this.buttonClose.Click += Button1_Click;
            this.FormClosed += Form1_FormClosed;
            buttonMini.Click += ButtonMini_Click;
            buttonMax.Click += ButtonMax_Click;
            //this.Opacity = 0.999;
            this.DoubleClick += Form1_DoubleClick;
            this.Load += Form1_Load;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
           
        }
        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            //画红色边框
            using (var g = this.CreateGraphics())
            {
                g.FillRectangle(new SolidBrush(this.BackColor), 0, 0, this.Width, this.Height);
                g.DrawRectangle(Pens.Red, 0, 0, this.Width - 1, this.Height - 1);
            }
        }
        private void Form1_DoubleClick(object sender, EventArgs e)
        {
            MessageBox.Show("Form1_DoubleClick");
        }
        private void ButtonMax_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                PostMessage(this.Handle, 0x0112, new IntPtr(0xF120), new IntPtr(0));
                //this.WindowState = FormWindowState.Normal;
            }
            else
            {
                PostMessage(this.Handle, 0x0112, new IntPtr(0xf030), new IntPtr(0));
                this.MaximumSize = Screen.FromHandle(this.Handle).WorkingArea.Size;
                //this.WindowState = FormWindowState.Maximized;
            }
        }
        private void ButtonMini_Click(object sender, EventArgs e)
        {
            //this.WindowState = FormWindowState.Minimized;
           
            PostMessage(this.Handle, 0x0112, new IntPtr(0xF020),new IntPtr(0));
        }
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            MessageBox.Show("Form1_FormClosed事件");
        }
        private void Button1_Click(object sender, EventArgs e)
        {
            PostQuitMessage(0);
            //this.Close();
        }
        private void Form1_Click(object sender, EventArgs e)
        {
            //Control.MousePosition
        }
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
            label1.Text = "MouseUp";
            //using (var g = this.CreateGraphics())
            //{
            //    //g.FillRectangle(new SolidBrush(this.BackColor), 0, 0, this.Width, this.Height);
            //    g.DrawRectangle(Pens.Red, 0, 0, this.Width - 1, this.Height - 1);
            //}
        }
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            isMouseDown = true;
            label1.Text = "MouseDown";
        }
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if(e.Location.X<30 && e.Location.Y<30)
            {
                if(isMouseDown)
                {
                }
            }
        }
        long i = 0;
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            using (var g = e.Graphics)
            {
               
                g.DrawRectangle(Pens.Red, 0, 0, this.Width - 1, this.Height - 1);

            }
        }
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                //case 0x00A2:
                //    {
                //        base.WndProc(ref m);
                //        using (var g = this.CreateGraphics())
                //        {
                //            g.FillRectangle(new SolidBrush(this.BackColor), 0, 0, this.Width - 1, this.Height - 1);
                //            g.DrawRectangle(Pens.Red, 0, 0, this.Width - 1, this.Height - 1);
                //        }
                //    }
                //    break;
                //case 0x00A1:
                //    {
                //    }
                //    break;
                //case 0x0112:
                //    {
                //        if (m.WParam == (IntPtr)0xF020)
                //        {
                //            m.Result = new IntPtr(1);
                //        }
                //        base.WndProc(ref m);
                //        break;
                //    }
                case 0x00A3://WM_NCLBUTTONDBLCLK
                    {
                        //m.Result = (IntPtr)2;
                        //m.LParam = new IntPtr(0x0A0A); //默认值  
                        //m.WParam = new IntPtr(20);
                        //base.WndProc(ref m);
                        //if(this.Size.Height> System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height)
                        //{
                        //    this.MaximumSize = Screen.FromHandle(this.Handle).WorkingArea.Size;
                        //}
                        Form1_DoubleClick(this, null);
                        ButtonMax_Click(this, null);
                       
                    }
                    break;
                case 0x0084://WM_NCHITTEST
                    base.WndProc(ref m);
                    Point vPoint = new Point((int)m.LParam & 0xFFFF,
                        (int)m.LParam >> 16 & 0xFFFF);
                    vPoint = PointToClient(vPoint);
                    //按下鼠标左键时拖动窗体
                    if (vPoint.X > 5 && vPoint.X < this.Width - 5 && vPoint.Y > 5 && vPoint.Y < this.Height-5)
                    {
                        m.Result = (IntPtr)2;
                        //Form1_MouseUp(this, null);
                    }
                    //按下鼠标左键时放大缩小窗体
                    else
                    {
                        if (vPoint.X <= 5)
                            if (vPoint.Y <= 5)
                                m.Result = (IntPtr)Guying_HTTOPLEFT;
                            else if (vPoint.Y >= ClientSize.Height - 5)
                                m.Result = (IntPtr)Guying_HTBOTTOMLEFT;
                            else
                            {
                                m.Result = (IntPtr)Guying_HTLEFT;
                                //using (var g = this.CreateGraphics())
                                //{
                                //    g.DrawRectangle(Pens.Red, 0, 0, this.Width - 1, this.Height - 1);
                                //}
                            }
                        else if (vPoint.X >= ClientSize.Width - 5)
                            if (vPoint.Y <= 5)
                                m.Result = (IntPtr)Guying_HTTOPRIGHT;
                            else if (vPoint.Y >= ClientSize.Height - 5)
                                m.Result = (IntPtr)Guying_HTBOTTOMRIGHT;
                            else
                            {
                                m.Result = (IntPtr)Guying_HTRIGHT;
                                //SendMessage(this.Handle, 0x00A2, IntPtr.Zero, IntPtr.Zero);
                               
                            }
                        else if (vPoint.Y <= 5)
                            m.Result = (IntPtr)Guying_HTTOP;
                        else if (vPoint.Y >= ClientSize.Height - 5)
                            m.Result = (IntPtr)Guying_HTBOTTOM;
                    }
                    break;
                //case 0x0201:                //鼠标左键按下的消息  
                //    //m.Msg = 0x00A1;         //更改消息为非客户区按下鼠标  
                //    //m.LParam = IntPtr.Zero; //默认值  
                //    //m.WParam = new IntPtr(2);//鼠标放在标题栏内  
                //    base.WndProc(ref m);
                //    Form1_MouseDown(this, null);
                //    break;
                //case 0x0202:
                //    base.WndProc(ref m);
                //    Form1_MouseUp(this, null);
                //    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }
    }
posted @ 2020-11-12 15:04  carrot_hlb  阅读(74)  评论(0编辑  收藏