3 using System
.Drawing
.Imaging
;
4 using System
.Runtime
.InteropServices
;
5 using System
.Windows
.Forms
;
9 // This only works with Windows 2000/XP
12 public Splash(Bitmap bitmap
)
16 this.ShowInTaskbar
= false;
17 this.Size
= bitmap
.Size
;
18 this.StartPosition
= System
.Windows
.Forms
.FormStartPosition
.CenterScreen
;
20 // Must be called before setting bitmap
21 this.SelectBitmap(bitmap
);
22 this.BackColor
= Color
.Red
;
23 this.Click
+= new EventHandler(Splash_Click
);
24 this.MouseClick
+= new MouseEventHandler(Splash_MouseClick
);
27 void Splash_MouseClick(object sender
, MouseEventArgs e
)
32 void Splash_Click(object sender
, EventArgs e
)
37 // Sets the current bitmap
38 public void SelectBitmap(Bitmap bitmap
)
40 // Does this bitmap contain an alpha channel?
41 if (bitmap
.PixelFormat
!= PixelFormat
.Format32bppArgb
)
43 throw new ApplicationException("The bitmap must be 32bpp with alpha-channel.");
46 // Get device contexts
47 IntPtr screenDc
= APIHelp
.GetDC(IntPtr
.Zero
);
48 IntPtr memDc
= APIHelp
.CreateCompatibleDC(screenDc
);
49 IntPtr hBitmap
= IntPtr
.Zero
;
50 IntPtr hOldBitmap
= IntPtr
.Zero
;
54 // Get handle to the new bitmap and select it into the current device context
55 hBitmap
= bitmap
.GetHbitmap(Color
.FromArgb(0));
56 hOldBitmap
= APIHelp
.SelectObject(memDc
, hBitmap
);
58 // Set parameters for layered window update
59 APIHelp
.Size newSize
= new APIHelp
.Size(bitmap
.Width
, bitmap
.Height
); // Size window to match bitmap
60 APIHelp
.Point sourceLocation
= new APIHelp
.Point(0, 0);
61 APIHelp
.Point newLocation
= new APIHelp
.Point(this.Left
, this.Top
); // Same as this window
62 APIHelp
.BLENDFUNCTION blend
= new APIHelp
.BLENDFUNCTION();
63 blend
.BlendOp
= APIHelp
.AC_SRC_OVER
; // Only works with a 32bpp bitmap
64 blend
.BlendFlags
= 0; // Always 0
65 blend
.SourceConstantAlpha
= 255; // Set to 255 for per-pixel alpha values
66 blend
.AlphaFormat
= APIHelp
.AC_SRC_ALPHA
; // Only works when the bitmap contains an alpha channel
69 APIHelp
.UpdateLayeredWindow(Handle
, screenDc
, ref newLocation
, ref newSize
,
70 memDc
, ref sourceLocation
, 0, ref blend
, APIHelp
.ULW_ALPHA
);
74 // Release device context
75 APIHelp
.ReleaseDC(IntPtr
.Zero
, screenDc
);
76 if (hBitmap
!= IntPtr
.Zero
)
78 APIHelp
.SelectObject(memDc
, hOldBitmap
);
79 APIHelp
.DeleteObject(hBitmap
); // Remove bitmap resources
81 APIHelp
.DeleteDC(memDc
);
85 protected override CreateParams CreateParams
89 // Add the layered extended style (WS_EX_LAYERED) to this window
90 CreateParams createParams
= base.CreateParams
;
91 createParams
.ExStyle
|= APIHelp
.WS_EX_LAYERED
;
96 // Let Windows drag this window for us (thinks its hitting the title bar of the window)
97 protected override void WndProc(ref Message message
)
99 if (message
.Msg
== APIHelp
.WM_NCHITTEST
)
101 // Tell Windows that the user is on the title bar (caption)
102 message
.Result
= (IntPtr
)APIHelp
.HTCAPTION
;
106 base.WndProc(ref message
);
111 // Class to assist with Win32 API calls
114 // Required constants
115 public const Int32 WS_EX_LAYERED
= 0x80000;
116 public const Int32 HTCAPTION
= 0x02;
117 public const Int32 WM_NCHITTEST
= 0x84;
118 public const Int32 ULW_ALPHA
= 0x02;
119 public const byte AC_SRC_OVER
= 0x00;
120 public const byte AC_SRC_ALPHA
= 0x01;
128 [StructLayout(LayoutKind
.Sequential
)]
134 public Point(Int32 x
, Int32 y
) { this.x = x; this.y = y; }
137 [StructLayout(LayoutKind
.Sequential
)]
143 public Size(Int32 cx
, Int32 cy
) { this.cx = cx; this.cy = cy; }
146 [StructLayout(LayoutKind
.Sequential
, Pack
= 1)]
155 [StructLayout(LayoutKind
.Sequential
, Pack
= 1)]
156 public struct BLENDFUNCTION
159 public byte BlendFlags
;
160 public byte SourceConstantAlpha
;
161 public byte AlphaFormat
;
164 [DllImport("user32.dll", ExactSpelling
= true, SetLastError
= true)]
165 public static extern Bool
UpdateLayeredWindow(IntPtr hwnd
, IntPtr hdcDst
, ref Point pptDst
, ref Size psize
, IntPtr hdcSrc
, ref Point pprSrc
, Int32 crKey
, ref BLENDFUNCTION pblend
, Int32 dwFlags
);
167 [DllImport("gdi32.dll", ExactSpelling
= true, SetLastError
= true)]
168 public static extern IntPtr
CreateCompatibleDC(IntPtr hDC
);
170 [DllImport("user32.dll", ExactSpelling
= true, SetLastError
= true)]
171 public static extern IntPtr
GetDC(IntPtr hWnd
);
173 [DllImport("user32.dll", ExactSpelling
= true)]
174 public static extern int ReleaseDC(IntPtr hWnd
, IntPtr hDC
);
176 [DllImport("gdi32.dll", ExactSpelling
= true, SetLastError
= true)]
177 public static extern Bool
DeleteDC(IntPtr hdc
);
179 [DllImport("gdi32.dll", ExactSpelling
= true)]
180 public static extern IntPtr
SelectObject(IntPtr hDC
, IntPtr hObject
);
182 [DllImport("gdi32.dll", ExactSpelling
= true, SetLastError
= true)]
183 public static extern Bool
DeleteObject(IntPtr hObject
);