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

重绘代码如下:

  1 using System;
  2 using System.Collections.Generic;
 3 using System.ComponentModel;
  4 using System.Drawing;
  5 using System.Data;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.Drawing.Drawing2D;
 10 
 11 namespace ShadowPanel3
 12 {
 13     public partial class UserPanel : Panel
 14     {
 15         public UserPanel()
 16         {
 17 
 18         }
 19             /// <summary>
 20         /// 字段和属性 , panel的颜色
 21         /// </summary>
 22         private Color _panelColor;
 23         public Color PanelColor 
 24         {
 25             get { return _panelColor; }
 26             set { this._panelColor = value; }
 27         }
 28 
 29         /// <summary>
 30         /// 字段和属性,border的颜色
 31         /// </summary>
 32         private Color _borderColor;
 33         public Color BorderColor
 34         {
 35             get { return _borderColor; }
 36             set { this._borderColor = value; }
 37         }
 38 
 39         /// <summary>
 40         /// 阴影区域大小
 41         /// </summary>
 42         private int shadowSize = 5;
 43 
 44         //将预备的小图标转化
 45         static Image shadowDownRight = new Bitmap(typeof(UserPanel), "Images.tshadowdownright.png");
 46         static Image shadowDown = new Bitmap(typeof(UserPanel), "Images.tshadowdown.png");
 47         static Image shadowRight = new Bitmap(typeof(UserPanel), "Images.tshadowright.png");
 48         static Image shadowTop = new Bitmap(typeof(UserPanel),"Images.tshadowtop.png");
 49         static Image shadowLeft = new Bitmap(typeof(UserPanel),"Images.tshadowleft.png");
 50         static Image shadowLeftDown = new Bitmap(typeof(UserPanel),"Images.tshadowleftdown.png");
 51         static Image shadowLeftTop = new Bitmap(typeof(UserPanel),"Images.tshadowlefttop.png");
 52         static Image shadowTopLeft = new Bitmap(typeof(UserPanel),"Images.tshadowtopleft.png");
 53 
 54         /// <summary>
 55         /// 重绘panel
 56         /// </summary>
 57         /// <param name="e"></param>
 58         protected override void OnPaint(PaintEventArgs e)
 59         {
 60             base.OnPaint(e);
 61             //Get the graphics object. We need something to draw with 
 62             Graphics g = e.Graphics;
 63 
 64             //下边和右边画笔
 65             TextureBrush shadowRightBrush = new TextureBrush(shadowRight,WrapMode.Tile);
 66             TextureBrush shadowDownBrush = new TextureBrush(shadowDown,WrapMode.Tile);
 67 
 68             //上边和左边画笔
 69             TextureBrush shadowLeftBrush = new TextureBrush(shadowLeft, WrapMode.Tile);
 70             TextureBrush shadowTopBrush = new TextureBrush(shadowTop,WrapMode.Tile);
 71 
 72             //给画笔定位
 73             shadowDownBrush.TranslateTransform(0, Height - shadowSize);
 74             shadowRightBrush.TranslateTransform(Width - shadowSize, 0);
 75 
 76             shadowTopBrush.TranslateTransform(0,0);
 77             shadowLeftBrush.TranslateTransform(0,0);
 78 
 79             //每个阴影区域非配一个矩形区域
 80             Rectangle shadowDownRectangle = new Rectangle(
 81                 shadowSize,                                                     //X
 82                 Height-shadowSize,                                         //Y
 83                 Width-shadowSize*2,                                     //width(stretches)           
 84                 shadowSize                                                    //height
 85                 );
 86 
 87             Rectangle shadowRightRectangle = new Rectangle(
 88               Width-shadowSize,                                           //X
 89               shadowSize,                                                     //Y
 90               shadowSize,                                                    //width         
 91               Height-shadowSize*2                                       //height(stretches)  
 92               );
 93 
 94             Rectangle shadowTopRectangle = new Rectangle(
 95               shadowSize,                                                    //X
 96               0,                                                                     //Y
 97               Width-shadowSize*2,                                       //width         
 98               shadowSize                                                     //height(stretches)  
 99               );
100 
101             Rectangle shadowLeftRectangle = new Rectangle(
102               0,                                                                    //X
103               shadowSize,                                                    //Y
104               shadowSize,                                                    //width         
105               Height-shadowSize*2                                       //height(stretches)  
106               );
107 
108             //在底部和右边画出阴影
109             g.FillRectangle(shadowDownBrush,shadowDownRectangle);
110             g.FillRectangle(shadowRightBrush,shadowRightRectangle);
111             //在上部和左边画出阴影
112             g.FillRectangle(shadowTopBrush,shadowTopRectangle);
113             g.FillRectangle(shadowLeftBrush,shadowLeftRectangle);
114 
115             //四个角落处的阴影
116             g.DrawImage(shadowDownRight, new Rectangle(Width-shadowSize, Height-shadowSize,shadowSize,shadowSize));
117             g.DrawImage(shadowLeftDown, new Rectangle(0, Height - shadowSize, shadowSize, shadowSize));
118             g.DrawImage(shadowLeftTop, new Rectangle(0, 0, shadowSize, shadowSize));
119             g.DrawImage(shadowTopLeft, new Rectangle(Width-shadowSize, 0, shadowSize, shadowSize));
120 
121 
122             Rectangle fullRectangle = new Rectangle(
123                 1,
124                 1,
125                 Width - (shadowSize + 2),
126                 Height - (shadowSize + 2)
127                 );
128 
129             if (PanelColor != null)
130             {
131                 SolidBrush bgBrush = new SolidBrush(_panelColor);
132                 g.FillRectangle(bgBrush,fullRectangle);
133             }
134 
135             //给panel添加边框颜色
136             if (_borderColor != null)
137             {
138                 Pen borderPen = new Pen(BorderColor);
139                 g.DrawRectangle(borderPen,fullRectangle);
140             }
141 
142             //释放画笔资源
143             shadowDownBrush.Dispose();
144             shadowRightBrush.Dispose();
145             shadowLeftBrush.Dispose();
146             shadowTopBrush.Dispose();
147 
148             shadowDownBrush = null;
149             shadowRightBrush = null;
150             shadowTopBrush = null;
151             shadowLeftBrush = null;
152         }
153 
154             //Correct resizing
155             protected override void OnResize(EventArgs e)
156             {
157                 base.Invalidate();
158                 base.OnResize(e);
159             }
160     }
161 }

 

posted on 2015-12-24 22:16  SeeYouLater  阅读(3539)  评论(3编辑  收藏  举报