private int shadowSize = 5;
//将预备的小图标转化
static Image shadowDownRight = new Bitmap(Resources.tshadowdownright);//下右
static Image shadowDown = new Bitmap(Resources.tshadowdown);// 下
static Image shadowRight = new Bitmap(Resources.tshadowright);//右
static Image shadowTop = new Bitmap(Resources.tshadowtop);//上
static Image shadowLeft = new Bitmap(Resources.tshadowleft);//左
static Image shadowLeftDown = new Bitmap(Resources.tshadowleftdown);//左下
static Image shadowLeftTop = new Bitmap(Resources.tshadowlefttop);//左上
static Image shadowTopRight = new Bitmap(Resources.tshadowtopleft);//上右
Graphics g = this.CreateGraphics();
bool ShowDown = true;
bool ShowRight = true;
bool ShowTop = true;
bool ShowLeft = true;
int x = 50;
int y = 100;
int Height = 100;
int Width = 100;
//在底部和右边画出阴影
if (ShowDown) //底部
{
//下边画笔
TextureBrush shadowDownBrush = new TextureBrush(shadowDown, WrapMode.Tile);
//给画笔定位
shadowDownBrush.TranslateTransform(0, Height - shadowSize);
//每个阴影区域非配一个矩形区域
Rectangle shadowDownRectangle = new Rectangle(
x + shadowSize, //X
y + Height - shadowSize, //Y
Width - shadowSize * 2, //width(stretches)
shadowSize //height
);
g.FillRectangle(shadowDownBrush, shadowDownRectangle);
//释放画笔资源
shadowDownBrush.Dispose();
shadowDownBrush = null;
}
if (ShowRight)//右部
{
TextureBrush shadowRightBrush = new TextureBrush(shadowRight, WrapMode.Tile);
shadowRightBrush.TranslateTransform(Width - shadowSize, 0);
Rectangle shadowRightRectangle = new Rectangle(
x + Width - shadowSize, //X
y+shadowSize, //Y
shadowSize, //width
Height - shadowSize * 2 //height(stretches)
);
g.FillRectangle(shadowRightBrush, shadowRightRectangle);
//释放画笔资源
shadowRightBrush.Dispose();
shadowRightBrush = null;
}
//在上部和左边画出阴影
if (ShowTop)//上部
{
TextureBrush shadowTopBrush = new TextureBrush(shadowTop, WrapMode.Tile);
shadowTopBrush.TranslateTransform(0, 0);
Rectangle shadowTopRectangle = new Rectangle(
x + shadowSize, //X
y+0, //Y
Width - shadowSize * 2, //width
shadowSize //height(stretches)
);
g.FillRectangle(shadowTopBrush, shadowTopRectangle);
//释放画笔资源
shadowTopBrush.Dispose();
shadowTopBrush = null;
}
if (ShowLeft)//左部
{
TextureBrush shadowLeftBrush = new TextureBrush(shadowLeft, WrapMode.Tile);
shadowLeftBrush.TranslateTransform(0, 0);
Rectangle shadowLeftRectangle = new Rectangle(
x+0, //X
y+shadowSize, //Y
shadowSize, //width
Height - shadowSize * 2 //height(stretches)
);
g.FillRectangle(shadowLeftBrush, shadowLeftRectangle);
//释放画笔资源
shadowLeftBrush.Dispose();
shadowLeftBrush = null;
}
//四个角落处的阴影
if (ShowTop && ShowDown && ShowLeft && ShowRight)
{
g.DrawImage(shadowDownRight, new Rectangle(x+Width - shadowSize, y+Height - shadowSize, shadowSize, shadowSize));//右下
g.DrawImage(shadowLeftDown, new Rectangle(x+0, y + Height - shadowSize, shadowSize, shadowSize));//左下
g.DrawImage(shadowLeftTop, new Rectangle(x+0, y+0, shadowSize, shadowSize));//左上
g.DrawImage(shadowTopRight, new Rectangle(x+Width - shadowSize, y+0, shadowSize, shadowSize));//右上
}
else if (ShowLeft && ShowDown && ShowRight)
{
g.DrawImage(shadowDownRight, new Rectangle(x + Width - shadowSize, y + Height - shadowSize, shadowSize, shadowSize));//右下
g.DrawImage(shadowLeftDown, new Rectangle(x + 0, y + Height - shadowSize, shadowSize, shadowSize));//左下
}
else if (ShowDown && ShowRight && ShowTop)
{
g.DrawImage(shadowDownRight, new Rectangle(x + Width - shadowSize, y + Height - shadowSize, shadowSize, shadowSize));//右下
g.DrawImage(shadowTopRight, new Rectangle(x + Width - shadowSize, y + 0, shadowSize, shadowSize));//右上
}
else if (ShowRight && ShowTop && ShowLeft)
{
g.DrawImage(shadowLeftTop, new Rectangle(x + 0, y + 0, shadowSize, shadowSize));//左上
g.DrawImage(shadowTopRight, new Rectangle(x + Width - shadowSize, y + 0, shadowSize, shadowSize));//右上
}
else if (ShowTop && ShowLeft && ShowDown)
{
g.DrawImage(shadowLeftDown, new Rectangle(x + 0, y + Height - shadowSize, shadowSize, shadowSize));//左下
g.DrawImage(shadowLeftTop, new Rectangle(x + 0, y + 0, shadowSize, shadowSize));//左上
}
else if (ShowDown && ShowLeft)
{
g.DrawImage(shadowLeftDown, new Rectangle(x + 0, y + Height - shadowSize, shadowSize, shadowSize));//左下
}
else if (ShowDown && ShowRight)
{
g.DrawImage(shadowDownRight, new Rectangle(x + Width - shadowSize, y + Height - shadowSize, shadowSize, shadowSize));//右下
}
else if (ShowTop && ShowLeft)
{
g.DrawImage(shadowLeftTop, new Rectangle(x + 0, y + 0, shadowSize, shadowSize));//左上
}
else if (ShowTop && ShowRight)
{
g.DrawImage(shadowTopRight, new Rectangle(x + Width - shadowSize, y + 0, shadowSize, shadowSize));//右上
}