基本信息
源码名称:C# 简单地在WinForm上放置一个有阴影边框的矩形面板
源码大小:0.09M
文件格式:.rar
开发语言:C#
更新时间:2019-03-22
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace ShadowPanel { public class ShadowPanel : Panel { private Color _panelColor; public Color PanelColor { get { return _panelColor; } set { _panelColor = value; } } private Color _borderColor; public Color BorderColor { get { return _borderColor; } set { _borderColor = value; } } private int shadowSize = 5; private int shadowMargin = 2; // static for good perfomance static Image shadowDownRight = new Bitmap(typeof(ShadowPanel), "Images.tshadowdownright.png"); static Image shadowDownLeft = new Bitmap(typeof(ShadowPanel), "Images.tshadowdownleft.png"); static Image shadowDown = new Bitmap(typeof(ShadowPanel), "Images.tshadowdown.png"); static Image shadowRight = new Bitmap(typeof(ShadowPanel), "Images.tshadowright.png"); static Image shadowTopRight = new Bitmap(typeof(ShadowPanel), "Images.tshadowtopright.png"); public ShadowPanel() { } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); // Get the graphics object. We need something to draw with ;-) Graphics g = e.Graphics; // Create tiled brushes for the shadow on the right and at the bottom. TextureBrush shadowRightBrush = new TextureBrush(shadowRight, WrapMode.Tile); TextureBrush shadowDownBrush = new TextureBrush(shadowDown, WrapMode.Tile); // Translate (move) the brushes so the top or left of the image matches the top or left of the // area where it's drawed. If you don't understand why this is necessary, comment it out. // Hint: The tiling would start at 0,0 of the control, so the shadows will be offset a little. shadowDownBrush.TranslateTransform(0, Height - shadowSize); shadowRightBrush.TranslateTransform(Width - shadowSize, 0); // Define the rectangles that will be filled with the brush. // (where the shadow is drawn) Rectangle shadowDownRectangle = new Rectangle( shadowSize shadowMargin, // X Height - shadowSize, // Y Width - (shadowSize * 2 shadowMargin), // width (stretches) shadowSize // height ); Rectangle shadowRightRectangle = new Rectangle( Width - shadowSize, // X shadowSize shadowMargin, // Y shadowSize, // width Height - (shadowSize * 2 shadowMargin) // height (stretches) ); // And draw the shadow on the right and at the bottom. g.FillRectangle(shadowDownBrush, shadowDownRectangle); g.FillRectangle(shadowRightBrush, shadowRightRectangle); // Now for the corners, draw the 3 5x5 pixel images. g.DrawImage(shadowTopRight, new Rectangle(Width - shadowSize, shadowMargin, shadowSize, shadowSize)); g.DrawImage(shadowDownRight, new Rectangle(Width - shadowSize, Height - shadowSize, shadowSize, shadowSize)); g.DrawImage(shadowDownLeft, new Rectangle(shadowMargin, Height - shadowSize, shadowSize, shadowSize)); // Fill the area inside with the color in the PanelColor property. // 1 pixel is added to everything to make the rectangle smaller. // This is because the 1 pixel border is actually drawn outside the rectangle. Rectangle fullRectangle = new Rectangle( 1, // X 1, // Y Width - (shadowSize 2), // Width Height - (shadowSize 2) // Height ); if (PanelColor != null) { SolidBrush bgBrush = new SolidBrush(_panelColor); g.FillRectangle(bgBrush, fullRectangle); } // Draw a nice 1 pixel border it a BorderColor is specified if (_borderColor != null) { Pen borderPen = new Pen(BorderColor); g.DrawRectangle(borderPen, fullRectangle); } // Memory efficiency shadowDownBrush.Dispose(); shadowRightBrush.Dispose(); shadowDownBrush = null; shadowRightBrush = null; } // Correct resizing protected override void OnResize(EventArgs e) { base.Invalidate(); base.OnResize(e); } } }