当前位置: 首页 > news >正文

winfrom 自定义空间 ,UcCustomDataGridView

  public partial class UcCustomDataGridView : DataGridView{// 标题列(列头)背景色:#19196F(深蓝色)public Color HeaderBackColor { get; set; } = Color.FromArgb(25, 25, 111); // #19196F的RGB值// 内容单元格背景色:#4682B4(钢蓝色)public Color ContentBackColor { get; set; } = Color.FromArgb(70, 130, 180); // #4682B4的RGB值// 其他样式属性(优化文字颜色以适配背景)public Color HeaderTextColor { get; set; } = Color.White; // 列头文字白色(与#19196F对比清晰)public Font HeaderFont { get; set; } = new Font("宋体", 9F, FontStyle.Regular);public Color ContentTextColor { get; set; } = Color.White; // 内容文字白色(与#4682B4对比清晰)public Color CellBorderColor { get; set; } = Color.FromArgb(100, 149, 237); // 边框浅蓝色(适配整体风格)public Color SelectedRowColor { get; set; } = Color.FromArgb(30, 144, 255); // 选中行亮蓝色public UcCustomDataGridView(){// 基础设置SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);DoubleBuffered = true;RowHeadersVisible = false;AllowUserToAddRows = false;ReadOnly = true;ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;ColumnHeadersHeight = 35;CellBorderStyle = DataGridViewCellBorderStyle.None;GridColor = CellBorderColor;// 单击选中整行配置SelectionMode = DataGridViewSelectionMode.FullRowSelect;EditMode = DataGridViewEditMode.EditProgrammatically;MultiSelect = false;// 内容文字颜色应用ForeColor = ContentTextColor;Font = new Font("宋体", 12F, FontStyle.Regular);// 列头字体:宋体,12pt(可根据需求添加加粗)HeaderFont = new Font("宋体", 12F, FontStyle.Bold);}protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e){base.OnCellPainting(e);if (e.RowIndex == -1 && e.ColumnIndex >= 0){DrawHeaderCell(e); // 绘制列头(标题列)}else if (e.RowIndex >= 0 && e.ColumnIndex >= 0){DrawContentCell(e); // 绘制内容单元格}}/// <summary>/// 绘制标题列(列头):背景色#19196F/// </summary>private void DrawHeaderCell(DataGridViewCellPaintingEventArgs e){// 标题列背景色(纯色#19196F)using (SolidBrush brush = new SolidBrush(HeaderBackColor)){e.Graphics.FillRectangle(brush, e.CellBounds);}// 标题文字(白色、居中、加粗)string text = Columns[e.ColumnIndex].HeaderText;StringFormat sf = new StringFormat{Alignment = StringAlignment.Center,LineAlignment = StringAlignment.Center};using (SolidBrush textBrush = new SolidBrush(HeaderTextColor)){e.Graphics.DrawString(text, HeaderFont, textBrush, e.CellBounds, sf);}// 标题列右侧边框if (e.ColumnIndex != Columns.Count - 1){using (Pen pen = new Pen(CellBorderColor, 1)){e.Graphics.DrawLine(pen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);}}e.Handled = true;}/// <summary>/// 绘制内容单元格:背景色#4682B4/// </summary>private void DrawContentCell(DataGridViewCellPaintingEventArgs e){// 内容背景色(#4682B4),选中行使用高亮色Color backColor = ContentBackColor;if (Rows[e.RowIndex].Selected){backColor = SelectedRowColor;}using (SolidBrush brush = new SolidBrush(backColor)){e.Graphics.FillRectangle(brush, e.CellBounds);}// 内容文字(白色、居中)if (e.Value != null){StringFormat sf = new StringFormat{Alignment = StringAlignment.Center,LineAlignment = StringAlignment.Center};using (SolidBrush textBrush = new SolidBrush(ContentTextColor)){Rectangle textRect = new Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 1, e.CellBounds.Width - 2, e.CellBounds.Height - 2);e.Graphics.DrawString(e.Value.ToString(), Font, textBrush, textRect, sf);}}// 内容单元格边框using (Pen pen = new Pen(CellBorderColor, 1)){if (e.ColumnIndex != Columns.Count - 1){e.Graphics.DrawLine(pen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);}if (e.RowIndex != Rows.Count - 1){e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);}}e.Handled = true;}// 选中行变化事件(保持不变)public event Action<int> SelectedRowChanged;protected override void OnSelectionChanged(EventArgs e){base.OnSelectionChanged(e);SelectedRowChanged?.Invoke(CurrentRow?.Index ?? -1);}// 其他重绘方法(保持不变)protected override void OnColumnWidthChanged(DataGridViewColumnEventArgs e){base.OnColumnWidthChanged(e);Invalidate();}protected override void OnRowHeightChanged(DataGridViewRowEventArgs e){base.OnRowHeightChanged(e);Invalidate();}}

http://www.dtcms.com/a/606111.html

相关文章:

  • Spring Boot环境配置
  • 新版Xsens Link可穿戴式动捕设备
  • 淘客网站如何做推广莱芜网站建设案例
  • Linux 上怎么跑 Python 脚本
  • 微服务污点分析
  • 科学小制作 小发明 简单 手工网站seo策划方案
  • 手搓UEFI.h
  • MySQL(六) - 视图管理
  • R语言在线编译器 | 提供快速便捷的编程环境,助力数据分析与学习
  • 网站没有备案是假的吗什么是大型门户网站
  • 做电脑网站与手机上的一样吗网站建设维护需要懂哪些知识
  • UE5 PAK 封包 加载实用方法
  • UE5蓝图实现物体自动沿样条线运动
  • 基于Fovea算法的AI机械手目标检测模型详解
  • 十大景观设计网站上海有名的设计工作室
  • TR3D: Towards Real-Time Indoor 3D Object Detection论文精读
  • Vue 3 函数式编程与Composition API
  • 数据结构——四十一、分块查找(索引顺序查找)(王道408)
  • 苏州网站建设公司有哪几家还可以的洛阳制作网站的公司哪家好
  • 源码篇 虚拟DOM
  • Pig4Cloud微服务分布式ID生成:Snowflake算法深度集成指南
  • 考研资源合集
  • Go语言编译器 | 探讨Go语言编译器的工作原理与优化策略
  • 宁夏一站式网站建设网站做的简单是什么意思
  • 重庆网站建设重庆无锡做企业网站
  • 永嘉县住房和城乡建设局网站哪个程序做下载网站好
  • 刷题leetcode——链表2
  • Telegram 自动打包上传机器人 通过 Telegram 消息触发项目的自动打包和上传。
  • vps网站管理助手下载网页设计及网站建设在线作业
  • Frida 把MessagePack的二进制数据反序列化成JSON,