gpt4 book ai didi

c# - 如何获得具有 3D 外观风格的彩色 datagridview 标题

转载 作者:行者123 更新时间:2023-11-30 20:54:11 25 4
gpt4 key购买 nike

我正在使用 Winform datagridview 并尝试通过更改背景色来自定义标题。为了能够修改颜色,我必须将 EnableHeadersVisualStyles 属性设置为 false。

但是当我设置这个属性时,我失去了默认的 3D 主题。您知道是否有任何解决方法来保存 3D 样式并具有彩色标题?

我的代码:

dataGridTaxes.EnableHeadersVisualStyles = false;
dataGridTaxes.ColumnHeadersDefaultCellStyle = BlueDataGridHeaderStyle.GetInstance();

其中 BlueDataGridHeaderStyle 只是带有新 BackColor 的基本样式

最佳答案

您必须自定义您的DataGridView,这是我为您制作的示例代码。与 ColumnHeader 的默认 3D 样式相比,它看起来非常好,但支持 BackColorHover backcolor:

    //CellPainting event handler for your dataGridView1
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e){
if (e.RowIndex == -1 && e.ColumnIndex > -1){
e.PaintBackground(e.CellBounds, true);
RenderColumnHeader(e.Graphics, e.CellBounds, e.CellBounds.Contains(hotSpot) ? hotSpotColor : backColor);
RenderColumnHeaderBorder(e.Graphics, e.CellBounds, e.ColumnIndex);
using (Brush brush = new SolidBrush(e.CellStyle.ForeColor)){
using (StringFormat sf = new StringFormat() {LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center}) {
e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, brush, e.CellBounds, sf);
}
}
e.Handled = true;
}
}
Color hotSpotColor = Color.LightGreen;//For hover backcolor
Color backColor = Color.LimeGreen; //For backColor
Point hotSpot;
private void RenderColumnHeader(Graphics g, Rectangle headerBounds, Color c) {
int topHeight = 10;
Rectangle topRect = new Rectangle(headerBounds.Left, headerBounds.Top+1, headerBounds.Width, topHeight);
RectangleF bottomRect = new RectangleF(headerBounds.Left, headerBounds.Top+1 + topHeight, headerBounds.Width, headerBounds.Height- topHeight-4);
Color c1 = Color.FromArgb(180, c);
using (SolidBrush brush = new SolidBrush(c1)) {
g.FillRectangle(brush, topRect);
brush.Color = c;
g.FillRectangle(brush, bottomRect);
}
}
private void RenderColumnHeaderBorder(Graphics g, Rectangle headerBounds, int colIndex) {
g.DrawRectangle(new Pen(Color.White, 0.1f), headerBounds.Left + 0.5f, headerBounds.Top + 0.5f,headerBounds.Width-1f,headerBounds.Height-1f);
ControlPaint.DrawBorder(g, headerBounds, Color.Gray, 0, ButtonBorderStyle.Inset,
Color.Gray, 0, ButtonBorderStyle.Inset,
Color.Gray, colIndex != dataGridView1.ColumnCount - 1 ? 1 : 0, ButtonBorderStyle.Inset,
Color.Gray, 1, ButtonBorderStyle.Inset);
}
//MouseMove event handler for your dataGridView1
private void dataGridView1_MouseMove(object sender, MouseEventArgs e){
hotSpot = e.Location;
}
//MouseLeave event handler for your dataGridView1
private void dataGridView1_MouseLeave(object sender, EventArgs e){
hotSpot = Point.Empty;
}

enter image description here

您可以使用ControlPaint.DrawBorder3D 实现RenderColumnHeaderBorder,这样更简单:

private void RenderColumnHeaderBorder(Graphics g, Rectangle headerBounds, int colIndex)
{
ControlPaint.DrawBorder3D(g, headerBounds, Border3DStyle.Raised, Border3DSide.All & ~Border3DSide.Middle);
}

enter image description here

private void RenderColumnHeaderBorder(Graphics g, Rectangle headerBounds, int colIndex)
{
ControlPaint.DrawBorder3D(g, headerBounds, Border3DStyle.RaiseInner, Border3DSide.All & ~Border3DSide.Middle);
}

enter image description here

private void RenderColumnHeaderBorder(Graphics g, Rectangle headerBounds, int colIndex)
{
ControlPaint.DrawBorder3D(g, headerBounds, Border3DStyle.RaiseOuter, Border3DSide.All & ~Border3DSide.Middle);
}

enter image description here

关于c# - 如何获得具有 3D 外观风格的彩色 datagridview 标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19296027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com