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; namespace 圆形窗体 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } [System.Runtime.InteropServices.DllImport("gdi32")] private static extern IntPtr BeginPath(IntPtr hdc); [System.Runtime.InteropServices.DllImport("gdi32")] private static extern int SetBkMode(IntPtr hdc, int nBkMode); const int TRANSPARENT = 1; [System.Runtime.InteropServices.DllImport("gdi32")] private static extern IntPtr EndPath(IntPtr hdc); [System.Runtime.InteropServices.DllImport("gdi32")] private static extern IntPtr PathToRegion(IntPtr hdc); [System.Runtime.InteropServices.DllImport("gdi32")] private static extern int Ellipse(IntPtr hdc, int X1, int Y1, int X2, int Y2); [System.Runtime.InteropServices.DllImport("user32")] private static extern IntPtr SetWindowRgn(IntPtr hwnd, IntPtr hRgn, bool bRedraw); [System.Runtime.InteropServices.DllImport("user32")] private static extern IntPtr GetDC(IntPtr hwnd); private void Form1_Load(object sender, System.EventArgs e) { IntPtr dc; IntPtr region; dc = GetDC(this.Handle); BeginPath(dc); //根据路径创建不规则窗体 SetBkMode(dc, TRANSPARENT); //设置为透明模式 Ellipse(dc, 20, 20, 220, 220); EndPath(dc); region = PathToRegion(dc); SetWindowRgn(this.Handle, region, true); } const int WM_NCHITTEST = 0x0084; const int HTCLIENT = 0x0001; const int HTCAPTION = 0x0002; protected override void WndProc(ref System.Windows.Forms.Message m) { switch (m.Msg) { case WM_NCHITTEST: base.WndProc(ref m); if (m.Result == (IntPtr)HTCLIENT) m.Result = (IntPtr)HTCAPTION; break; default: base.WndProc(ref m); break; } } } }