无边框窗体的拖动和拉伸

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.Runtime.InteropServices;
 10 
 11 namespace WindowsFormsApplication2
 12 {
 13     public partial class Form1 : Form
 14     {
 15         public Form1()
 16         {
 17             InitializeComponent();
 18             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
 19         }
 20 
 21         protected override void OnMouseDown(MouseEventArgs e)
 22         {
 23             base.OnMouseDown(e);
 24             if (e.Button == MouseButtons.Left &&
 25                 this.WindowState != FormWindowState.Maximized)
 26             {
 27                 ReleaseCapture();
 28                 SendMessage(this.Handle, 274, 61440 + 9, 0);
 29             }
 30         }
 31 
 32         [DllImport("user32.dll")]
 33         public static extern int ReleaseCapture();
 34 
 35         [DllImport("user32.dll")]
 36         public static extern int SendMessage(IntPtr hwnd, int msg, int up, int lp);
 37 
 38         protected override void WndProc(ref Message m)
 39         {
 40             switch (m.Msg)
 41             {
 42                 case (int)WindowsMessage.WM_NCHITTEST:
 43                     this.WM_NCHITTEST(ref m);
 44                     break;
 45                 default:
 46                     base.WndProc(ref m);
 47                     break;
 48             }
 49         }
 50 
 51         public static int LOWORD(int value)
 52         {
 53             return value & 0xFFFF;
 54         }
 55 
 56         public static int HIWORD(int value)
 57         {
 58             return value >> 16;
 59         }
 60 
 61         private void WM_NCHITTEST(ref Message m)
 62         {
 63             int wparam = m.LParam.ToInt32();
 64             Point point = new Point(LOWORD(wparam), HIWORD(wparam));
 65             point = this.PointToClient(point);
 66 
 67             if (point.X <= 5)
 68             {
 69                 if (point.Y <= 5)
 70                     m.Result = (IntPtr)WinAPIConst.HTTOPLEFT;
 71                 else if (point.Y > this.Height - 5)
 72                     m.Result = (IntPtr)WinAPIConst.HTBOTTOMLEFT;
 73                 else
 74                     m.Result = (IntPtr)WinAPIConst.HTLEFT;
 75             }
 76             else if (point.X >= this.Width - 5)
 77             {
 78                 if (point.Y <= 5)
 79                     m.Result = (IntPtr)WinAPIConst.HTTOPRIGHT;
 80                 else if (point.Y >= this.Height - 5)
 81                     m.Result = (IntPtr)WinAPIConst.HTBOTTOMRIGHT;
 82                 else
 83                     m.Result = (IntPtr)WinAPIConst.HTRIGHT;
 84             }
 85             else if (point.Y <= 5)
 86             {
 87                 m.Result = (IntPtr)WinAPIConst.HTTOP;
 88             }
 89             else if (point.Y >= this.Height - 5)
 90             {
 91                 m.Result = (IntPtr)WinAPIConst.HTBOTTOM;
 92             }
 93             else
 94                 base.WndProc(ref m);
 95         }
 96     }
 97 
 98     public enum WindowsMessage
 99     {
100         /// <summary>
101         /// 移动鼠标,桉树或释放鼠标时发生
102         /// </summary>
103         WM_NCHITTEST = 0x84,
104     }
105     public class WinAPIConst
106     {
107         public const int HTLEFT = 10;
108         public const int HTRIGHT = 11;
109         public const int HTTOP = 12;
110         public const int HTTOPLEFT = 13;
111         public const int HTTOPRIGHT = 14;
112         public const int HTBOTTOM = 15;
113         public const int HTBOTTOMLEFT = 0x10;//16
114         public const int HTBOTTOMRIGHT = 17;
115         public const int HTCAPTION = 2;
116 
117     }
118 }
因为相信,所以我去做了......
原文地址:https://www.cnblogs.com/jeffqing/p/2748255.html