无标题窗口移动及缩放

public partial class FrmResizeDrapTemplate : Form
    {
        public FrmResizeDrapTemplate()
        {
            InitializeComponent();
        }

        private const int WM_NCHITTEST = 0x84;
        private const int HTCLIENT = 0x1;
        private const int HTCAPTION = 0x2; 
 
        protected override void WndProc(ref Message m)
        {           
            base.WndProc(ref m);
            #region Move without Form Border
            if (m.Msg == WM_NCHITTEST)
            {
                this.DefWndProc(ref m);
                if (m.Result.ToInt32() == HTCLIENT)
                    m.Result = new IntPtr(HTCAPTION);
            }
            #endregion               
            #region Resize without Form Border                           
            if (m.Msg == WM_NCHITTEST && this.FormBorderStyle == FormBorderStyle.None)             
            {                 
                Point z = Control.MousePosition;                 
                if (z != null)                 
                {                     
                    Point tmp = this.PointToScreen(new Point());                     
                    z.X -= tmp.X;                     
                    z.Y -= tmp.Y;                 
                }                   
                Padding resizePadding = new Padding(3);                 
                int result = 0;                 
                if (z.X  < (resizePadding.Left)) result = 10;                 
                else if (z.X > (Width - resizePadding.Right)) result = 11;                   
                if (z.Y  < (resizePadding.Top))                 
                {                     
                    switch (result)                     
                    {                         
                        case 0:                             
                            result = 12;                             
                            break;                         
                        case 10:                             
                            result = 13;                             
                            break;                         
                        case 11:                             
                            result = 14;                             
                            break;                         
                        default:                             
                            break;                     
                    }                 
                }                 
                else if (z.Y  < (20))                 
                {                     
                    switch (result)                     
                    {                         
                        case 0:                             
                            result = 2;                             
                            break;                         
                        default:                             
                            break;                     
                    }                 
                }                 
                else if (z.Y > (Height - resizePadding.Bottom))                 
                {                     
                    switch (result)                     
                    {                         
                        case 0:                             
                            result = 15;                            
                            break;                         
                        case 10:                             
                            result = 16;                            
                            break;                         
                        case 11:                             
                            result = 17;                             
                            break;                         
                        default:                             
                            break;                     
                    }                 
                }
                  
                if (result != 0) m.Result = (IntPtr)result;             
            }               
            #endregion
        }
    }

猜你喜欢

转载自blog.csdn.net/pepsisoft/article/details/5750633