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)));
}
这样既可得到图片所示形状的窗体。试试看空白的地方是可以穿透的。说明这个是镂空了的窗体。
首先你可以准备一张你想要的窗体的形状的图片。如下所示
我所需要的窗体只包含除了白色的部分像素点,当然你可以准备一张黑白图片,如下所示
我们写一个函数来获取我们窗体需要的区域(这个当然需要你的窗体比你的这张图要大),如下:
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生成不规则窗体
- C#:生成基于WinForm的不规则窗体或控件
- Winform不规则窗体的实现心得
- Winform中不规则窗体制作的解决方案
- C# winform中不规则窗体制作的解决方案(已经解决24位色以上不能正常显示问题)
- WinForm中创建不规则窗体
- C# winform中不规则窗体制作的解决方案 转
- Winform中不规则窗体制作的解决方案(续)
- Winform中不规则窗体制作的解决方案(再续)
- c# WinForm 窗体美化(二)--关于东日skinbuilder软件生成皮肤的使用
- C# winform中不规则窗体制作的解决方案(已经解决24位色以上不能正常显示问题)
- [转]C# winform中不规则窗体制作的解决方案(已经解决24位色以上不能正常显示问题)
- [转]C# winform中不规则窗体制作的解决方案(已经解决24位色以上不能正常显示问题)
- c# winform动态生成窗体及控件,并使用控件事件
- C# winform中不规则窗体制作的解决方案(已经解决24位色以上不能正常显示问题)
- C#之Winform基础 点击多次button仅生成一个窗体
- C# Winform 窗体美化(三、不规则窗体)
- C# winform中不规则窗体制作的解决方案(已经解决24位色以上不能正常显示问题)
- Winform命令窗口与窗体一同打开/如何生成DLL文件
- (转)C# winform中不规则窗体制作的解决方案(已经解决24位色以上不能正常显示问题)