最近想在项目中添加一个要有阴影的panel控件,找了好多资料,最后通过采用图片的方式实现了panel的阴影效果,效果图如下:

重绘代码如下:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D; namespace ShadowPanel3
{
public partial class UserPanel : Panel
{
public UserPanel()
{ }
/// <summary>
/// 字段和属性 , panel的颜色
/// </summary>
private Color _panelColor;
public Color PanelColor
{
get { return _panelColor; }
set { this._panelColor = value; }
} /// <summary>
/// 字段和属性,border的颜色
/// </summary>
private Color _borderColor;
public Color BorderColor
{
get { return _borderColor; }
set { this._borderColor = value; }
} /// <summary>
/// 阴影区域大小
/// </summary>
private int shadowSize = ; //将预备的小图标转化
static Image shadowDownRight = new Bitmap(typeof(UserPanel), "Images.tshadowdownright.png");
static Image shadowDown = new Bitmap(typeof(UserPanel), "Images.tshadowdown.png");
static Image shadowRight = new Bitmap(typeof(UserPanel), "Images.tshadowright.png");
static Image shadowTop = new Bitmap(typeof(UserPanel),"Images.tshadowtop.png");
static Image shadowLeft = new Bitmap(typeof(UserPanel),"Images.tshadowleft.png");
static Image shadowLeftDown = new Bitmap(typeof(UserPanel),"Images.tshadowleftdown.png");
static Image shadowLeftTop = new Bitmap(typeof(UserPanel),"Images.tshadowlefttop.png");
static Image shadowTopLeft = new Bitmap(typeof(UserPanel),"Images.tshadowtopleft.png"); /// <summary>
/// 重绘panel
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//Get the graphics object. We need something to draw with
Graphics g = e.Graphics; //下边和右边画笔
TextureBrush shadowRightBrush = new TextureBrush(shadowRight,WrapMode.Tile);
TextureBrush shadowDownBrush = new TextureBrush(shadowDown,WrapMode.Tile); //上边和左边画笔
TextureBrush shadowLeftBrush = new TextureBrush(shadowLeft, WrapMode.Tile);
TextureBrush shadowTopBrush = new TextureBrush(shadowTop,WrapMode.Tile); //给画笔定位
shadowDownBrush.TranslateTransform(, Height - shadowSize);
shadowRightBrush.TranslateTransform(Width - shadowSize, ); shadowTopBrush.TranslateTransform(,);
shadowLeftBrush.TranslateTransform(,); //每个阴影区域非配一个矩形区域
Rectangle shadowDownRectangle = new Rectangle(
shadowSize, //X
Height-shadowSize, //Y
Width-shadowSize*, //width(stretches)
shadowSize //height
); Rectangle shadowRightRectangle = new Rectangle(
Width-shadowSize, //X
shadowSize, //Y
shadowSize, //width
Height-shadowSize* //height(stretches)
); Rectangle shadowTopRectangle = new Rectangle(
shadowSize, //X
, //Y
Width-shadowSize*, //width
shadowSize //height(stretches)
); Rectangle shadowLeftRectangle = new Rectangle(
, //X
shadowSize, //Y
shadowSize, //width
Height-shadowSize* //height(stretches)
); //在底部和右边画出阴影
g.FillRectangle(shadowDownBrush,shadowDownRectangle);
g.FillRectangle(shadowRightBrush,shadowRightRectangle);
//在上部和左边画出阴影
g.FillRectangle(shadowTopBrush,shadowTopRectangle);
g.FillRectangle(shadowLeftBrush,shadowLeftRectangle); //四个角落处的阴影
g.DrawImage(shadowDownRight, new Rectangle(Width-shadowSize, Height-shadowSize,shadowSize,shadowSize));
g.DrawImage(shadowLeftDown, new Rectangle(, Height - shadowSize, shadowSize, shadowSize));
g.DrawImage(shadowLeftTop, new Rectangle(, , shadowSize, shadowSize));
g.DrawImage(shadowTopLeft, new Rectangle(Width-shadowSize, , shadowSize, shadowSize)); Rectangle fullRectangle = new Rectangle(
,
,
Width - (shadowSize + ),
Height - (shadowSize + )
); if (PanelColor != null)
{
SolidBrush bgBrush = new SolidBrush(_panelColor);
g.FillRectangle(bgBrush,fullRectangle);
} //给panel添加边框颜色
if (_borderColor != null)
{
Pen borderPen = new Pen(BorderColor);
g.DrawRectangle(borderPen,fullRectangle);
} //释放画笔资源
shadowDownBrush.Dispose();
shadowRightBrush.Dispose();
shadowLeftBrush.Dispose();
shadowTopBrush.Dispose(); shadowDownBrush = null;
shadowRightBrush = null;
shadowTopBrush = null;
shadowLeftBrush = null;
} //Correct resizing
protected override void OnResize(EventArgs e)
{
base.Invalidate();
base.OnResize(e);
}
}
}

最新文章

  1. 关于Apache日志的统计
  2. OC语言BLOCK和协议
  3. 20145222黄亚奇《Java程序设计》实验二实验报告
  4. Java发展史之Java由来
  5. 1.Spring Web MVC有什么
  6. Introduction to Windows 8: The Definitive Guide for Developer
  7. ASCII字符集
  8. Cocos2dx老版本适配64位
  9. ISIN编码
  10. Decode放在where条件后的新用法
  11. iOS集成微信支付
  12. Java语言中IO流的操作规律学习笔记
  13. android studio的快捷键设置的和eclispe一样
  14. bzoj 2653: middle (主席树+二分)
  15. python模拟艺龙网登录requests包
  16. gdb 调试及优化
  17. &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=7&quot; /&gt;
  18. DIV+CSS如何让文字垂直居中?(转)
  19. Linux下启动Tomcat启动并显示控制台日志信息
  20. LNMP笔记:阿里云32位 CentOS 5.4 配置 LNMP环境

热门文章

  1. Java中transient有何作用?
  2. 重构6-Push Down Field(字段下移)
  3. 每天一个Linux命令(1):ls命令
  4. JDK 自带工具试用(一)
  5. weimi 短信API post方式的简易代码。
  6. css改变谷歌浏览器的滚动条样式
  7. 基础面试题——HTML/CSS
  8. 根据不同的分辨率选择不同的css文件
  9. static和public
  10. 【转】DBA需要的技能