首页 > 编程语言 >【转】C# WinForm 自定义控件,DataGridView背景透明,TabControl背景透明

【转】C# WinForm 自定义控件,DataGridView背景透明,TabControl背景透明

时间:2024-01-11 13:11:24浏览次数:29  
标签:透明 set 自定义 控件 Color System new true public

原文:

https://www.cnblogs.com/leavind/p/6732530.html

 

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

标签:透明,set,自定义,控件,Color,System,new,true,public
From: https://www.cnblogs.com/81/p/17958354

相关文章

  • Vue2 使用 Knova Canvas 合成图片、多个视频、音频在一个画面中并播放,自定义 video co
    本文转载https://blog.csdn.net/RosaChampagne/article/details/128020428?spm=1001.2014.3001.5502的文章安装插件npminstallvue-konva@2konva--save在main.js中使用importVuefrom'vue';importVueKonvafrom'vue-konva';Vue.use(VueKonva);相关实现代......
  • 创新工具:2024年开发者必备的一款表格控件
    前言在现代工作环境中,信息的处理和管理是至关重要的。表格是一种常见的数据呈现和整理工具,被广泛应用于各行各业。然而,随着技术的不断发展,市场对表格控件的需求也越来越高。随着工作效率的重要性日益凸显,一款高效的表格控件成为了开发者们的首选,因此本文小编将从葡萄城公司的纯前......
  • 【愚公系列】2024年01月 WPF控件专题 ListBox控件详解
    ......
  • 使用Winform开发自定义用户控件,以及实现相关自定义事件的处理
    在我们一些非标的用户界面中,我们往往需要自定义用户控件界面,从而实现不同的内容展示和处理规则,本篇内容介绍使用Winform开发自定义用户控件,以及实现相关自定义事件的处理。1、用户控件的界面分析对于比较规范的界面,需要进行一定的分析,以便从中找到对应的规则,逐步细化为自定义用......
  • 自定义快捷键实操与踩坑
    0.缘起要做一个自定义快捷键的功能,web端实现。这里分为两块逻辑,一部分是快捷键的应用,一部分是快捷键的定义。先从应用说起,快捷键实际上是对浏览器按键动作的监听,不过由于浏览器本身也有快捷键,就会有冲突的情况,自定义的要求应运而生。快捷键的定义,其实类似于设置的功能,也是存、......
  • go 新建一个自定义包
    一、概述在go中新建一个自定义包供其他包使用。步骤:1.新建一个目录2.目录下新建一个xxx.go文件3.在xxx.go文件中使用packagexxx(包名)4.此时你的包已经新建好了5.在需要使用上面包的地方导入即可,如:import"xxxx"p......
  • 【愚公系列】2024年01月 WPF控件专题 ComboBox控件详解
    ......
  • springboot通过自定义注解@Log实现日志打印
    springboot通过自定义注解@Log实现日志打印效果图实操步骤注意,本代码在springboot环境下运行,jdk1.81.引入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><dependency>......
  • Three.js——十五、Box3、相机动画、lookAt()视线方向、管道漫游案例、OrbitControls
    正投影相机正投影相机和透视相机的区别如果都以高处俯视去看整个场景,正投影相机就类似于2d的可视化的效果,透视相机就类似于人眼观察效果调整left,right,top,bottom范围大小如果你想整体预览全部立方体,就需要调整相机的渲染范围,比如设置上下左右的范围。使用场景:正投影可以......
  • 自定义ADFS登录页
    修改adfs登录页公司名称:Set-AdfsGlobalWebContent-CompanyName"ExchangeOWA" 参考:ADFS自定义:https://learn.microsoft.com/zh-cn/windows-server/identity/ad-fs/operations/ad-fs-customization-in-windows-server#custom-themes-and-advanced-custom-themes 修改ADFS登录页......