/// <summary> /// Sets the bitmap to use for this form. /// </summary> private void SetBitmap( Bitmap bitmap ) { // Collect some variables we'll use to render. IntPtr screenDc = GetDC( IntPtr.Zero ); IntPtr memDc = CreateCompatibleDC( screenDc ); IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; try { hBitmap = bitmap.GetHbitmap( Color.FromArgb(0) ); oldBitmap = SelectObject( memDc, hBitmap ); W32Size size = new W32Size( bitmap.Width, bitmap.Height ); W32Point pointSource = new W32Point( 0, 0 ); W32Point topPos = new W32Point( this.Left, this.Top ); BLENDFUNCTION blend = new BLENDFUNCTION(); blend.BlendOp = 0x00; // AC_SRC_OVER blend.BlendFlags = 0; blend.SourceConstantAlpha = 255; blend.AlphaFormat = 0x01; // AC_SRC_ALPHA lock( this ) if( !Disposing ) UpdateLayeredWindow( Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, (Int32) 0x00000002 // ULW_ALPHA ); } catch { /* Ignore errors */ } finally { // Clean things up... ReleaseDC( IntPtr.Zero, screenDc ); if( hBitmap != IntPtr.Zero ) { SelectObject( memDc, oldBitmap ); DeleteObject( hBitmap ); } DeleteDC( memDc ); } }
private static extern W32Bool UpdateLayeredWindow( IntPtr hwnd, IntPtr hdcDst, ref W32Point pptDst, ref W32Size psize, IntPtr hdcSrc, ref W32Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags );