最近想在项目中添加一个要有阴影的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. DataGridView显示行号的几种方法来自http://www.soaspx.com/dotnet/csharp/csharp_20100204_2740.html

    方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件在RowHeaderCell中绘制行号: private void dataGridView1_RowPostPai ...

  2. 【Linux/Ubuntu学习 14】Linux下查看文件和文件夹大小

    当磁盘大小超过标准时会有报警提示,这时如果掌握df和du命令是非常明智的选择. df可以查看一级文件夹大小.使用比例.档案系统及其挂入点,但对文件却无能为力.    du可以查看文件及文件夹的大小. ...

  3. PHP 中mysql如何实现事务提交?

    事务就是指对数据库的多次修改,要么全部成功,要么全部失败,不能出现部分修改成功,部分修改失败的情况. PHP下操作mysql数据库要实现事务提交,需注意以下方面: 1, 数据库表存储引擎类型设置为in ...

  4. netbeans 调试 php

    修改php.ini文件 原来配置 [XDebug];zend_extension = "E:\xampp\php\ext\php_xdebug.dll";xdebug.profil ...

  5. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13982   Accepted: 6307 ...

  6. Abp Zero——前端如何新增功能模块

    为适应不同开发人员,abp rezo的UI实现了spa和mpa两套: spa--Single-page Application(单页面应用),默认"http://localhost/Acco ...

  7. Part 98 Anonymous methods in c#

    What is an anonymous method? Anonymous method is a method without a name. Introduced in C# 2.0,they ...

  8. 发布ASP.NET网站到IIS

    1.         在Web项目中点击发布网站,如图1所示 图1 2.         选择要发布的路径——>“确定”,如果项目显示发布成功就可以了.如图2所示 图2 3.         打 ...

  9. DEEPIN下搭建FTP服务器步骤(备忘录)

    1.打开终端,执行命令[apt-get install vsftpd],安装VSFTPD 2.安装完成后,修改以下配置信息(否则文件无法传输) [echo 'listen=YES'>>/e ...

  10. 基于系统的UIMenuController的使用及自定义UIMenuItem

    1.前言 在开发中 UIMenuController 用得较少,偶尔遇到了,一时竟想不起来,因此做个回顾 2.系统默认支持 UIMenuController 的UI控件 UITextField UIT ...