C# WinForm 自定义控件,DataGridView背景透明,TabControl背景透明
1 using System.ComponentModel; 2 using System.Drawing; 3 using System.Windows.Forms; 4 namespace RaywindStudio.Components 5 { 6 public class TabCtrlX : TabControl 7 { 8 public TabCtrlX() 9 { 10 InitializeComponent(); 11 this.SetStyle(ControlStyles.UserPaint, true);//用户自己绘制 12 this.SetStyle(ControlStyles.ResizeRedraw, true); 13 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); 14 this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 15 //让控件支持透明色 16 this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); 17 } 18 19 #region designer 20 /// <summary> 21 /// 必需的设计器变量。 22 /// </summary> 23 private System.ComponentModel.IContainer components = null; 24 25 /// <summary> 26 /// 清理所有正在使用的资源。 27 /// </summary> 28 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> 29 protected override void Dispose(bool disposing) 30 { 31 if (disposing && (components != null)) 32 { 33 components.Dispose(); 34 } 35 base.Dispose(disposing); 36 } 37 38 #region 组件设计器生成的代码 39 40 /// <summary> 41 /// 设计器支持所需的方法 - 不要 42 /// 使用代码编辑器修改此方法的内容。 43 /// </summary> 44 private void InitializeComponent() 45 { 46 components = new System.ComponentModel.Container(); 47 } 48 49 #endregion 50 #endregion 51 //[EditorBrowsable(EditorBrowsableState.Always), Browsable(true)] 52 public override Color BackColor { 53 get { 54 return Color.FromArgb(Alpha, BgColor); 55 } 56 set { 57 Alpha = BackColor.A; 58 BgColor = BackColor; 59 } 60 } 61 62 [DisplayName("Color.Alpha")] 63 [CategoryAttribute("Color"), DescriptionAttribute("Opacity不透明度0--255")] 64 [EditorBrowsable(EditorBrowsableState.Always), Browsable(true)] 65 public byte Alpha { get; set; } = 16; 66 67 [DisplayName("Color.BgColor")] 68 [CategoryAttribute("Color"), DescriptionAttribute("TabControl工作区背景色")] 69 [EditorBrowsable(EditorBrowsableState.Always), Browsable(true)] 70 public Color BgColor { get; set; } = Color.White; 71 72 [DisplayName("Color.BordColor")] 73 [CategoryAttribute("Color"), DescriptionAttribute("TabControl边线颜色")] 74 [EditorBrowsable(EditorBrowsableState.Always), Browsable(true)] 75 public Color BordColor { get; set; } = Color.LightGray; 76 77 [DisplayName("Color.TitleColor")] 78 [CategoryAttribute("Color"), DescriptionAttribute("TabPage标头背景色")] 79 [EditorBrowsable(EditorBrowsableState.Always), Browsable(true)] 80 public Color TitleColor { get; set; } = Color.WhiteSmoke; 81 82 [DisplayName("Color.TitleSeleColor")] 83 [CategoryAttribute("Color"), DescriptionAttribute("TabPage标头选中背景色")] 84 [EditorBrowsable(EditorBrowsableState.Always), Browsable(true)] 85 public Color TitleSeleColor { get; set; } = Color.White; 86 87 [DisplayName("Color.TextColor")] 88 [CategoryAttribute("Color"), DescriptionAttribute("TabPage标题颜色")] 89 [EditorBrowsable(EditorBrowsableState.Always), Browsable(true)] 90 public Color TextColor { get; set; } = Color.Gray; 91 92 [DisplayName("Color.TextSeleColor")] 93 [CategoryAttribute("Color"), DescriptionAttribute("TabPage选中标题颜色")] 94 [EditorBrowsable(EditorBrowsableState.Always), Browsable(true)] 95 public Color TextSeleColor { get; set; } = Color.Black; 96 97 protected override void OnPaint(PaintEventArgs e) 98 { 99 this.DrawTitle(e.Graphics); 100 base.OnPaint(e); 101 DrawBorder(e.Graphics); 102 } 103 104 protected void DrawBorder(Graphics g) 105 { 106 g.DrawRectangle(new Pen(BordColor, 1F), ClientRectangle); 107 } 108 109 protected void DrawTitle(Graphics g) 110 { 111 StringFormat sf = new StringFormat(); 112 sf.Alignment = StringAlignment.Center; 113 sf.LineAlignment = StringAlignment.Center; 114 using (SolidBrush sb = new SolidBrush(SystemColors.Control)) 115 { 116 for (int i = 0; i < this.TabPages.Count; i++) 117 { 118 Rectangle rect = this.GetTabRect(i); 119 if (this.SelectedIndex == i) 120 { 121 sb.Color = TitleSeleColor; 122 g.FillRectangle(sb, rect); 123 g.DrawString(this.TabPages[i].Text, this.Font, new SolidBrush(TextSeleColor), rect, sf); 124 } 125 else 126 { 127 sb.Color = TitleColor; 128 g.FillRectangle(sb, rect); 129 g.DrawString(this.TabPages[i].Text, this.Font, new SolidBrush(TextColor), rect, sf); 130 } 131 } 132 } 133 } 134 } 135 }
TabCtrlX
1 using System.ComponentModel; 2 using System.Drawing; 3 using System.Windows.Forms; 4 5 namespace RaywindStudio.Components 6 { 7 8 public partial class DataGViewX : DataGridView 9 { 10 public DataGViewX() 11 { 12 InitializeComponent(); 13 this.SetStyle(ControlStyles.UserPaint, true);//用户自己绘制 14 this.SetStyle(ControlStyles.ResizeRedraw, true); 15 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); 16 this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 17 this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); 18 } 19 20 private System.ComponentModel.IContainer components = null; 21 protected override void Dispose(bool disposing) 22 { 23 if (disposing && (components != null)) 24 { 25 components.Dispose(); 26 } 27 base.Dispose(disposing); 28 } 29 private void InitializeComponent() 30 { 31 components = new System.ComponentModel.Container(); 32 } 33 34 [DescriptionAttribute("自定义背景图:当BackTransparent=True时,忽略此设置,直接使用父容器背景")] 35 [EditorBrowsable(EditorBrowsableState.Always), Browsable(true)] 36 public Image BackImage { get; set; } 37 38 [DescriptionAttribute("背景透明:当True时,直接使用父容器背景。否则使用BackImage填充背景")] 39 [EditorBrowsable(EditorBrowsableState.Always), Browsable(true)] 40 public bool BackTransparent { get; set; } = true; 41 42 protected override void PaintBackground(Graphics graphics, Rectangle clipBounds, Rectangle gridBounds) 43 { 44 base.PaintBackground(graphics, clipBounds, gridBounds); 45 if(BackTransparent) 46 BackImage = GetBackImage(this.Parent, this.Left, this.Top, this.Width, this.Height); 47 if (BackImage != null) 48 graphics.DrawImage(BackImage, clipBounds); 49 } 50 51 public Bitmap GetBackImage(Control parent, int x, int y, int w, int h) 52 { 53 if (parent.BackgroundImage != null) 54 { 55 Bitmap bt = new Bitmap(parent.Width, parent.Height); 56 PictureBox pb = new PictureBox(); 57 pb.Size = parent.Size; 58 pb.BackgroundImage = parent.BackgroundImage; 59 pb.BackgroundImageLayout = parent.BackgroundImageLayout; 60 pb.DrawToBitmap(bt, pb.DisplayRectangle); 61 pb.Dispose(); 62 Bitmap destBitmap = new Bitmap(w, h); 63 Graphics g = Graphics.FromImage(destBitmap); 64 g.DrawImage(bt, new Rectangle(0, 0, w, h), new Rectangle(x, y, w, h), GraphicsUnit.Pixel); 65 bt.Dispose(); 66 g.Dispose(); 67 return destBitmap; 68 } 69 else 70 return null; 71 } 72 } 73 74 }
DataGViewX
转载于:https://www.cnblogs.com/leavind/p/6732530.html
C# WinForm 自定义控件,DataGridView背景透明,TabControl背景透明相关推荐
- CSS 背景(background)(背景颜色color、背景图片image、背景平铺repeat、背景位置position、背景附着、背景简写、背景透明、链接导航栏综合案例)
1. 背景颜色(color) background-color:颜色值; 默认的值是 transparent 透明的 示例代码: <!DOCTYPE html> <html lang ...
- vc实现透明位图,透明背景
vc实现透明位图,透明背景 我们在进行程序的界面设计时,常常希望将位图的关键部分,也既是图像的前景显示在界面上,而将位图的背景隐藏起来,将位图与界面很自然的融合在一起,本文介绍了透明位图的制作知识,并 ...
- CSS实现背景透明而背景上的文字不透明
在我们设计制作一些网页的时候可能会用到半透明的效果,首先我们可能会想到用PNG图片处理,当然这是一个不错的办法,唯一的兼容性问题就是ie6 下的BUG,但这也不困难,加上一段js处理就行了.但假如我们 ...
- CSS实现背景图片透明文字不透明效果的两种方法
网页设计中经常要在背景图上放一些文字介绍,这就需要背景图片能有透明效果以便突出显示文字信息,经多方查阅,终于找到了2种有趣的方法. 1.在文字层添加rgba样式实现半透明效果 方法: 背景图片层添加样 ...
- 如何实现背景/背景图片透明文字不透明
如何实现背景/背景图片透明文字不透明 第一种情况,背景为自定义颜色 第二种情况,背景为图片 总结: 第一种情况,背景为自定义颜色 直接给父级背景颜色设置background:rgba();就可以了: ...
- C# WinForm窗体制作以图片为背景的登陆界面
一.Form窗体 1.标题栏不显示 FormBorderStyle = None; 2.任务栏不显示 ShowInTaskbar = false; 3.关闭按钮不显示 ControlBox = fal ...
- html控件透明与背景透明
html控件透明与背景透明 如何实现一个控件的半透明效果? IE中关于半透明和透明控件的一些说明 .大家在有些时候希望自己做的控件是半透明或透明的,就像半透明的窗体一样,我经过一段时间的查询,发现这个 ...
- 验证码及验证码透明的背景
不说什么,代码能为我解释.透明的背景重要的一点就是只能在于png格式,而不是jpg格式. package com.shengdai.gzb.yzmcode.web; import java.awt.C ...
- css实现背景透明文字不透明
我们所说的设置透明度实际上是设置不透明度 使用opacity会导致背景和图片都透明,在此处不合适 <!DOCTYPE html> <html> <head> < ...
- duilib修复ActiveXUI控件bug,以支持flash透明动态背景
转载请说明原出处,谢谢~~ 昨天在QQ控件里和同学说起QQ2013登陆窗体的开发,从界面角度考虑,单单一个登陆界面是很容易做出来的.腾讯公司为了防止各种盗号行为可谓煞费苦心,QQ2013采用了动态背景 ...
最新文章
- 干货 | 使用FFT变换自动去除图像中严重的网纹
- B - 数据结构实验之排序二:交换排序(冒泡和快排)
- 获得WebBrowser中的图片数据
- OpenCV textDetectionModel和textRecognitionModel API的端到端的实例(附完整代码)
- jQuery 重置/reset()表单
- 10-5 4-6 查询在具有最小内存容量的所有PC中具有最快处理器的PC制造商 (10 分)
- vue基础知识之vue-resource/axios
- python基础笔记_python基础笔记
- Excel按某一列排序
- Windows10 VS2017 C++ Json解析(使用jsoncpp库)
- 2021年PMP考试模拟题8(含答案解析)
- Python安装word2vec
- x86 单线并发多拨_带宽“单线多拨“倍增大法教程
- LCD-QC1602A v2.0
- Day1学firefly学到python
- 入门神经网络——识别图片是否为猫
- 【优化求解】基于猫群算法CSO求解最优目标matlab源码
- 基于 Vue.js+Springboot 的学院社团管理系统的设计与实现
- 365天挑战LeetCode1000题——Day 103 400题 检查二进制字符串字段 最大子序列交替和 最低票价 K 站中转内最便宜的航班
- 【光纤通信课程-每周一练(含答案)】第十周—光纤通信系统设计与性能分析
热门文章
- “主要的编程范型”及其语言特性关系(多图)
- OpenCV之feature2d 模块. 2D特征框架(2)特征描述 使用FLANN进行特征点匹配 使用二维特征点(Features2D)和单映射(Homography)寻找已知物体 平面物体检测
- 图像处理(二)Seam Carving算法-Siggraph 2007
- 编程之美-饮料供货方法整理
- 【OpenCV3】阈值化操作——cv::threshold()与cv::adaptiveThreshold()详解
- Gradle 设置 本地maven仓库及发布mavenLocal()路径的方法
- 如何对ABAP SE80 workbench做增强
- 仿LordPE获取PE结构
- 移动银行木马活跃度升级 恐成黑客攻击跳板
- (转)OpenNLP进行中文命名实体识别(下:载入模型识别实体)