注意: 以下代码,属性直接赋值的语法糖要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

C# WinForm 自定义控件,DataGridView背景透明,TabControl背景透明的更多相关文章

  1. duilib修复ActiveXUI控件bug,以支持flash透明动态背景

    转载请说明原出处,谢谢~~ 昨天在QQ控件里和同学说起QQ2013登陆窗体的开发,从界面角度考虑,单单一个登陆界面是很容易做出来的.腾讯公司为了 防止各种盗号行为可谓煞费苦心,QQ2013采用了动态背 ...

  2. CSS实现背景透明而背景上的文字不透明完美解决

    在我们设计制作一些网页的时候可能会用到半透明的效果,首先我们可能会想到用PNG图片处理,当然这是一个不错的办法,唯一的兼容性问题就是ie6 下的BUG,但这也不困难,加上一段js处理就行了.但假如我们 ...

  3. c#Winform程序的toolStripButton自己定义背景应用演示样例源代码

    C# Winform程序的toolStrip中toolStripButton的背景是蓝色的,怎样改变背景及边框的颜色和样式呢? 实现此功能须要重写toolStripButton的Paint方法 这里仅 ...

  4. 27.给input边框和背景颜色设置全透明

    给input边框和背景颜色设置全透明,但是里面的字不会消失 1.让背景颜色变透明(二选一) background-color:rgba(0,0,0,0); background:rgba(0,0,0, ...

  5. CSS实现背景透明而背景上的文字不透明

    在我们设计制作一些网页的时候可能会用到半透明的效果,首先我们可能会想到用PNG图片处理,当然这是一个不错的办法,唯一的兼容性问题就是ie6 下的BUG,但这也不困难,加上一段js处理就行了.但假如我们 ...

  6. css背景透明文字不透明

    测试背景透明度为0.3.文字不透明: background-color: #000; /* 一.CSS3的opacity */ opacity: 0.3; /* 兼容浏览器为:firefox,chro ...

  7. (八十九)c#Winform自定义控件-自定义滚动条(treeview、panel、datagridview、listbox、listview、textbox)

    官网 http://www.hzhcontrols.com/ 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kw ...

  8. Winform自定义控件实例

    本文转自http://www.cnblogs.com/hahacjh/archive/2010/04/29/1724125.html 写在前面: .Net已经成为许多软件公司的选择,而.Net自定义W ...

  9. 基于Winform框架DataGridView控件的SqlServer数据库查询展示功能的实现

    关键词:Winform.DataGridView.SqlServer 一个基于winform框架的C/S软件,主要实现对SqlServer数据库数据表的实时查询. 一.为DataGridView添加数 ...

  10. Pyqt 设置 背景颜色和背景图片、 QPalette 调色板 与QPainter 画板区别 、 不规则图片

    设置 背景颜色和背景图片 首先设置autoFillBackground属性为真然后定义一个QPalette对象设置QPalette对象的背景属性(颜色或图片)最后设置QWidget对象的Palette ...

随机推荐

  1. 解决游览器安装Vue.js devtools插件无效的问题

    一: 打开自己写的一个vue.js网页,发现这个图标并没有亮起来,还是灰色 解决方案:  1.我们先看看Vue.js devtools是否生效,打开Bilibili(https://www.bilib ...

  2. 洛谷P1144最短路计数

    题目描述 给出一个NNN个顶点MMM条边的无向无权图,顶点编号为1−N1-N1−N.问从顶点111开始,到其他每个点的最短路有几条. 输入格式 第一行包含222个正整数N,MN,MN,M,为图的顶点数 ...

  3. JAVA GUI窗体及控件

    Swing基本操作: JAVA显示一个带按钮的窗口: import java.awt.*; import javax.swing.*; import javax.swing.border.EmptyB ...

  4. node js 判断数组中是否包含某个值

    判断数组中是否包含某个值这里有四种方法.用的测试数据: let arr=["a","b","c"]; let arr2={"a&q ...

  5. 5-3 使用antDesign的form组件

    import { Form, Icon, Input, Button, Checkbox } from 'antd'; class NormalLoginForm extends React.Comp ...

  6. 夯实Java基础(二十四)——Java8新特征之Optional类

    1.概述 对于Java程序员来说,到目前为止出现次数最多的应该是NullpointException,它是导致Java应用程序失败的最常见原因.之前处理空指针我们必须先通过条件先去判断,然后再确认是否 ...

  7. C语言入门---第九章 C语言指针

    没学指针就是没学C语言! 指针是C语言的精华,也是C语言的难点. 所谓指针,也就是内存的地址,所谓指针变量,也就是保存了内存地址的变量.不过人们往往不会区分两者的概念,而是混淆在一起使用. ===== ...

  8. idea中scala语言自动补全变量的同时,也自动补全类型

    IDE是IDEA,scala中,在new一个对象时,通过快捷键ctrl + Alt + V自动补全变量,但是我还想自动补全变量的类型,就像图中所示,在Specify type前面自动帮你打勾. 可以按 ...

  9. 常见Linux发行版有哪些?

    Linux 发行版(英语:Linux distribution,也被叫做GNU/Linux 发行版),为一般用户预先集成好的Linux操作系统及各种应用软件.一般用户不需要重新编译,在直接安装之后,只 ...

  10. 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)

    题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...