using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
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 );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(128)), ((System.Byte)(255)));
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.Location = new System.Drawing.Point(169, 178);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 19);
this.label1.TabIndex = 0;
this.label1.Text = "圆形窗体";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(128)), ((System.Byte)(255)));
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.Drawing.Size(392, 373);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Example007 用获取路径的方法得到圆形窗体";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//DLL引用
[DllImport("gdi32")]
private static extern IntPtr BeginPath(IntPtr hdc);
[DllImport("gdi32")]
private static extern int SetBkMode(IntPtr hdc,int nBkMode);
private System.Windows.Forms.Label label1;
const int transparent = 1;
[DllImport("gdi32")]
private static extern IntPtr EndPath(IntPtr hdc);
[DllImport("gdi32")]
private static extern IntPtr PathToRegion(IntPtr hdc);
[DllImport("gdi32")]
private static extern int Ellipse(IntPtr hdc,int x1,int y1,int x2,int y2);
[DllImport("gdi32")]
private static extern int Circle(int x,int y,int r);
[DllImport("user32")]
private static extern IntPtr SetWindowRgn(IntPtr hwnd,IntPtr hRgn,bool bRedraw);
[DllImport("user32")]
private static extern IntPtr GetDC(IntPtr hwnd);
/// <summary>
/// 窗体加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, System.EventArgs e)
{
IntPtr dc;
IntPtr region;
dc = GetDC(this.Handle);
BeginPath(dc);
//根据路径创建不规则窗体
SetBkMode(dc,transparent);
//设置为透明模式
Ellipse(dc,50,50,350,350);
//Circle(this.Width / 2,this.Height /2,this.Width / 2);
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;
}
}
}
}