类似主题 Set datagrid view background to transparent
DataGridView VB透明代码:
Imports System.ComponentModel
Public Class TransparentDGV
Inherits DataGridView
Private _DGVHasTransparentBackground As Boolean
<Category("Transparency"), Description("Select whether the control has a Transparent Background.")> Public Property DGVHasTransparentBackground As Boolean
Get
Return _DGVHasTransparentBackground
End Get
Set(ByVal value As Boolean)
_DGVHasTransparentBackground = value
If _DGVHasTransparentBackground Then
SetTransparentProperties(True)
Else
SetTransparentProperties(False)
End If
End Set
End Property
Public Sub New()
DGVHasTransparentBackground = True
End Sub
Private Sub SetTransparentProperties(ByRef SetAsTransparent As Boolean)
If SetAsTransparent Then
MyBase.DoubleBuffered = True
MyBase.EnableHeadersVisualStyles = False
MyBase.ColumnHeadersDefaultCellStyle.BackColor = Color.Transparent
MyBase.RowHeadersDefaultCellStyle.BackColor = Color.Transparent
SetCellStyle(Color.Transparent)
Else
MyBase.DoubleBuffered = False
MyBase.EnableHeadersVisualStyles = True
MyBase.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control
MyBase.RowHeadersDefaultCellStyle.BackColor = SystemColors.Control
SetCellStyle(Color.White)
End If
End Sub
Protected Overrides Sub PaintBackground(ByVal graphics As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle, ByVal gridBounds As System.Drawing.Rectangle)
MyBase.PaintBackground(graphics, clipBounds, gridBounds)
If _DGVHasTransparentBackground Then
If Not IsNothing(MyBase.Parent.BackgroundImage) Then
Dim rectSource As New Rectangle(MyBase.Location, MyBase.Size)
Dim rectDest As New Rectangle(0, 0, rectSource.Width, rectSource.Height)
Dim b As New Bitmap(Parent.ClientRectangle.Width, Parent.ClientRectangle.Height)
graphics.FromImage(b).DrawImage(MyBase.Parent.BackgroundImage, Parent.ClientRectangle)
graphics.DrawImage(b, rectDest, rectSource, GraphicsUnit.Pixel)
Else
Dim myBrush As New SolidBrush(MyBase.Parent.BackColor)
Dim rectDest As New Region(New Rectangle(0, 0, MyBase.Width, MyBase.Height))
graphics.FillRegion(myBrush, rectDest)
End If
End If
End Sub
Protected Overrides Sub OnColumnAdded(ByVal e As System.Windows.Forms.DataGridViewColumnEventArgs)
MyBase.OnColumnAdded(e)
If _DGVHasTransparentBackground Then
SetCellStyle(Color.Transparent)
End If
End Sub
Private Sub SetCellStyle(ByVal cellColour As Color)
For Each col As DataGridViewColumn In MyBase.Columns
col.DefaultCellStyle.BackColor = cellColour
col.DefaultCellStyle.SelectionBackColor = cellColour
Next
End Sub
End Class
转换C#代码?
帮助!!
OR
protected override void PaintBackground(Graphics graphics, Rectangle clipBounds, Rectangle gridBounds) {
base.PaintBackground(graphics, clipBounds, gridBounds);
Rectangle rectSource = new Rectangle(this.Location.X, this.Location.Y, this.Width, this.Height);
Rectangle rectDest = new Rectangle(0, 0, rectSource.Width, rectSource.Height);
Bitmap b = new Bitmap(Parent.ClientRectangle.Width, Parent.ClientRectangle.Height);
Graphics.FromImage(b).DrawImage(this.Parent.BackgroundImage, Parent.ClientRectangle);
graphics.DrawImage(b, rectDest, rectSource, GraphicsUnit.Pixel);
SetCellsTransparent(); }
public void SetCellsTransparent() {
this.EnableHeadersVisualStyles = false;
this.ColumnHeadersDefaultCellStyle.BackColor = Color.Transparent;
this.RowHeadersDefaultCellStyle.BackColor = Color.Transparent;
foreach (DataGridViewColumn col in this.Columns)
{
col.DefaultCellStyle.BackColor = Color.Transparent;
col.DefaultCellStyle.SelectionBackColor = Color.Transparent;
} }
示例项目请或代码
我无法运行此代码。
答案 0 :(得分:-1)
我已经想过了。 - Srriam Sakthivel
我想投射。
尽管如此,谢谢。 :)
项目下载:https://www.mediafire.com/?1nac18vdpdg27x5
示例图片:
http://s1278.photobucket.com/user/murat34can52/media/datagridviewTransparent_zpstgnkjsb7.png.html
源代码
Form1中强>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace winFormDataGridViewTransparentAndImage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/*
VB olan bu kodu C# internetteki convert siteleri sayesinde çevirdim
Ancak yeterli olmadı hata alıyordu nedeni bunu Form1.Designer.cs den
itibaren başlayan bir class olayı olduğu için. Nesne oluşturulurken
bizim verdiğimiz özelliklere göre oluşturulması gerekiyordu.
Neyse uzatmadan baya zaman harcadım C# çevirmek ve doğru istenilen
amaca ulaşmak için ama sonunda başarmak önemli adımlarımdan biri oldu.
#Murat Çakmak (Dikey)
*/
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Columns1");
dt.Columns.Add("Columns2");
dt.Rows.Add("Rows1", "Rows1");
dt.Rows.Add("Rows2", "Rows2");
dataGridView1.DataSource = dt;
dataGridView2.DataSource = dt;
}
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.Size = new Size(0,0);
dataGridView2.Size = new Size(0,0);
dataGridView1.Size = new Size(350, 350);
dataGridView2.Size = new Size(350, 350);
}
}
}
Form1 Designer
namespace winFormDataGridViewTransparentAndImage
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle5 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle6 = new System.Windows.Forms.DataGridViewCellStyle();
this.button1 = new System.Windows.Forms.Button();
this.dataGridView2 = new transDataGridView();
this.dataGridView1 = new transDataGridViewAndImage();
((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(13, 397);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 3;
this.button1.Text = "Size++";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dataGridView2
//
this.dataGridView2.AllowUserToAddRows = false;
this.dataGridView2.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.dataGridView2.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None;
this.dataGridView2.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None;
dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle1.BackColor = System.Drawing.Color.Transparent;
dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.dataGridView2.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
this.dataGridView2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle2.BackColor = System.Drawing.Color.Transparent;
dataGridViewCellStyle2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
dataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
this.dataGridView2.DefaultCellStyle = dataGridViewCellStyle2;
this.dataGridView2.DGVHasTransparentBackground = true;
this.dataGridView2.EnableHeadersVisualStyles = false;
this.dataGridView2.Location = new System.Drawing.Point(369, 12);
this.dataGridView2.Name = "dataGridView2";
this.dataGridView2.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None;
dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle3.BackColor = System.Drawing.Color.Transparent;
dataGridViewCellStyle3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
dataGridViewCellStyle3.ForeColor = System.Drawing.Color.Transparent;
dataGridViewCellStyle3.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle3.SelectionForeColor = System.Drawing.Color.Red;
dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.dataGridView2.RowHeadersDefaultCellStyle = dataGridViewCellStyle3;
this.dataGridView2.Size = new System.Drawing.Size(337, 206);
this.dataGridView2.TabIndex = 2;
//
// dataGridView1
//
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.dataGridView1.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None;
this.dataGridView1.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None;
dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle4.BackColor = System.Drawing.Color.Transparent;
dataGridViewCellStyle4.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
dataGridViewCellStyle4.ForeColor = System.Drawing.SystemColors.WindowText;
dataGridViewCellStyle4.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle4.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle4.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.dataGridView1.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle4;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.DGVHasTransparentBackground = true;
this.dataGridView1.EnableHeadersVisualStyles = false;
this.dataGridView1.Location = new System.Drawing.Point(12, 12);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None;
dataGridViewCellStyle5.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle5.BackColor = System.Drawing.Color.Transparent;
dataGridViewCellStyle5.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
dataGridViewCellStyle5.ForeColor = System.Drawing.Color.Transparent;
dataGridViewCellStyle5.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle5.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle5.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.dataGridView1.RowHeadersDefaultCellStyle = dataGridViewCellStyle5;
dataGridViewCellStyle6.ForeColor = System.Drawing.Color.Red;
this.dataGridView1.RowsDefaultCellStyle = dataGridViewCellStyle6;
this.dataGridView1.Size = new System.Drawing.Size(304, 206);
this.dataGridView1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ClientSize = new System.Drawing.Size(782, 432);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGridView2);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private transDataGridViewAndImage dataGridView1;
private transDataGridView dataGridView2;
private System.Windows.Forms.Button button1;
}
}
透明DataGridView类源
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
public class transDataGridView : DataGridView
{
// VB Converted To C# And Code Edit : Murat Çakmak
private bool _DGVHasTransparentBackground;
[Category("Transparency"), Description("Select whether the control has a Transparent Background.")]
public bool DGVHasTransparentBackground
{
get { return _DGVHasTransparentBackground; }
set
{
_DGVHasTransparentBackground = value;
if (_DGVHasTransparentBackground)
{
this.SetTransparentProperties(true);
}
else
{
this.SetTransparentProperties(false);
}
}
}
public transDataGridView()
{
DGVHasTransparentBackground = true;
}
private void SetTransparentProperties(bool SetAsTransparent)
{
if (SetAsTransparent)
{
base.DoubleBuffered = true;
base.EnableHeadersVisualStyles = false;
base.ColumnHeadersDefaultCellStyle.BackColor = Color.Transparent;
base.RowHeadersDefaultCellStyle.BackColor = Color.Transparent;
SetCellStyle(Color.Transparent);
}
else
{
base.DoubleBuffered = false;
base.EnableHeadersVisualStyles = true;
base.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control;
base.RowHeadersDefaultCellStyle.BackColor = SystemColors.Control;
SetCellStyle(Color.White);
}
}
protected override void PaintBackground(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle gridBounds)
{
base.PaintBackground(graphics, clipBounds, gridBounds);
if (_DGVHasTransparentBackground)
{
// DataGridView Transparent !
SolidBrush myBrush = new SolidBrush(base.Parent.BackColor); // (Form1 BackColor) veya bir resme boyanabilir.
Region rectDest = new Region(new Rectangle(0, 0, base.Width, base.Height));
graphics.FillRegion(myBrush, rectDest);
}
}
protected override void OnColumnAdded(System.Windows.Forms.DataGridViewColumnEventArgs e)
{
base.OnColumnAdded(e);
if (_DGVHasTransparentBackground)
{
SetCellStyle(Color.Transparent);
}
}
private void SetCellStyle(Color cellColour)
{
foreach (DataGridViewColumn col in base.Columns)
{
col.DefaultCellStyle.BackColor = cellColour;
col.DefaultCellStyle.SelectionBackColor = cellColour; // selection cell color example Color.Red;
}
}
}
透明DataGridView和İmage类源
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
public class transDataGridViewAndImage : DataGridView
{
// VB Converted To C# And Code Edit : Murat Çakmak
private bool _DGVHasTransparentBackground;
[Category("Transparency"), Description("Select whether the control has a Transparent Background.")]
public bool DGVHasTransparentBackground
{
get { return _DGVHasTransparentBackground; }
set
{
_DGVHasTransparentBackground = value;
if (_DGVHasTransparentBackground)
{
this.SetTransparentProperties(true);
}
else
{
this.SetTransparentProperties(false);
}
}
}
public transDataGridViewAndImage()
{
DGVHasTransparentBackground = true;
}
private void SetTransparentProperties(bool SetAsTransparent)
{
if (SetAsTransparent)
{
base.DoubleBuffered = true;
base.EnableHeadersVisualStyles = false;
base.ColumnHeadersDefaultCellStyle.BackColor = Color.Transparent;
base.RowHeadersDefaultCellStyle.BackColor = Color.Transparent;
SetCellStyle(Color.Transparent);
}
else
{
base.DoubleBuffered = false;
base.EnableHeadersVisualStyles = true;
base.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control;
base.RowHeadersDefaultCellStyle.BackColor = SystemColors.Control;
SetCellStyle(Color.White);
}
}
bool imageLoad = false;
protected override void PaintBackground(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle gridBounds)
{
base.PaintBackground(graphics, clipBounds, gridBounds);
if (_DGVHasTransparentBackground)
{
// DataGridView İmage Load !
Bitmap b = new Bitmap(winFormDataGridViewTransparentAndImage.Properties.Resources._3d_Hd_Wallpapers);
graphics.DrawImage(b, 0, 0, base.Size.Width, base.Size.Height);
}
}
protected override void OnColumnAdded(System.Windows.Forms.DataGridViewColumnEventArgs e)
{
base.OnColumnAdded(e);
if (_DGVHasTransparentBackground)
{
SetCellStyle(Color.Transparent);
}
}
private void SetCellStyle(Color cellColour)
{
foreach (DataGridViewColumn col in base.Columns)
{
col.DefaultCellStyle.BackColor = cellColour;
col.DefaultCellStyle.SelectionBackColor = cellColour;
}
}
}