C# WinForm 自定义控件,DataGridView背景透明,TabControl背景透明-LMLPHP

 注意: 以下代码,属性直接赋值的语法糖要vs2015以上才支持。
 
 using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace RaywindStudio.Components
{
public class TabCtrlX : TabControl
{
public TabCtrlX()
{
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);
} #region designer
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region 组件设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
} #endregion
#endregion
//[EditorBrowsable(EditorBrowsableState.Always), Browsable(true)]
public override Color BackColor {
get {
return Color.FromArgb(Alpha, BgColor);
}
set {
Alpha = BackColor.A;
BgColor = BackColor;
}
} [DisplayName("Color.Alpha")]
[CategoryAttribute("Color"), DescriptionAttribute("Opacity不透明度0--255")]
[EditorBrowsable(EditorBrowsableState.Always), Browsable(true)]
public byte Alpha { get; set; } = ; [DisplayName("Color.BgColor")]
[CategoryAttribute("Color"), DescriptionAttribute("TabControl工作区背景色")]
[EditorBrowsable(EditorBrowsableState.Always), Browsable(true)]
public Color BgColor { get; set; } = Color.White; [DisplayName("Color.BordColor")]
[CategoryAttribute("Color"), DescriptionAttribute("TabControl边线颜色")]
[EditorBrowsable(EditorBrowsableState.Always), Browsable(true)]
public Color BordColor { get; set; } = Color.LightGray; [DisplayName("Color.TitleColor")]
[CategoryAttribute("Color"), DescriptionAttribute("TabPage标头背景色")]
[EditorBrowsable(EditorBrowsableState.Always), Browsable(true)]
public Color TitleColor { get; set; } = Color.WhiteSmoke; [DisplayName("Color.TitleSeleColor")]
[CategoryAttribute("Color"), DescriptionAttribute("TabPage标头选中背景色")]
[EditorBrowsable(EditorBrowsableState.Always), Browsable(true)]
public Color TitleSeleColor { get; set; } = Color.White; [DisplayName("Color.TextColor")]
[CategoryAttribute("Color"), DescriptionAttribute("TabPage标题颜色")]
[EditorBrowsable(EditorBrowsableState.Always), Browsable(true)]
public Color TextColor { get; set; } = Color.Gray; [DisplayName("Color.TextSeleColor")]
[CategoryAttribute("Color"), DescriptionAttribute("TabPage选中标题颜色")]
[EditorBrowsable(EditorBrowsableState.Always), Browsable(true)]
public Color TextSeleColor { get; set; } = Color.Black; protected override void OnPaint(PaintEventArgs e)
{
this.DrawTitle(e.Graphics);
base.OnPaint(e);
DrawBorder(e.Graphics);
} protected void DrawBorder(Graphics g)
{
g.DrawRectangle(new Pen(BordColor, 1F), ClientRectangle);
} protected void DrawTitle(Graphics g)
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
using (SolidBrush sb = new SolidBrush(SystemColors.Control))
{
for (int i = ; i < this.TabPages.Count; i++)
{
Rectangle rect = this.GetTabRect(i);
if (this.SelectedIndex == i)
{
sb.Color = TitleSeleColor;
g.FillRectangle(sb, rect);
g.DrawString(this.TabPages[i].Text, this.Font, new SolidBrush(TextSeleColor), rect, sf);
}
else
{
sb.Color = TitleColor;
g.FillRectangle(sb, rect);
g.DrawString(this.TabPages[i].Text, this.Font, new SolidBrush(TextColor), rect, sf);
}
}
}
}
}
}

TabCtrlX

 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(, , w, h), new Rectangle(x, y, w, h), GraphicsUnit.Pixel);
bt.Dispose();
g.Dispose();
return destBitmap;
}
else
return null;
}
} }

DataGViewX

05-06 02:10