本文转载于:CSKIN论坛,地址:http://bbs.cskin.net/thread-62-1-1.html
下面贴下核心代码
public static void RenderBackground(Graphics g, Image img, Rectangle rect) {
//填充四个角
g.DrawImage(img, new Rectangle(rect.X, rect.Y, 5, 5),
new Rectangle(0, 0, 5, 5), GraphicsUnit.Pixel);
g.DrawImage(img, new Rectangle(rect.Right - 5, rect.Y, 5, 5),
new Rectangle(img.Width - 5, 0, 5, 5), GraphicsUnit.Pixel);
g.DrawImage(img, new Rectangle(rect.X, rect.Bottom - 5, 5, 5),
new Rectangle(0, img.Height - 5, 5, 5), GraphicsUnit.Pixel);
g.DrawImage(img, new Rectangle(rect.Right - 5, rect.Bottom - 5, 5, 5),
new Rectangle(img.Width - 5, img.Height - 5, 5, 5), GraphicsUnit.Pixel);
//四边
g.DrawImage(img, new Rectangle(rect.X, rect.Y + 5, 5, rect.Height - 10),
new Rectangle(0, 5, 5, img.Height - 10), GraphicsUnit.Pixel);
g.DrawImage(img, new Rectangle(rect.X + 5, rect.Y, rect.Width - 10, 5),
new Rectangle(5, 0, img.Width - 10, 5), GraphicsUnit.Pixel);
g.DrawImage(img, new Rectangle(rect.Right - 5, rect.Y + 5, 5, rect.Height - 10),
new Rectangle(img.Width - 5, 5, 5, img.Height - 10), GraphicsUnit.Pixel);
g.DrawImage(img, new Rectangle(rect.X + 5, rect.Bottom - 5, rect.Width - 10, 5),
new Rectangle(5, img.Height - 5, img.Width - 10, 5), GraphicsUnit.Pixel);
//中间
g.DrawImage(img,
new Rectangle(rect.X + 5, rect.Y + 5, rect.Width - 10, rect.Height - 10),
new Rectangle(5, 5, img.Width - 10, img.Height - 10), GraphicsUnit.Pixel);
}
public class TestA : Control
{
protected override void OnPaint(PaintEventArgs e) {
RenderHelper.RenderBackground(
e.Graphics,
Properties.Resources.Qbtn_Gray,//资源图
this.ClientRectangle);
base.OnPaint(e);
}
}