我有个窗口Form1,然后里面镶嵌了另一个窗口Form2
我想让Form2的背景透明,来显示Form1的背景。
怎么做呢?
我设置了Form1的透明过滤。
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255)));
this.TransparencyKey = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255)));
然后把Form1的背景设为白色。
虽然Form2是透明了,但是也看不到Form1的背景图了。请问为什么?
我想让Form2的背景透明,来显示Form1的背景。
怎么做呢?
我设置了Form1的透明过滤。
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255)));
this.TransparencyKey = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(255)));
然后把Form1的背景设为白色。
虽然Form2是透明了,但是也看不到Form1的背景图了。请问为什么?
它就能受Form1的opacity的影响,可是也会导致了From2的别的控件都透明了。
这样不行
{
if (opacity < 0.0 || opacity > 1.0)
{
throw new ArgumentOutOfRangeException("opacity", "must be in the range [0, 1]");
} if (System.Environment.OSVersion.Version >= OS.WindowsXP)
{
uint exStyle = SafeNativeMethods.GetWindowLongW(form.Handle, NativeConstants.GWL_EXSTYLE); byte bOldAlpha = 255; if ((exStyle & NativeConstants.GWL_EXSTYLE) != 0)
{
uint dwOldKey;
uint dwOldFlags;
bool result = SafeNativeMethods.GetLayeredWindowAttributes(form.Handle, out dwOldKey, out bOldAlpha, out dwOldFlags);
} byte bNewAlpha = (byte)(opacity * 255.0);
uint newExStyle = exStyle; if (bNewAlpha != 255)
{
newExStyle |= NativeConstants.WS_EX_LAYERED;
} if (newExStyle != exStyle || (newExStyle & NativeConstants.WS_EX_LAYERED) != 0)
{
if (newExStyle != exStyle)
{
SafeNativeMethods.SetWindowLongW(form.Handle, NativeConstants.GWL_EXSTYLE, newExStyle);
} if ((newExStyle & NativeConstants.WS_EX_LAYERED) != 0)
{
SafeNativeMethods.SetLayeredWindowAttributes(form.Handle, 0, bNewAlpha, NativeConstants.LWA_ALPHA);
}
} GC.KeepAlive(form);
}
else
{
form.Opacity = opacity;
}
}这个是paint.net的代码SafeNativeMethods类,是一个用来做win32API的类,你可以参考一下,最好自己去下他的代码研究下。有很多图形方面的东西可以借鉴
我看到函数Clear,只能清除全部的:
PictureBox box;
Bitmap bm = (Bitmap)box.Image;
Graphics g = Graphics.FromImage(bm);
g.Clear(Color.Transparent);