C# 获取表单图像背景时出现问题
我正在创建一个透明的datagridview //我得到了父母的背景图像 位图parentBackGround=新位图(this.Parent.BackgroundImage) //将要创建的区域设置为网格的大小 矩形rect=新矩形(this.Location.X,this.Location.Y,this.Width,this.Height) //并在整个网格中绘制背景图像的区域,该区域被我的网格覆盖,形成“透明”效果 graphics.DrawImage(parentBackGround.Clone(rect,PixelFormat.Format32bppRgb),gridBounds)C# 获取表单图像背景时出现问题,c#,image,winforms,C#,Image,Winforms,我正在创建一个透明的datagridview //我得到了父母的背景图像 位图parentBackGround=新位图(this.Parent.BackgroundImage) //将要创建的区域设置为网格的大小 矩形rect=新矩形(this.Location.X,this.Location.Y,this.Width,this.Height) //并在整个网格中绘制背景图像的区域,该区域被我的网格覆盖,形成“透明”效果 graphics.DrawImage(parentBackGround.C
当网格父级的背景图像显示在普通布局中时,所有工作正常,但如果布局是拉伸、居中或其他任何形式,透明度效果消失,您有没有想法修复它?为什么不改为尝试使用TransparencyKey属性(表单)?
我创建了一个位图并复制了网格父窗体的背景图像,其大小与网格的大小完全一致,然后仅使用它覆盖网格的部分 我从网格继承并重写这些方法:
protected override void PaintBackground(Graphics graphics, Rectangle clipBounds, Rectangle gridBounds)
{
base.PaintBackground(graphics, clipBounds, gridBounds);
if (DesignMode) return;
Control tmpParent = Parent;
int locationX = this.Location.X;
int locationY = this.Location.Y;
while (tmpParent.BackgroundImage == null)
{
locationX += tmpParent.Location.X;
locationY += tmpParent.Location.Y;
tmpParent = tmpParent.Parent;
}
Rectangle rectSource = new Rectangle(locationX, locationY, this.Width, this.Height);
Rectangle rectDest = new Rectangle(0, 0, rectSource.Width, rectSource.Height);
Bitmap b = new Bitmap(tmpParent.ClientRectangle.Width, tmpParent.ClientRectangle.Height);
Graphics.FromImage(b).DrawImage(tmpParent.BackgroundImage, tmpParent.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;
}
}
因为我想使datagridview无it父窗体透明,所以您可以将transparencyKey设置为任何颜色,比如说粉红色,那么窗体上的任何颜色属性及其控件设置为粉红色都将是透明的是啊,但是粉红色控件将非常“透明”,我的意思是,这就像在网格区域中创建一个洞一样