In last preject, a transparent picturebox is needed to meet the requiremnt. If you have tried before, you should know that .net transparent is not a real one, the controll only transparent to it's parent, all the controlls betweens the parent and itself will be ignore, it will be very funny if there are some children controll overlap.
No solution is found after search internet, so I decided write my own controll to implement this function.
The main idea is
1. The controll will override the PictureBox controll。
2. In the OnPaint method, the controll will redraw all the children controlls of it's parent which are below it and have overlap part.
3. In the OnPaint method, the controll will call Invalidate method of all the children controlls of it's parent which are above it.
Following is the code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace VisualImage
{
public class TransparentPictureBox:PictureBox
{
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
ControlCollection cc = this.Parent.Controls;
if (cc.Count > 1)
{
bool isUp = false;
for (int i = cc.Count - 1; i >= 0; i--)
{
Control c = cc[i];
if (isUp)
{
c.Invalidate();
}
else
{
drawRectangleIntersection(g, (TransparentPictureBox)c);
}
isUp = c == this;
}
}
Bitmap b = new Bitmap(Image);
g.DrawImage(b, 0, 0, b.Width, b.Height);
base.OnPaint(e);
}
protected void drawRectangleIntersection(Graphics g, TransparentPictureBox tpb)
{
Rectangle rectangle1 = new Rectangle(Location.X, Location.Y, Width, Height);
Rectangle rectangle2 = new Rectangle(tpb.Location.X, tpb.Location.Y, tpb.Width, tpb.Height);
if (rectangle1.IntersectsWith(rectangle2))
{
Rectangle rectangle3 = Rectangle.Intersect(rectangle1, rectangle2);
if (!rectangle3.IsEmpty)
{
Rectangle srcRect = new Rectangle(rectangle3.Location.X - tpb.Location.X, rectangle3.Location.Y - tpb.Location.Y, rectangle3.Width, rectangle3.Height);
Rectangle destRect = new Rectangle(Math.Abs(Location.X - rectangle3.Location.X), Math.Abs(Location.Y - rectangle3.Location.Y), rectangle3.Width, rectangle3.Height);
g.DrawImage(tpb.Image, destRect, srcRect, GraphicsUnit.Pixel);
}
}
}
}
}
download url: http://download.csdn.net/source/3469956
No solution is found after search internet, so I decided write my own controll to implement this function.
The main idea is
1. The controll will override the PictureBox controll。
2. In the OnPaint method, the controll will redraw all the children controlls of it's parent which are below it and have overlap part.
3. In the OnPaint method, the controll will call Invalidate method of all the children controlls of it's parent which are above it.
Following is the code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace VisualImage
{
public class TransparentPictureBox:PictureBox
{
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
ControlCollection cc = this.Parent.Controls;
if (cc.Count > 1)
{
bool isUp = false;
for (int i = cc.Count - 1; i >= 0; i--)
{
Control c = cc[i];
if (isUp)
{
c.Invalidate();
}
else
{
drawRectangleIntersection(g, (TransparentPictureBox)c);
}
isUp = c == this;
}
}
Bitmap b = new Bitmap(Image);
g.DrawImage(b, 0, 0, b.Width, b.Height);
base.OnPaint(e);
}
protected void drawRectangleIntersection(Graphics g, TransparentPictureBox tpb)
{
Rectangle rectangle1 = new Rectangle(Location.X, Location.Y, Width, Height);
Rectangle rectangle2 = new Rectangle(tpb.Location.X, tpb.Location.Y, tpb.Width, tpb.Height);
if (rectangle1.IntersectsWith(rectangle2))
{
Rectangle rectangle3 = Rectangle.Intersect(rectangle1, rectangle2);
if (!rectangle3.IsEmpty)
{
Rectangle srcRect = new Rectangle(rectangle3.Location.X - tpb.Location.X, rectangle3.Location.Y - tpb.Location.Y, rectangle3.Width, rectangle3.Height);
Rectangle destRect = new Rectangle(Math.Abs(Location.X - rectangle3.Location.X), Math.Abs(Location.Y - rectangle3.Location.Y), rectangle3.Width, rectangle3.Height);
g.DrawImage(tpb.Image, destRect, srcRect, GraphicsUnit.Pixel);
}
}
}
}
}
download url: http://download.csdn.net/source/3469956