added samples
[windows-sources.git] / sdk / samples / WPFSamples / StickyNotes / csharp / form.cs
blobe091fb564bc1ae08dd26a963ab7d4cf6adeb4a61
1 using System;
2 using System.Drawing;
3 using System.Drawing.Imaging;
4 using System.Runtime.InteropServices;
5 using System.Windows.Forms;
7 namespace StickyNotes
9 // This only works with Windows 2000/XP
10 class Splash : Form
12 public Splash(Bitmap bitmap)
14 // Window settings
15 this.TopMost = true;
16 this.ShowInTaskbar = false;
17 this.Size = bitmap.Size;
18 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
19 this.Show();
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)
29 this.Close();
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;
52 try
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
68 // Update the window
69 APIHelp.UpdateLayeredWindow(Handle, screenDc, ref newLocation, ref newSize,
70 memDc, ref sourceLocation, 0, ref blend, APIHelp.ULW_ALPHA);
72 finally
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
87 get
89 // Add the layered extended style (WS_EX_LAYERED) to this window
90 CreateParams createParams = base.CreateParams;
91 createParams.ExStyle |= APIHelp.WS_EX_LAYERED;
92 return createParams;
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;
104 else
106 base.WndProc(ref message);
111 // Class to assist with Win32 API calls
112 class APIHelp
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;
122 public enum Bool
124 False = 0,
125 True = 1
128 [StructLayout(LayoutKind.Sequential)]
129 public struct Point
131 public Int32 x;
132 public Int32 y;
134 public Point(Int32 x, Int32 y) { this.x = x; this.y = y; }
137 [StructLayout(LayoutKind.Sequential)]
138 public struct Size
140 public Int32 cx;
141 public Int32 cy;
143 public Size(Int32 cx, Int32 cy) { this.cx = cx; this.cy = cy; }
146 [StructLayout(LayoutKind.Sequential, Pack = 1)]
147 struct ARGB
149 public byte Blue;
150 public byte Green;
151 public byte Red;
152 public byte Alpha;
155 [StructLayout(LayoutKind.Sequential, Pack = 1)]
156 public struct BLENDFUNCTION
158 public byte BlendOp;
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);