C # Winform removed form gray border and drag the border to achieve form to change the size

1) remove the gray border form:

In the design window, set the Form FormBorderStyle for FixedSingle. But after setting window can not be achieved drag the border to change the size. Need to add the following code in Form.cs:

Private  const  int WM_NCLBUTTONDBLCLK = 163 ;
         Private  const  int the WM_NCHITTEST = 132 ;
         /// <Summary> ///  frame resize
         /// </ Summary> /// <param name = "m"> </ param> protected the override void the WndProc ( REF the message m) 
        { Switch (m.Msg) 
            { Case WM_NCLBUTTONDBLCLK: // WM_NCLBUTTONDBLCLK = 163 <0xA3> knockdown non-client area mouse left click message, determine whether the display form is maximized iF ( the this .MaximizeBox) 
         
         
          
            
                
                     
                    {
                         Base .WndProc ( REF m);
                         the this .Invalidate (); 
                    } 
                    return ;
                 Case the WM_NCHITTEST: // the WM_NCHITTEST = 132 <0x84>  
                    Base .WndProc ( REF m); // if this line is removed, the form will lose MouseMove .. other events
                     // Point LPINT new new Point = ((int) m.LParam); // get the mouse coordinates, so that you can decide how to deal with this news, mobile form, or scaling, as well as to which direction scaling
                     // IF (lpint.Y <30)
                     //     m.Result = (IntPtr) 0x2; // torr = 2 movable HTCAPTION <0x2> 
                    IF(! = The WindowState FormWindowState.Maximized) 
                    { 
                        Point P2 = the this .PointToClient (MousePosition); // mouse coordinates relative to the form
                         // course be movable holder may also be varied the size
                         @ Label1.Text + = P2.x " , "+ p2.Y;
                         // HTLEFT = 10 <0xA> left border 
                        IF (P2.x < . 5 && p2.Y> . 5 && p2.Y < the this .Height - . 5 ) 
                            m.Result = (IntPtr) 0xA ;
                         the else  IF (p2.Y < 5 && p2.X > 5 && p2.X < this.Width - 5)
                            m.Result = (IntPtr)0xC;
                        //HTTOP=12 <0xC> 上边框                   
                        else if (p2.X < 5 && p2.Y < 5)
                            m.Result = (IntPtr)0xD;
                        //HTTOPLEFT=13 <0xD>
                        else if (p2.X >= this.Width - 5 && p2.Y < 5)
                            m.Result = (IntPtr)0xE;
                        //HTTOPRIGHT=14 <0xE> 
                        else if (p2.X > this.Width - 5 && p2.Y > 5 && p2.Y < this.Height - 5)
                            m.Result = (IntPtr)0xB;
                        //HTRIGHT=11 <0xB>
                        else if (p2.Y >= this.Height - 5 && p2.X > 5 && p2.X < this.Width - 5)
                            m.Result = (IntPtr)0xF;
                        //HTBOTTOM=15 <0xF>
                        else if (p2.X < 5 && p2.Y >= this.Height - 5)
                            m.Result = (IntPtr)0x10;
                        //HTBOTTOMLEFT=16 <0x10>
                        else if (p2.X > this.Width - 5 && p2.Y >= this.Height - 5)
                            m.Result = (IntPtr)0x11;
                        //HTBOTTOMRIGHT=17 <0x11>

                        //= 18 is HTBORDER <0x12> 

                        //HTMINBUTTON = 8 <0x8> minimize button 

                        // HTMAXBUTTON. 9 = <0x9> maximize button
                         // HTCLOSE = 20 is <0x14> Close button 
                    }
                     return ;
                 default :
                     Base .WndProc ( REF m);
                     return ; 
            } 
        }

 

Guess you like

Origin www.cnblogs.com/PER10/p/11541497.html