/// <summary>
/// 重新绘制窗口样式
/// </summary>
/// <param name="control"></param>
/// <param name="bitmap"></param>
public
static
void
CreateControlRegion(Control control, Bitmap bitmap)
{
if
(control ==
null
|| bitmap ==
null
)
return
;
control.Width = bitmap.Width;
control.Height = bitmap.Height;
if
(control
is
System.Windows.Forms.Form)
{
Form form = (Form)control;
form.Width = control.Width;
form.Height = control.Height;
form.FormBorderStyle = FormBorderStyle.None;
form.BackgroundImage = bitmap;
GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);
form.Region =
new
Region(graphicsPath);
form.Width = bitmap.Width;
form.Height = bitmap.Height;
}
else
if
(control
is
System.Windows.Forms.Button)
{
Button button = (Button)control;
button.Text =
""
;
button.Cursor = Cursors.Hand;
button.BackgroundImage = bitmap;
GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);
button.Region =
new
Region(graphicsPath);
button.Width = bitmap.Width;
button.Height = bitmap.Height;
button.FlatStyle = FlatStyle.Popup;
}
}
private
static
GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap)
{
GraphicsPath graphicsPath =
new
GraphicsPath();
Color colorTransparent = bitmap.GetPixel(0, 0);
int
colOpaquePixel = 0;
for
(
int
row = 0; row < bitmap.Height - 1; row++)
{
colOpaquePixel = 0;
for
(
int
col = 0; col < bitmap.Width - 1; col++)
{
if
(bitmap.GetPixel(col, row) != colorTransparent)
{
colOpaquePixel = col;
int
colNext = col;
///从找到的不透明点开始,继续寻找不透明点,一直到找到或则达到图片宽度
for
(colNext = colOpaquePixel; colNext < bitmap.Width; colNext++)
{
Color gpi = bitmap.GetPixel(colNext, row);
if
(bitmap.GetPixel(colNext, row) == colorTransparent)
{
break
;
}
}
{
graphicsPath.AddRectangle(
new
Rectangle(colOpaquePixel, row, colNext - colOpaquePixel, 1));
}
col = colNext;
}
}
}
return
graphicsPath;
}