您的位置:首页 > 其它

WinForm生成不规则窗体

2012-05-19 20:45 344 查看
有的时候你需要让你的窗体更酷点,不想要规规矩矩的用那个方方正正的窗体,那么接下来的代码可能是你想要的。

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



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



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

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

}

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: