public void SetView(Bitmap newView, int newOpacity) { if (newView == null) return; if (newView.PixelFormat != PixelFormat.Format32bppArgb) throw new ApplicationException("The bitmap must be 32ppp with alpha-channel."); View = newView; Opacity = newOpacity%256; IntPtr screenDc = User.GetDC(IntPtr.Zero); IntPtr memDc = GDI.CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; try { hBitmap = newView.GetHbitmap(Color.FromArgb(0)); oldBitmap = GDI.SelectObject(memDc, hBitmap); var size = new SIZE {cx = newView.Width, cy = newView.Height}; var pointSource = new POINT {x = 0, y = 0}; var topPos = new POINT {x = Left, y = Top}; var blend = new User.BLENDFUNCTION { BlendOp = User.AC_SRC_OVER, BlendFlags = 0, SourceConstantAlpha = (byte) newOpacity, AlphaFormat = User.AC_SRC_ALPHA }; User.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, User.ULW_ALPHA); } finally { User.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { GDI.SelectObject(memDc, oldBitmap); GDI.DeleteObject(hBitmap); } GDI.DeleteDC(memDc); } }
public static extern int SetViewportExtEx(IntPtr hdc, int nX, int nY, ref SIZE lpSize);
public static extern int SetWindowExtEx(IntPtr hdc, int nX, int nY, ref SIZE lpSize);
public static extern int ScaleWindowExtEx(IntPtr hdc, int nXnum, int nXdenom, int nYnum, int nYdenom, ref SIZE lpSize);
public static extern int SetBitmapDimensionEx(IntPtr hbm, int nX, int nY, ref SIZE lpSize);
public static extern int GetViewportExtEx(IntPtr hdc, ref SIZE lpSize);
public static extern int GetWindowExtEx(IntPtr hdc, ref SIZE lpSize);
public static extern int GetTextExtentPoint32(IntPtr hdc, string lpsz, int cbString, ref SIZE lpSize);
public static extern int GetTextExtentExPoint(IntPtr hdc, string lpszStr, int cchString, int nMaxExtent, ref int lpnFit, int alpDx, ref SIZE lpSize);
public static extern int GetBitmapDimensionEx(IntPtr hBitmap, ref SIZE lpDimension);
public static extern int GetAspectRatioFilterEx(IntPtr hdc, ref SIZE lpAspectRatio);
public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref POINT pptDst, ref SIZE psize, IntPtr hdcSrc, ref POINT pptSrc, uint crKey, [In] ref BLENDFUNCTION pblend, uint dwFlags);