Groups | Blog | Home
all groups > dotnet drawing api > april 2007 >

dotnet drawing api : Transparency problems


CH
4/17/2007 8:12:02 AM
Hy,

I have a form with transparent BackColor
(MyForm.TransparencyKey=MyForm.BackColor) and the OnPaint method draws
a rectangle with a GradientBrush the gradient is from Color.Black to
Color.Transparent.

The result seems to be a gradient from Color.Black to
SystemColors.Control instead of from Color.Black to whatever is behind
my form.

Why is that?
Any idea how to accomplish the desired effect?

Thanks


///
///
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace MyNameSpace
{
public partial class AboutForm : Form
{
private GraphicsPath shadowPath;
private PathGradientBrush shadow;
bool shrink;
public AboutForm()
{

InitializeComponent();

//this.Owner.Deactivate += new
EventHandler(Owner_Deactivate);
Rectangle myRect = new Rectangle(0, 0, this.Width,
this.Height);
this.RightToLeftLayout = false;
this.TransparencyKey = this.BackColor;



shadowPath = new GraphicsPath();

shadowPath.StartFigure();
shadowPath.AddLine(10, 10, 10, myRect.Height);
shadowPath.AddLine(10, myRect.Height, myRect.Width,
myRect.Height);
shadowPath.AddLine(myRect.Width, myRect.Height,
myRect.Width, 10);
shadowPath.CloseFigure();

shadow = new PathGradientBrush(shadowPath);
shadow.CenterColor = Color.Black;
shadow.CenterPoint = new PointF(myRect.Width / 2 + 30,
myRect.Height / 2);
shadow.FocusScales = new PointF(0.9F, 0.9F);


shadow.SurroundColors = new Color[] { Color.Transparent,
Color.Transparent, Color.Transparent, Color.Transparent };
shrink = false;
}

protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Rectangle myRect = new Rectangle(0, 0, this.Width,
this.Height);


Rectangle shadowRect = myRect;
Rectangle contentsRect = new Rectangle(0, 0, myRect.Width
- 10, myRect.Height - 10);

e.Graphics.FillPath(shadow,shadowPath);
e.Graphics.FillRectangle(Brushes.SkyBlue, contentsRect);
e.Graphics.DrawIcon (this.FindForm().Icon,new
Rectangle(10,30,60,60));
e.Graphics.DrawString("About Form", this.Font,
Brushes.Black, new PointF(myRect.X + 10, myRect.Y + 10));
}

protected override void OnClick(EventArgs e)
{
this.Close();
}
}
}
Bob Powell [MVP]
4/19/2007 12:00:00 AM
The only way to implement this is using the LayeredWindow API and the
per-pixel-alpha option. The transparent form mechanism uses the same API
but with the single alpha option instead.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





[quoted text, click to view]
AddThis Social Bookmark Button