C# WinForm 自定义控件,DataGridView背景透明,TabControl背景透明
注意: 以下代码,属性直接赋值的语法糖要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背景透明的更多相关文章
- duilib修复ActiveXUI控件bug,以支持flash透明动态背景
转载请说明原出处,谢谢~~ 昨天在QQ控件里和同学说起QQ2013登陆窗体的开发,从界面角度考虑,单单一个登陆界面是很容易做出来的.腾讯公司为了 防止各种盗号行为可谓煞费苦心,QQ2013采用了动态背 ...
- CSS实现背景透明而背景上的文字不透明完美解决
在我们设计制作一些网页的时候可能会用到半透明的效果,首先我们可能会想到用PNG图片处理,当然这是一个不错的办法,唯一的兼容性问题就是ie6 下的BUG,但这也不困难,加上一段js处理就行了.但假如我们 ...
- c#Winform程序的toolStripButton自己定义背景应用演示样例源代码
C# Winform程序的toolStrip中toolStripButton的背景是蓝色的,怎样改变背景及边框的颜色和样式呢? 实现此功能须要重写toolStripButton的Paint方法 这里仅 ...
- 27.给input边框和背景颜色设置全透明
给input边框和背景颜色设置全透明,但是里面的字不会消失 1.让背景颜色变透明(二选一) background-color:rgba(0,0,0,0); background:rgba(0,0,0, ...
- CSS实现背景透明而背景上的文字不透明
在我们设计制作一些网页的时候可能会用到半透明的效果,首先我们可能会想到用PNG图片处理,当然这是一个不错的办法,唯一的兼容性问题就是ie6 下的BUG,但这也不困难,加上一段js处理就行了.但假如我们 ...
- css背景透明文字不透明
测试背景透明度为0.3.文字不透明: background-color: #000; /* 一.CSS3的opacity */ opacity: 0.3; /* 兼容浏览器为:firefox,chro ...
- (八十九)c#Winform自定义控件-自定义滚动条(treeview、panel、datagridview、listbox、listview、textbox)
官网 http://www.hzhcontrols.com/ 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kw ...
- Winform自定义控件实例
本文转自http://www.cnblogs.com/hahacjh/archive/2010/04/29/1724125.html 写在前面: .Net已经成为许多软件公司的选择,而.Net自定义W ...
- 基于Winform框架DataGridView控件的SqlServer数据库查询展示功能的实现
关键词:Winform.DataGridView.SqlServer 一个基于winform框架的C/S软件,主要实现对SqlServer数据库数据表的实时查询. 一.为DataGridView添加数 ...
- Pyqt 设置 背景颜色和背景图片、 QPalette 调色板 与QPainter 画板区别 、 不规则图片
设置 背景颜色和背景图片 首先设置autoFillBackground属性为真然后定义一个QPalette对象设置QPalette对象的背景属性(颜色或图片)最后设置QWidget对象的Palette ...
随机推荐
- 20 JavaScript随机&逻辑&比较&类型转换
JavaScript 随机 Math.random(): 返回0~1之间的随机数,包括0不包括1 Math.floor():下舍入.Math.floor(2.9) = 2.Math.floor(Mat ...
- 吴裕雄--天生自然Numpy库学习笔记:NumPy 高级索引
import numpy as np x = np.array([[1, 2], [3, 4], [5, 6]]) y = x[[0,1,2], [0,1,0]] print (y) import n ...
- lc 0219
目录 ✅ 463. 岛屿的周长 描述 解答 cpp py ✅ 1122. 数组的相对排序 描述 解答 cpp py ✅ 876. 链表的中间结点 描述 解答 cpp ✅ 1160. 拼写单词 描述 解 ...
- C/S的接口测试工具
Postman概述: Postman是一个接口测试工具,在做接口测试的时候,Postman相当于一个客户端,它可以模拟用户发起的各类HTTP请求,将请求数据发送至服务端,获取对应的响应结果,从而验证响 ...
- 研究Zookeeper的原理(一)
阅读声明:以下内容是结合网上材料所写个人理解,如有不当,欢迎大家指正~~~谢谢 一.Zookeeper介绍 zookeeper,见名知意嘛,zoo动物园,keeper保持者.管理员,结合起来就是动物管 ...
- ArrayQueue(队列)
code1: #include <stdio.h> #include <conio.h> #include <stdlib.h> #define MAXSIZE 6 ...
- Invalid or unexpected token:数据格式错误
一个查询页面突然出现如下这个错误: Uncaught SyntaxError: Invalid or unexpected token, 翻译成中文是: 捕获的查询无效或意外的标记. 既然代码逻辑没问 ...
- centos启动jar包
不挂断运行命令,日志输出到log.txt中 nohup java -jar boot-cms-module-system-2.0.1.jar >log.txt & Linux 运行jar ...
- 牛客网Sql
牛客网Sql: 1.查询最晚入职的员工信息 select * from employees where hire_date =(select max(hire_date) from employee ...
- 关于TXT文件中英文单词出现频率排序问题
题目要求: 指定文件目录, 但是会递归遍历目录下的所有子目录,输出文件中所有不重复的单词,按照出现次数由多到少排列. 源码: package word; import java.io.File; i ...