Hi, I've got a graphical application where some child controls are
rendered inside a panel. This controls are user drawn and are
transparent. The transparency works OK, I've been using Bob Powell
tricks (http://www.bobpowell.net/transcontrols.htm). However, there's a
big problem: child controls are drawn BACKWARDS its order.
For example, if I add two controls and then I execute BringToFront() in
one of them, it draws behind the other, but when I click, the mouse
events are fired on it (so it is really in front).
Below is the most important code from the control, the panel where
controls are added is just a simple Panel with a transparent background,
I hope if someone could help me. Thanks in advance.
=== THE CODE ===
public partial class DrawViewer : UserControl
{
public DrawViewer()
{
InitializeComponent();
Initialize();
this.MouseDown += new MouseEventHandler(DrawViewer_MouseDown);
this.MouseUp += new MouseEventHandler(DrawViewer_MouseUp);
this.MouseMove += new MouseEventHandler(DrawViewer_MouseMove);
}
#region This allows to drag the control
bool dragging = false;
int mousex, mousey;
void DrawViewer_MouseUp(object sender, MouseEventArgs e)
{
if (dragging)
{
dragging = false;
mousex = mousey = 0;
}
}
void DrawViewer_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (dragging)
{
Point MPosition =
(ScrollableControl)this.Parent).PointToClient(MousePosition);
MPosition.Offset(mousex, mousey);
this.Location = MPosition;
this.InvalidateEx();
}
}
else
dragging = false;
}
void DrawViewer_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
dragging = true;
Point MPosition =
((ScrollableControl)this.Parent).PointToClient(MousePosition);
mousex = -(MPosition.X - Location.X);
mousey = -(MPosition.Y - Location.Y);
}
}
#endregion
protected override OnPaint (PaintEventArgs e)
{
// This just paints
}
#region This is for supporting transparency
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
// Nothing
}
protected override void OnMove(EventArgs e)
{
InvalidateEx();
}
protected override void OnResize(EventArgs e)
{
InvalidateEx();
}
public void InvalidateEx()
{
try
{
if (Parent != null)
return;
Rectangle rc = new Rectangle(this.Location, this.Size);
Parent.Invalidate(rc, true);
}
catch { }
}
public void Initialize()
{
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.SupportsTransparentBackColor, true);
// More internal initialization
}
public override void Refresh()
{
base.Refresh();
InvalidateEx();
}
#endregion