2 using System.Drawing;
\r
3 using System.Windows.Forms;
\r
5 using System.Runtime.InteropServices;
\r
7 namespace AppliStation.Util
\r
9 public class DisplayDeviceContext : IDeviceContext, IDisposable
\r
11 private HandleRef hWnd;
\r
13 private bool isGottenDc;
\r
18 /// <param name="hWnd">ウィンドウハンドル</param>
\r
19 public DisplayDeviceContext(IntPtr hWnd)
\r
20 : this(new HandleRef(null, hWnd))
\r
27 /// <param name="hWnd">ウィンドウハンドル</param>
\r
28 public DisplayDeviceContext(HandleRef hWnd)
\r
31 this.hDc = GetDC(hWnd);
\r
32 this.isGottenDc = true;
\r
35 protected DisplayDeviceContext(IntPtr hDc, bool isGottenDc)
\r
38 this.isGottenDc = isGottenDc;
\r
41 public static DisplayDeviceContext FromHdc(IntPtr hDc)
\r
43 return new DisplayDeviceContext(hDc, false);
\r
46 public DisplayDeviceContext CreateCompatibleDisplayDeviceContext()
\r
48 return FromHdc(CreateCompatibleDC(hDc));
\r
51 public IntPtr GetHdc()
\r
56 public void ReleaseHdc()
\r
58 if (this.isGottenDc) { // GetDC 関数を使って取得した場合
\r
59 ReleaseDC(hWnd, hDc);
\r
65 public void Dispose()
\r
71 /// 指定されたデバイスコンテキストのオブジェクトを選択
\r
74 /// context.SelectObject(bitmap.GetHbitmap(Color.FromArgb(0)));
\r
75 /// </code></example>
\r
76 /// <param name="hGdiObj">選択するオブジェクトのハンドル</param>
\r
77 /// <returns>置き換えが発生する前のオブジェクトのハンドル</returns>
\r
78 public IntPtr SelectObject(IntPtr hGdiObj)
\r
80 return SelectObject(hDc, hGdiObj);
\r
85 [DllImport("user32.dll")]
\r
86 private static extern IntPtr GetDC(HandleRef hWnd);
\r
88 [DllImport("user32.dll")]
\r
89 private static extern int ReleaseDC(HandleRef hWnd, IntPtr hDC);
\r
91 [DllImport("gdi32.dll", SetLastError=true)]
\r
92 private static extern IntPtr CreateCompatibleDC(IntPtr hdc);
\r
94 [DllImport("gdi32.dll", ExactSpelling=true, PreserveSig=true, SetLastError=true)]
\r
95 private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
\r
97 [DllImport("gdi32.dll")]
\r
98 private static extern bool DeleteObject(IntPtr hObject);
\r
103 public class SprashScreenLayered : Form
\r
105 public static Form CreateSprashScreenLayered(Bitmap bitmap, Color bgColor)
\r
109 if (OSFeature.Feature.IsPresent(OSFeature.LayeredWindows)) {
\r
110 form = new SprashScreenLayered();
\r
111 form.BackgroundImage = bitmap;
\r
114 form.BackgroundImage = bitmap;
\r
115 form.BackColor = bgColor;
\r
117 //form.ShowInTaskbar = false;
\r
118 form.ControlBox = false;
\r
119 form.FormBorderStyle = FormBorderStyle.None;
\r
120 form.StartPosition = FormStartPosition.CenterScreen;
\r
121 form.Size = bitmap.Size;
\r
123 if (form is SprashScreenLayered) {
\r
124 ((SprashScreenLayered) form).CenterToScreen2();
\r
130 public SprashScreenLayered()
\r
134 protected internal void CenterToScreen2()
\r
136 base.CenterToScreen();
\r
139 protected override CreateParams CreateParams {
\r
141 CreateParams param = base.CreateParams;
\r
142 // param.ExStyle |= WS_EX_LAYERED;
\r
143 param.ExStyle |= 0x00080000;
\r
148 public override Image BackgroundImage {
\r
150 base.BackgroundImage = value;
\r
151 updateLayeredWindow();
\r
155 #region UpdateLayeredWindow関連
\r
157 [DllImport("user32.dll", ExactSpelling=true, SetLastError=true)]
\r
158 private static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst,
\r
159 [In] ref Point pptDst, [In] ref Size psize,
\r
160 IntPtr hdcSrc, [In] ref Point pptSrc,
\r
162 [In] ref BLENDFUNCTION pblend,
\r
165 private Bitmap convertBitmap(Image img)
\r
167 if (img is Bitmap) {
\r
168 return (Bitmap) img;
\r
170 Bitmap bitmap = new Bitmap(img.Width, img.Height, img.PixelFormat);
\r
171 Graphics.FromImage(bitmap).DrawImage(img, 0, 0);
\r
176 protected void updateLayeredWindow()
\r
178 Point ptDest = this.Location;
\r
179 Point ptSrc = Point.Empty;
\r
180 Size size = BackgroundImage.Size;
\r
182 BLENDFUNCTION blend = new BLENDFUNCTION();
\r
183 blend.BlendOp = 0; // AC_SRC_OVER
\r
184 blend.BlendFlags = 0;
\r
185 blend.SourceConstantAlpha = 255;
\r
186 blend.AlphaFormat = 1; // AC_SRC_ALPHA
\r
188 using(DisplayDeviceContext screen = new DisplayDeviceContext(IntPtr.Zero))
\r
189 using(DisplayDeviceContext mem = screen.CreateCompatibleDisplayDeviceContext())
\r
190 using(DisplayDeviceContext hBitmap = DisplayDeviceContext.FromHdc(
\r
191 convertBitmap(BackgroundImage).GetHbitmap(Color.FromArgb(0))
\r
193 IntPtr hOldBitmap = IntPtr.Zero;
\r
196 hOldBitmap = mem.SelectObject(hBitmap.GetHdc());
\r
198 UpdateLayeredWindow(this.Handle, screen.GetHdc(),
\r
199 ref ptSrc, ref size,
\r
200 mem.GetHdc(), ref ptDest,
\r
203 2 /* ULW_ALPHA */);
\r
205 if (hBitmap.GetHdc() != IntPtr.Zero) {
\r
206 mem.SelectObject(hOldBitmap);
\r
212 [StructLayout(LayoutKind.Sequential, Pack=1)]
\r
213 protected struct BLENDFUNCTION
\r
215 public byte BlendOp;
\r
216 public byte BlendFlags;
\r
217 public byte SourceConstantAlpha;
\r
218 public byte AlphaFormat;
\r