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

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

WinForm生成不规则窗体

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

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

}

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

 

 WinForm生成不规则窗体

 
 

 

相关文章:

  • 2022-12-23
  • 2021-08-10
  • 2021-04-09
  • 2021-10-07
  • 2021-08-26
  • 2021-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-12
  • 2022-12-23
  • 2022-12-23
  • 2021-11-12
  • 2021-07-09
  • 2022-12-23
  • 2021-07-19
相关资源
相似解决方案