C# WinForm 自定义控件,DataGridView背景透明,TabControl背景透明
2024-10-03 17:48:37
注意: 以下代码,属性直接赋值的语法糖要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
最新文章
- hdu 2202 最大三角形 (凸包)
- java8中的Stream
- 百度地图BMap API实例
- poj 2749 Building roads (二分+拆点+2-sat)
- Vue爬坑之vuex初识
- 一般处理程序获取Session方式
- codeforces 895B XK Segments 二分 思维
- 第三节:dingo/API 最新版 V2.0 之 Creating API Endpoints (连载)
- 【java】字节码操作技术
- vue的基本操作
- 修改xml成正方形,保存
- SSH(Spring+Struts2+Hibernate) of mappings(SSH三大框架的映射问题)
- redis 配置命令
- system generator学习笔记【02】
- SpringMVC之文件上传异常处理
- HDFS高级功能
- pyspider爬取数据存入es--1.安装驱动
- vs 加载 dll 缓慢
- oracle和mysql对时间与字符串的转换
- Java5的新特性