我的分层表单出了问题。我用它来显示形状背景模糊的PNG图像。在Windows 7中一切都运行良好,但是当我在Windows XP上运行时,我的表单不会显示。
有我的代码:
public class LayeredForm : Form
{
public LayeredForm()
{
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = false;
StartPosition = FormStartPosition.Manual;
Width = 0;
Height = 0;
DoubleBuffered = true;
}
public void SetBitmap(Bitmap bitmap)
{
IntPtr screenDc = Win32Helper.GetDC(IntPtr.Zero);
IntPtr compatibleMemoryDc = Win32Helper.CreateCompatibleDC(screenDc);
IntPtr hgdiBitmap = IntPtr.Zero;
IntPtr hgdiOldBitmap = IntPtr.Zero;
try
{
hgdiBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
hgdiOldBitmap = Win32Helper.SelectObject(compatibleMemoryDc, hgdiBitmap);
Win32Helper.SizeStruct size = new Win32Helper.SizeStruct()
{
X = bitmap.Width,
Y = bitmap.Height
};
Win32Helper.PointStruct sourcePoint = new Win32Helper.PointStruct();
Win32Helper.PointStruct topPoint = new Win32Helper.PointStruct()
{
X = Left,
Y = Top
};
Win32Helper.BlendFunctionStruct blend = new Win32Helper.BlendFunctionStruct()
{
BlendOp = 255 /* opacity */,
BlendFlags = 0x00 /* AC_SRC_OVER */,
AlphaFormat = 0x01 /* AC_SRC_ALPHA */,
SourceConstantAlpha = byte.MaxValue
};
Win32Helper.UpdateLayeredWindow(this.Handle, screenDc, ref topPoint, ref size, compatibleMemoryDc,
ref sourcePoint, 0, ref blend, 2 /* ULW_ALPHA */);
}
finally
{
if (screenDc != IntPtr.Zero)
{
Win32Helper.ReleaseDC(IntPtr.Zero, screenDc);
}
if (hgdiBitmap != IntPtr.Zero)
{
Win32Helper.SelectObject(compatibleMemoryDc, hgdiOldBitmap);
Win32Helper.DeleteObject(hgdiBitmap);
}
Win32Helper.DeleteDC(compatibleMemoryDc);
}
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00080000; // Turn on WS_EX_LAYERED
return cp;
}
}
}
我通过扩展使用它,并使用SetBitmap()函数设置我的图像。我的表单是空的边框,而不是子窗口(WS_EX_LAYERED不适用于旧窗口的子窗口而不是Windows 8)。