C# WinForm 自定义控件,DataGridView背景透明,TabControl背景透明
2017-04-19 11:28
1296 查看
using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace RaywindStudio.Components { public partial class DataGViewX : DataGridView { public DataGViewX() { InitializeComponent(); this.SetStyle(ControlStyles.UserPaint, true);//用户自己绘制 this.SetStyle(ControlStyles.ResizeRedraw, true); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); } private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } private void InitializeComponent() { components = new System.ComponentModel.Container(); } [DescriptionAttribute("自定义背景图:当BackTransparent=True时,忽略此设置,直接使用父容器背景")] [EditorBrowsable(EditorBrowsableState.Always), Browsable(true)] public Image BackImage { get; set; } [DescriptionAttribute("背景透明:当True时,直接使用父容器背景。否则使用BackImage填充背景")] [EditorBrowsable(EditorBrowsableState.Always), Browsable(true)] public bool BackTransparent { get; set; } = true; protected override void PaintBackground(Graphics graphics, Rectangle clipBounds, Rectangle gridBounds) { base.PaintBackground(graphics, clipBounds, gridBounds); if(BackTransparent) BackImage = GetBackImage(this.Parent, this.Left, this.Top, this.Width, this.Height); if (BackImage != null) graphics.DrawImage(BackImage, clipBounds); } public Bitmap GetBackImage(Control parent, int x, int y, int w, int h) { if (parent.BackgroundImage != null) { Bitmap bt = new Bitmap(parent.Width, parent.Height); PictureBox pb = new PictureBox(); pb.Size = parent.Size; pb.BackgroundImage = parent.BackgroundImage; pb.BackgroundImageLayout = parent.BackgroundImageLayout; pb.DrawToBitmap(bt, pb.DisplayRectangle); pb.Dispose(); Bitmap destBitmap = new Bitmap(w, h); Graphics g = Graphics.FromImage(destBitmap); g.DrawImage(bt, new Rectangle(0, 0, w, h), new Rectangle(x, y, w, h), GraphicsUnit.Pixel); bt.Dispose(); g.Dispose(); return destBitmap; } else return null; } } }
DataGViewX
相关文章推荐
- C# WinForm控件美化扩展系列之TabControl控件(1)
- C# winform如何设置ListBox背景图或者透明背景及边框色
- C# TabConTrol控件背景颜色问题
- winform TabControl 添加关闭按钮 Add close button to TabControl TabPages - C#
- WinForm中ToolBar与TabControl的一些事件写法(C#)
- C# WinForm中控件与背景透明
- c# winform 点击按钮切换tabcontrol标签
- C# Winform中如何让PictureBox的背景透明
- C# Winform 变相实现背景透明
- C#自定义控件背景透明
- C# WinForm中控件与背景透明+c# linklabel 去掉下划线的方法
- WinForm中TabControl的一些事件写法(C#)
- 【转】C# TabConTrol控件背景颜色问题
- WinForm中TabControl的一些事件写法(C#)
- 背景透明的ListBox(C#.WinForm)
- C# winfrom TabConTrol控件背景颜色问题
- WinForm中TabControl的一些事件写法(C#)
- C# WinForm下DataGridView绘制背景图
- 背景透明的ListBox(C#.WinForm)
- c# winform 隐藏tabcontrol标签