C#创建不规则窗体代码

  1. using system;
  2.         using system.Collections.Generic;
  3.         using system.ComponentModel;
  4.         using system.Data;
  5.         using system.Drawing;
  6.         using system.Text;
  7.         using system.windows.Forms;
  8.         using system.Runtime.InteropServices;
  9.         namespace APIDemo
  10.         {
  11.             public partial class Form1 : Form
  12.             {
  13.                 [StructLayout(LayoutKind.Sequential)]
  14.                 private struct POINTAPI
  15.                 {
  16.                     internal int x;
  17.                     internal int y;
  18.                 }
  19.                 [DllImport("gdi32.dll")]
  20.                 private static extern IntPtr  CreatePolygonRgn(
  21.                      ref POINTAPI lpPoint,
  22.                      int nCount,
  23.                      int nPolyFillMode);
  24.                 [DllImport("user32.dll")]
  25.                 private static extern IntPtr  SetWindowRgn(
  26.                      IntPtr  hWnd,
  27.                      IntPtr  hRgn,
  28.                      ref Boolean bRedraw);
  29.                 public Form1()
  30.                 {
  31.                     InitializeComponent();
  32.                     //创建不规则窗体
  33.                     POINTAPI[] poin;
  34.                     poin =new POINTAPI [5];
  35.                     poin[0].x = 90;
  36.                     poin[0].y = 90;
  37.                     poin[1].x = this.Width;
  38.                     poin[1].y = 0;
  39.                     poin[2].x = Width ;
  40.                     poin[2].y = this.Height/2;
  41.                     poin[3].x = Width / 2;
  42.                     poin[3].y = Height / 2;
  43.                     poin[4].x = 0;
  44.                     poin[4].y = Width;
  45.                     Boolean flag = true;
  46.                    IntPtr  hRgn= CreatePolygonRgn(ref poin[0],8,1);
  47.                    SetWindowRgn(this.Handle, hRgn, ref flag );
  48.                    this.BackColor = Color.BurlyWood;
  49.                 }
  50.                 //设置窗体显示状态
  51.                 [DllImport("user32.dll")]
  52.                 private static extern int SetWindowPos(
  53.                      IntPtr  hwnd,
  54.                      int hWndInsertAfter,
  55.                      int x,
  56.                      int y,
  57.                      int cx,
  58.                      int cy,
  59.                      int wFlags);
  60.                 private void Start_Btn_Click(object sender, EventArgs e)
  61.                 {//始终显示在前面
  62.                     SetWindowPos(this.Handle, -1, 0, 0, 0, 0, 1);
  63.                 }
  64.                 private void button1_Click(object sender, EventArgs e)
  65.                 {
  66.                     //最小化始终显示在前面
  67.                     SetWindowPos(this.Handle, -1, 0, 0, 0, 0, 0);
  68.                 }
  69.             }
  70.         }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值