最近想在项目中添加一个要有阴影的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);
}
}
}

重绘panel控件,实现panel的阴影效果的更多相关文章

  1. 自行实现透明的控件如Panel GroupBox(使用不需要重绘父控件的效果,一切都因为窗口有了WS_EX_TRANSPARENT属性)

    CSDN的Blog开通了.我想这里的Blog作为今后自己回答别人问题的时候,收藏答案的地方很不错呢. 因为社区的贴子早晚都会沉下去,查找起来很不方便,甚至再也找不到呢. Q: http://commu ...

  2. 重绘MenuStrip 控件

    重绘MenuStrip控件 效果如图: 首先添加 CustomProfessionalRenderer类 用于重绘控件菜单样式 /// <summary> /// 自定义MenuStrip ...

  3. android控件拖动,移动、解决父布局重绘时控件回到原点

    这是主要代码: 保证其params发生改变,相对于父布局的位置就能达到位置移动到原来的位置 // 每次移动都要设置其layout,不然由于父布局可能嵌套listview,当父布局发生改变冲毁(如下拉刷 ...

  4. Delphi控件之---通过编码学习TStringGrid(也会涉及到Panel控件,还有对Object Inspector的控件Events的介绍

    我是参考了万一的博客里面的关于TStringGrid学习的教程,但是我也结合自己的实际操作和理解,加入了一些个人的补充,至少对我有用! 学用TStringGrid之——ColCount.RowCoun ...

  5. 为什么日历控件放在panel无法显示出来

    前言 以前一直没有做过c/s这块开发,最近一段时间,刚接触.当然winform自己肯定会,但是只是很早以前自己玩的时候弄弄.最近做的时候,很简单的一个效果,自己怎么也实现不出来,最后没事和一个哥们私下 ...

  6. delphi中panel控件应用

    delphi中的panel控件是怎么使用的?研究了很久了,还是搞不懂,只知道把它放到form上面,其他操作一律不懂了,有谁可以请教一下,如何把其他控件放到里面去呢?谢谢 提问者采纳   直接把控件放到 ...

  7. Panel控件的使用

    我们对控件进行分组的原因不外乎三个: 1.为了获得清晰的用户界面而将相关的窗体元素进行可视化分组. 2.编程分组,如对单选按钮进行分组. 3.为了在设计时将多个控件作为一个单元来移动. 在vb.net ...

  8. C#在splitContainer1控件和panel控件中显示窗体

    现在有两个窗体 Form1 和Form2 Form1中有控件splitContainer1 和panel .控件.我们希望Form2在splitContainer1或者panel控件中显示 1:首先看 ...

  9. GroupBox与Panel控件

    1.GroupBox控件常常用于逻辑地组合一组控件,如RadioButton 及 CheckBox控件,显示一个框架,其上有一个标题. 2.Panel 可以包含多个控件,以便将这些控件编为一组,以便方 ...

随机推荐

  1. .NET强制进行即时垃圾回收

    大家知道,.NET控制系统垃圾回收(一种自动回收未使用内存的服务)是自动的. 可有时候需要手动强制进行即时垃圾回收. 代码如下: GC.Collect();

  2. 在项目中使用SQLite数据库小结

    ------------------------------------------------------------------------推荐: - VS2012 使用 1.0.84 版的库 - ...

  3. React Native学习-CameraRoll

    react-native中CameraRoll模块提供了访问本地相册的功能. 在react版本为0.23.0的项目中,不支持Android,而且在iOS中使用CameraRoll还需要我们手动操作: ...

  4. hdu-5700 区间交(二分+树状数组)

    题目链接: 区间交 Problem Description   小A有一个含有n个非负整数的数列与mm个区间.每个区间可以表示为l​i​​,r​i​​. 它想选择其中k个区间, 使得这些区间的交的那些 ...

  5. POJ 3921 Destroying the bus stations 沿着最短路迭代加深搜索

    题目:给出一个图,问最少删除多少个点,使得从点1到点n经过的点数超过k个. 分析: 上网搜了一下,发现很多人用网络流做的,发现我不会.再后来看到这篇说网络流的做法是错的,囧. 后来发现点数有点少,直接 ...

  6. 把txt文件中的json字符串写到plist文件中

    - (void)json2Plist { NSString *filePath = [self applicationDocumentsDirectoryFileName:@"json&qu ...

  7. Android Bitmap详细介绍(转)

    转自: Bitmap详细介绍 package com.testbitmapscale; import java.io.File; import java.io.FileInputStream; imp ...

  8. Jquery和JS删除提示

    Jquery <script> $(function () { $('.deletes').click(function () { //提示 if (!confirm('确定呀删除吗?') ...

  9. 十七、Android学习笔记_Android 使用 搜索框

    1.在资源文件夹下创建xml文件夹,并创建一个searchable.xml: android:searchSuggestAuthorityshux属性的值跟实现SearchRecentSuggesti ...

  10. Part 12 Angularjs filter by multiple properties

    In the example below, we are using multiple search textboxes. As you type in the "Search name&q ...