Code:
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (!transparency) { return; }
foreach (Pawn p in Game.pawns)
{
if (!p.isDocked())
p.pic.Invalidate();
}
}
I used a code like this. Which just enables me to set "docked" transparent panels. If it is docked it is like a background transparent panel.
So, for further use, this is a class I suggest for a "transparent moving panel".
Docked is a panel that you want to be set to the background. The transparency variable is used to set the whole feautre On/Off. If the TPanels are not docked then there can be troubles on their z-order evaluation, but by docking you should be able to have only one un-docked panel.
Finaly, the resize Image is used in order to make the BackgroundImage the appropriate size of the panel so you can paint it with the DrawImage() method, since the Stretch layout won't work here.
Code:
public class TPanel : Panel
{
public bool transparency;
public bool docked;
public TPanel()
{
transparency = false;
docked = false;
}
protected override CreateParams CreateParams
{
get
{
if (!transparency) return base.CreateParams;
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
return cp;
}
}
void InvalidateEx()
{
if (Parent == null)
return;
Rectangle rc = new Rectangle(this.Location, this.Size);
Parent.Invalidate(rc, true);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
if (!transparency) { base.OnPaintBackground(e); }
}
protected override void OnMove (EventArgs e)
{
base.OnMove(e);
if (!transparency) { return; }
InvalidateEx();
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (!transparency) { return; }
foreach (Panel p in AllYourPanels)
{
if (!p.docked)
p.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
if (!transparency) { base.OnPaint(e); return; }
if (BackgroundImage != null)
e.Graphics.DrawImage(BackgroundImage, 0, 0);
else
e.Graphics.FillRectangle(new SolidBrush(BackColor), 0, 0, Size.Width, Size.Height);
}
public static Image resizeImage(Image imgToResize, Size size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (Image)b;
}