winform DataGridView重绘滚动条颜色大小

时间: 2023-02-11 17:57:16 浏览: 281
Winform DataGridView 中可以使用 "AdvancedCellBorderStyle" 属性来更改单元格边框样式,包括颜色和大小。可以通过设置 "AdvancedCellBorderStyle.Top" 和 "AdvancedCellBorderStyle.Bottom" 属性来更改滚动条颜色和大小。 如果要更改整个 DataGridView 的滚动条颜色和大小,可以使用 "ScrollBars" 属性来设置滚动条样式。
相关问题

winform DataGridView重绘滚动条

如果你想要重新绘制 Winform DataGridView 控件的滚动条,可以通过继承 DataGridView 控件并重写其 OnPaint 方法来实现。 以下是一个示例代码: ```c# public class MyDataGridView : DataGridView { protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); // 绘制滚动条 Rectangle scrollBarRect = new Rectangle(this.Width - SystemInformation.VerticalScrollBarWidth, this.Top, SystemInformation.VerticalScrollBarWidth, this.Height); ControlPaint.DrawScrollButton(e.Graphics, scrollBarRect, ScrollButton.Down, ButtonState.Normal); } } ``` 在这个示例中,我们继承了 DataGridView 控件并重写了它的 OnPaint 方法。在方法中,我们首先调用了基类的 OnPaint 方法以确保 DataGridView 的默认绘制行为不会被覆盖。 然后,我们使用 ControlPaint.DrawScrollButton 方法在 DataGridView 控件的右侧绘制了一个向下的滚动条按钮。你可以根据需要自定义绘制滚动条的方式。 最后,将你的新 MyDataGridView 控件添加到窗体上,并使用它来代替默认的 DataGridView 控件即可。 注意:重新绘制 Winform 控件的滚动条需要慎重,因为它可能会影响控件的性能和稳定性。如果你只是想改变滚动条的颜色或大小等外观属性,可以考虑使用控件的样式和主题设置来实现。

winform DataGridView重绘滚动条样式

要重绘WinForm DataGridView的滚动条样式,需要继承DataGridView并重写它的WndProc方法。以下是一个示例代码,可以实现自定义滚动条样式: ```csharp using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; public class CustomDataGridView : DataGridView { private const int WM_PAINT = 0xF; private const int WM_NCPAINT = 0x85; private const int WM_NCCALCSIZE = 0x83; private const int WM_ERASEBKGND = 0x14; private bool isScrolling = false; protected override void WndProc(ref Message m) { switch (m.Msg) { case WM_PAINT: base.WndProc(ref m); DrawScrollBar(); break; case WM_NCPAINT: case WM_NCCALCSIZE: case WM_ERASEBKGND: break; default: base.WndProc(ref m); break; } } private void DrawScrollBar() { if (!isScrolling) { // 获取滚动条的Graphics对象 Graphics g = this.CreateGraphics(); // 绘制滚动条背景 Rectangle bgRect = new Rectangle(this.VerticalScrollingOffset + this.Width - SystemInformation.VerticalScrollBarWidth, this.ColumnHeadersHeight, SystemInformation.VerticalScrollBarWidth, this.Height - this.ColumnHeadersHeight); g.FillRectangle(new SolidBrush(this.BackgroundColor), bgRect); // 绘制滚动条轨道 Rectangle trackRect = new Rectangle(bgRect.X + 1, bgRect.Y + SystemInformation.VerticalScrollBarArrowHeight, bgRect.Width - 2, bgRect.Height - 2 * SystemInformation.VerticalScrollBarArrowHeight); ControlPaint.DrawScrollTrack(g, trackRect, ScrollOrientation.Vertical); // 绘制滚动条滑块 Rectangle thumbRect = new Rectangle(bgRect.X + 1, bgRect.Y + SystemInformation.VerticalScrollBarArrowHeight + (this.FirstDisplayedScrollingRowIndex * trackRect.Height / this.RowCount), bgRect.Width - 2, this.DisplayedRowCount(false) * trackRect.Height / this.RowCount); ControlPaint.DrawScrollThumb(g, thumbRect, ScrollState.Focused); // 释放Graphics对象 g.Dispose(); } } protected override void OnScroll(ScrollEventArgs e) { isScrolling = true; base.OnScroll(e); isScrolling = false; this.Invalidate(new Rectangle(this.Width - SystemInformation.VerticalScrollBarWidth, this.ColumnHeadersHeight, SystemInformation.VerticalScrollBarWidth, this.Height - this.ColumnHeadersHeight())); } } ``` 在上述代码中,我们首先重写了DataGridView的WndProc方法,在其中处理WM_PAINT消息并调用DrawScrollBar方法绘制滚动条。DrawScrollBar方法中,我们使用CreateGraphics方法获取滚动条的Graphics对象,然后绘制滚动条背景、轨道和滑块。最后,记得在OnScroll方法中调用Invalidate方法重新绘制滚动条。 使用上述代码,我们可以实现自定义滚动条样式。需要注意的是,由于我们使用了CreateGraphics方法获取Graphics对象,因此需要在绘制完滚动条后释放该对象。此外,在绘制滑块时,我们使用了FirstDisplayedScrollingRowIndex和DisplayedRowCount方法来获取当前滚动条位置和可见行数,因此需要确保这些属性已经被正确设置。

相关推荐

最新推荐

recommend-type

WinForm中DataGridView添加,删除,修改操作具体方法

介绍了WinForm中DataGridView添加,删除,修改操作具体方法,有需要的朋友可以参考一下
recommend-type

WinForm中DataGridView折叠控件【超好看】

这如果在BS里面那太简单了,JqGrid默认都自带,可是DataGridview不支持折叠啊。自己一点经验没有,怎么办呢?于是上网搜了相关视频,资料,开始学习起来。最后借鉴源码封了这么一个东西,发出来分享下,也能让自己...
recommend-type

C#中改变DataGridView控件边框颜色的方法

主要介绍了C#中改变DataGridView控件边框颜色的方法,默认的DataGridView边框颜色很丑,本文用编程方法实现修改DataGridView边框颜色,需要的朋友可以参考下
recommend-type

Scrapy-1.8.2.tar.gz

文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

search-log.zip

搜索记录,包括时间、搜索关键词等,用于PySpark案例练习
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。