圆形窗体

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 );
        }


        
Windows 窗体设计器生成的代码

        
/// <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;
            }

        }

    }

}

posted on 2007-02-03 11:28  Achilles.NET  阅读(2254)  评论(0编辑  收藏  举报