另外给一个曲线救国的效果很好的方法,抓图,吧当前窗体抓图,然后赋值给Panel作为背景,吧Panel添加到窗体,并设置z-order为顶层,dock = fill即可。private void button2_Click(object sender, EventArgs e) { Panel p = new Panel(); p.Dock = DockStyle.Fill; Control frm = this; p.BackgroundImage = CaptureImage(ref frm); this.Controls.Add(p); p.BringToFront(); } public Bitmap CaptureImage(ref Control c) { int hDC; int sh; int sw; if (c == null) { hDC = GetDC(0); sw = Screen.PrimaryScreen.Bounds.Width; sh = Screen.PrimaryScreen.Bounds.Height; } else { hDC = GetDC((int)c.Handle); sw = c.Width; sh = c.Height; } int hMDC = CreateCompatibleDC(hDC); int hBMP = CreateCompatibleBitmap(hDC, sw, sh); int hBMPOld = SelectObject(hMDC, hBMP); BitBlt(hMDC, 0, 0, sw, sh, hDC, 0, 0, 0xcc0020); hBMP = SelectObject(hMDC, hBMPOld); Bitmap result = Image.FromHbitmap(new IntPtr(hBMP)); DeleteDC(hDC); DeleteDC(hMDC); DeleteObject(hBMP); return result; } [DllImport("gdi32.dll", EntryPoint = "CreateCompatibleBitmap")] public static extern int CreateCompatibleBitmap(int hdc, int nWidth, int nHeight); [DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC")] public static extern int CreateCompatibleDC(int hdc); [DllImport("user32.dll", EntryPoint = "GetDC")] public static extern int GetDC(int hwnd); [DllImport("gdi32.dll", EntryPoint = "DeleteDC")] public static extern int DeleteDC(int hdc); [DllImport("gdi32.dll", EntryPoint = "SelectObject")] public static extern int SelectObject(int hdc, int hObject); [DllImport("gdi32.dll", EntryPoint = "DeleteObject")] public static extern int DeleteObject(int hObject); [DllImport("gdi32.dll", EntryPoint = "BitBlt")] public static extern int BitBlt(int hDestDC, int x, int y, int nWidth, int nHeight, int hSrcDC, int xSrc, int ySrc, int dwRop);
TransParent
{
Panel p = new Panel();
p.Dock = DockStyle.Fill;
Control frm = this;
p.BackgroundImage = CaptureImage(ref frm);
this.Controls.Add(p);
p.BringToFront();
}
public Bitmap CaptureImage(ref Control c)
{
int hDC;
int sh;
int sw;
if (c == null)
{
hDC = GetDC(0);
sw = Screen.PrimaryScreen.Bounds.Width;
sh = Screen.PrimaryScreen.Bounds.Height;
}
else
{
hDC = GetDC((int)c.Handle);
sw = c.Width;
sh = c.Height;
}
int hMDC = CreateCompatibleDC(hDC);
int hBMP = CreateCompatibleBitmap(hDC, sw, sh);
int hBMPOld = SelectObject(hMDC, hBMP);
BitBlt(hMDC, 0, 0, sw, sh, hDC, 0, 0, 0xcc0020);
hBMP = SelectObject(hMDC, hBMPOld);
Bitmap result = Image.FromHbitmap(new IntPtr(hBMP));
DeleteDC(hDC);
DeleteDC(hMDC);
DeleteObject(hBMP);
return result;
}
[DllImport("gdi32.dll", EntryPoint = "CreateCompatibleBitmap")]
public static extern int CreateCompatibleBitmap(int hdc, int nWidth, int nHeight);
[DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC")]
public static extern int CreateCompatibleDC(int hdc);
[DllImport("user32.dll", EntryPoint = "GetDC")]
public static extern int GetDC(int hwnd);
[DllImport("gdi32.dll", EntryPoint = "DeleteDC")]
public static extern int DeleteDC(int hdc);
[DllImport("gdi32.dll", EntryPoint = "SelectObject")]
public static extern int SelectObject(int hdc, int hObject);
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
public static extern int DeleteObject(int hObject);
[DllImport("gdi32.dll", EntryPoint = "BitBlt")]
public static extern int BitBlt(int hDestDC, int x, int y, int nWidth, int nHeight, int hSrcDC, int xSrc, int ySrc, int dwRop);
public partial class tspControl : UserControl
{
public tspControl()
{
InitializeComponent();
SetStyle(ControlStyles.SupportsTransparentBackColor
| ControlStyles.UserPaint
| ControlStyles.AllPaintingInWmPaint
| ControlStyles.Opaque, true);
this.BackColor = Color.Transparent;
} private Image img;
public Image Image
{
get
{
return img;
}
set
{
img = value;
}
} protected override void OnLocationChanged(EventArgs e)
{
//base.OnLocationChanged(e);
Visible = false;
Visible = true;
} protected override CreateParams CreateParams
{
get
{
//return base.CreateParams;
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
return cp;
}
}
protected override void OnPaint(PaintEventArgs pe)
{
if (img != null)
{
base.OnPaint(pe);
pe.Graphics.DrawImage(img, 0, 0);
}
else
{ }
}
}