C#创建不规则窗体代码(Win API应用)

 
  1. /* * ***********************警告********************************************  
  2.  * 如果引用请不要删除本注释!  
  3.  * 本代码受版权法和国际条约保护,如未经授权擅自复制或散发本代码(或其中任何部份),  
  4.  * 将受到严厉的法律制裁,并将在法律许可的最大限度内受到起诉!  
  5.  * 版权所有(C)  嘉怡工作室2000-2008  
  6.  * 公司:嘉怡工作室  
  7.  * 地址:湖南省岳阳县荣家湾镇城南村三组  
  8.  * 作者:易庆华先生  
  9.  * BLOG: http://yqh2648.blog.163.com.cn  
  10.  * E-Mail: yqh2648@163.com jiayistudio@163.com yqh2648@hotmail.com  
  11.  * Date:2008年  
  12.  * Runtime Version:.net 2.0  
  13.  * ************************************************************************/  
  14.   
  15. using System;   
  16.   
  17. using System.Collections.Generic;   
  18.   
  19. using System.ComponentModel;   
  20.   
  21. using System.Data;   
  22.   
  23. using System.Drawing;   
  24.   
  25. using System.Text;   
  26.   
  27. using System.Windows.Forms;   
  28.   
  29. using System.Runtime.InteropServices;   
  30.   
  31. namespace APIDemo   
  32.   
  33. {   
  34.   
  35.     public partial class Form1 : Form   
  36.   
  37.     {   
  38.   
  39.         [StructLayout(LayoutKind.Sequential)]   
  40.   
  41.         private struct POINTAPI   
  42.   
  43.         {   
  44.   
  45.             internal int x;   
  46.   
  47.             internal int y;   
  48.   
  49.         }   
  50.   
  51.   
  52.   
  53.         [DllImport("gdi32.dll")]   
  54.   
  55.         private static extern IntPtr  CreatePolygonRgn(   
  56.   
  57.              ref POINTAPI lpPoint,   
  58.   
  59.              int nCount,   
  60.   
  61.              int nPolyFillMode);   
  62.   
  63.         [DllImport("user32.dll")]   
  64.   
  65.         private static extern IntPtr  SetWindowRgn(   
  66.   
  67.              IntPtr  hWnd,   
  68.   
  69.              IntPtr  hRgn,   
  70.   
  71.              ref Boolean bRedraw);   
  72.   
  73.   
  74.   
  75.          
  76.   
  77.         public Form1()   
  78.   
  79.         {   
  80.   
  81.             InitializeComponent();   
  82.   
  83.             //创建不规则窗体   
  84.   
  85.             POINTAPI[] poin;   
  86.   
  87.             poin =new POINTAPI [5];   
  88.   
  89.             poin[0].x = 90;   
  90.   
  91.             poin[0].y = 90;   
  92.   
  93.             poin[1].x = this.Width;   
  94.   
  95.             poin[1].y = 0;   
  96.   
  97.             poin[2].x = Width ;   
  98.   
  99.             poin[2].y = this.Height/2;   
  100.   
  101.             poin[3].x = Width / 2;   
  102.   
  103.             poin[3].y = Height / 2;   
  104.   
  105.             poin[4].x = 0;   
  106.   
  107.             poin[4].y = Width;   
  108.   
  109.             Boolean flag = true;   
  110.   
  111.            IntPtr  hRgn= CreatePolygonRgn(ref poin[0],8,1);   
  112.   
  113.            SetWindowRgn(this.Handle, hRgn, ref flag );   
  114.   
  115.            this.BackColor = Color.BurlyWood;   
  116.   
  117.   
  118.   
  119.         }   
  120.   
  121.         //设置窗体显示状态   
  122.   
  123.         [DllImport("user32.dll")]   
  124.   
  125.         private static extern int SetWindowPos(   
  126.   
  127.              IntPtr  hwnd,   
  128.   
  129.              int hWndInsertAfter,   
  130.   
  131.              int x,   
  132.   
  133.              int y,   
  134.   
  135.              int cx,   
  136.   
  137.              int cy,   
  138.   
  139.              int wFlags);   
  140.   
  141.   
  142.   
  143.   
  144.   
  145.         private void Start_Btn_Click(object sender, EventArgs e)   
  146.   
  147.         {//始终显示在前面   
  148.   
  149.             SetWindowPos(this.Handle, -1, 0, 0, 0, 0, 1);   
  150.   
  151.             
  152.   
  153.         }   
  154.   
  155.   
  156.   
  157.         
  158.   
  159.         private void button1_Click(object sender, EventArgs e)   
  160.   
  161.         {   
  162.   
  163.             //最小化始终显示在前面   
  164.   
  165.             SetWindowPos(this.Handle, -1, 0, 0, 0, 0, 0);   
  166.   
  167.         }   
  168.   
  169.     }   
  170.   
  171. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值