WinForm生成不规则窗体

有的时候你需要让你的窗体更酷点,不想要规规矩矩的用那个方方正正的窗体,那么接下来的代码可能是你想要的。

首先你可以准备一张你想要的窗体的形状的图片。如下所示

我所需要的窗体只包含除了白色的部分像素点,当然你可以准备一张黑白图片,如下所示

我们写一个函数来获取我们窗体需要的区域(这个当然需要你的窗体比你的这张图要大),如下:

public GraphicsPath SetAlltypeWindow(Control form,string regionBMP, Color transparentColor)

        {

            GraphicsPath path =new GraphicsPath();

            if (File.Exists(regionBMP))

            {

                Bitmap bitmap =new Bitmap(regionBMP);

                form.BackgroundImage = bitmap;

                form.Width = bitmap.Width;

                form.Height = bitmap.Height;

                for (int x = 0; x < bitmap.Width; x++)

                {

                    for (int y = 0; y < bitmap.Height; y++)

                    {

                        Color c = bitmap.GetPixel(x, y);

                        if (c != transparentColor)

                        {

                            path.AddRectangle(newRectangle(x, y, 1, 1));

                        }

                    }

                }

            }

            return path;

        }

然后在窗体中指定窗体的区域(注意,窗体的FormBorderStyle=None,否则会有偏移)

  private void Form1_Load(object sender, EventArgs e)

        {

            this.Region = new Region(SetAlltypeWindow(this,"形状图片的路径",Color.FromArgb(255,255,255,255)));

}

这样既可得到图片所示形状的窗体。试试看空白的地方是可以穿透的。说明这个是镂空了的窗体。

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值