Winform窗体通常都是矩形的,而本人在需要将一幅图片作为窗体背景,并且窗体边框呈现为图片外轮廓的不规则边缘时,却发现Framework并没有提供直接的实现。经过一通百度、Google和查阅 MSDN 后,发现实现的途径主要有以下几种:
这三种方法各有优劣:
对于第3种方法,网上大多数的文章都只是介绍了实现的原理和一些简单的 Demo 代码。直接照做可能会遇到以下的一些问题而影响显示效果:
解决这两个问题很简单,是要注意一些操作的细节。先看实现代码:
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace HoverTreeShape
{
public partial class Form_HoverTree : Form
{
public Form_HoverTree()
{
InitializeComponent();
//从指定的位图中获取透明度大于 10 的区域;
Bitmap img = (Bitmap)pictureBox_HoverTree.Image;
GraphicsPath grapth = GetNoneTransparentRegion(img, 10);
this.Region = new Region(grapth);
//要显示的图片设置为窗体背景;
this.BackgroundImage = pictureBox_HoverTree.Image;
this.BackgroundImageLayout = ImageLayout.Zoom;
//在修改窗体尺寸之前设置窗体为无边框样式;
this.FormBorderStyle = FormBorderStyle.None;
this.Width = pictureBox_HoverTree.Image.Width;
this.Height = pictureBox_HoverTree.Image.Height;
}
/// <summary>
/// 返回指定图片中的非透明区域 by 何问起
/// </summary>
/// <param name="img">位图</param>
/// <param name="alpha">alpha 小于等于该值的为透明</param>
/// <returns></returns>
public static GraphicsPath GetNoneTransparentRegion(Bitmap img, byte alpha)
{
int h_height = img.Height;
int h_width = img.Width;
int xStart, xEnd;
GraphicsPath grpPath = new GraphicsPath();
for (int y = 0; y < h_height; y++)
{
//逐行扫描;
for (int x = 0; x < h_width; x++)
{
//略过连续透明的部分;
while (x < h_width && img.GetPixel(x, y).A <= alpha)
{
x++;
}
//不透明部分;
xStart = x;
while (x < h_width && img.GetPixel(x, y).A > alpha)
{
x++;
}
xEnd = x;
if (img.GetPixel(x - 1, y).A > alpha)
{
grpPath.AddRectangle(new Rectangle(xStart, y, xEnd - xStart, 1));
}
}
}
return grpPath;
}
private void button_hovertree_Click(object sender, System.EventArgs e)
{
Close();
}
}
}
以上的代码中,解决边框与背景出现错误的办法是“在修改窗体尺寸之前设置窗体为无边框样式”,因为,窗体的长宽是包含在窗体标题栏的尺寸的,而背景显示图片的工作区是除去窗体标题栏的。
解决在设计时“界面控件在背景图片上的布局问题”可以也很简单,通过一些操作技巧就可以达到,具体如下: