using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Example007_用獲取路徑的方法得到圓形窗體
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在InitializeComponent 調用後添加任何構造函數代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
Windows Form Designer generated code
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
[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;
}
}
}
}