圆形窗体
using
System;
using
System.Drawing;
using
System.Collections;
using
System.ComponentModel;
using
System.Windows.Forms;
using
System.Data;
using
System.Runtime.InteropServices;
![None.gif](/Images/OutliningIndicators/None.gif)
namespace
Example007_用获取路径的方法得到圆形窗体
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public Form1()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if( disposing )
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (components != null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Application.Run(new Form1());
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//DLL引用
[DllImport("gdi32")]
private static extern IntPtr BeginPath(IntPtr hdc);
[DllImport("gdi32")]
private static extern int SetBkMode(IntPtr hdc,int nBkMode);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
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);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
[DllImport("gdi32")]
private static extern int Circle(int x,int y,int r);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
[DllImport("user32")]
private static extern IntPtr SetWindowRgn(IntPtr hwnd,IntPtr hRgn,bool bRedraw);
[DllImport("user32")]
private static extern IntPtr GetDC(IntPtr hwnd);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 窗体加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, System.EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IntPtr dc;
IntPtr region;
dc = GetDC(this.Handle);
BeginPath(dc);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//根据路径创建不规则窗体
SetBkMode(dc,transparent);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//设置为透明模式
Ellipse(dc,50,50,350,350);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//Circle(this.Width / 2,this.Height /2,this.Width / 2);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
EndPath(dc);
region = PathToRegion(dc);
SetWindowRgn(this.Handle,region,true);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
const int WM_NCHITTEST = 0x0084;
const int HTCLIENT = 0x0001;
const int HTCAPTION = 0x0002;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
protected override void WndProc(ref System.Windows.Forms.Message m)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch(m.Msg)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case WM_NCHITTEST:
base.WndProc(ref m);
if (m.Result == (IntPtr)HTCLIENT)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m.Result = (IntPtr)HTCAPTION;
}
break;
default:
base.WndProc(ref m);
break;
}
}
}
}
转载于:https://www.cnblogs.com/lizhi/archive/2007/02/03/638785.html