|
| 1 | +using System; |
| 2 | +using System.Drawing; |
| 3 | +using System.Drawing.Drawing2D; |
| 4 | +using System.Drawing.Imaging; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | + |
| 7 | +namespace Loupe |
| 8 | +{ |
| 9 | + /// <summary>A top-down 32-bit premultiplied-alpha bitmap selected into a memory DC.</summary> |
| 10 | + sealed class DibSurface : IDisposable |
| 11 | + { |
| 12 | + public readonly int Width; |
| 13 | + public readonly int Height; |
| 14 | + public IntPtr DC { get; private set; } |
| 15 | + public IntPtr Bits { get; private set; } |
| 16 | + |
| 17 | + IntPtr bitmap, previousBitmap; |
| 18 | + DibSurface scratch; // receives scaled frames during the appear / disappear animation |
| 19 | + |
| 20 | + public DibSurface(int width, int height) |
| 21 | + { |
| 22 | + Width = width; |
| 23 | + Height = height; |
| 24 | + |
| 25 | + var info = new Native.BITMAPINFOHEADER |
| 26 | + { |
| 27 | + biSize = Marshal.SizeOf(typeof(Native.BITMAPINFOHEADER)), |
| 28 | + biWidth = width, |
| 29 | + biHeight = -height, // top-down |
| 30 | + biPlanes = 1, |
| 31 | + biBitCount = 32, |
| 32 | + }; |
| 33 | + |
| 34 | + IntPtr screenDC = Native.GetDC(IntPtr.Zero); |
| 35 | + DC = Native.CreateCompatibleDC(screenDC); |
| 36 | + IntPtr bits; |
| 37 | + bitmap = Native.CreateDIBSection(screenDC, ref info, 0, out bits, IntPtr.Zero, 0); |
| 38 | + Bits = bits; |
| 39 | + previousBitmap = Native.SelectObject(DC, bitmap); |
| 40 | + Native.ReleaseDC(IntPtr.Zero, screenDC); |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Shows this surface in a per-pixel-alpha layered window at <paramref name="position"/>, |
| 45 | + /// scaled around its centre and faded according to the animation's appearance. |
| 46 | + /// </summary> |
| 47 | + public void Present(IntPtr hwnd, Point position, float appearance) |
| 48 | + { |
| 49 | + IntPtr source = DC; |
| 50 | + if (appearance < 0.999f) |
| 51 | + { |
| 52 | + if (scratch == null) scratch = new DibSurface(Width, Height); |
| 53 | + DrawScaled(scratch, LensAnimation.ScaleFor(appearance)); |
| 54 | + source = scratch.DC; |
| 55 | + } |
| 56 | + |
| 57 | + var dst = new Native.POINT { X = position.X, Y = position.Y }; |
| 58 | + var size = new Native.SIZE { cx = Width, cy = Height }; |
| 59 | + var origin = new Native.POINT(); |
| 60 | + var blend = new Native.BLENDFUNCTION |
| 61 | + { |
| 62 | + BlendOp = Native.AC_SRC_OVER, |
| 63 | + SourceConstantAlpha = (byte)Math.Round(255 * Math.Max(0f, Math.Min(1f, appearance))), |
| 64 | + AlphaFormat = Native.AC_SRC_ALPHA, |
| 65 | + }; |
| 66 | + Native.UpdateLayeredWindow(hwnd, IntPtr.Zero, ref dst, ref size, source, ref origin, 0, |
| 67 | + ref blend, Native.ULW_ALPHA); |
| 68 | + } |
| 69 | + |
| 70 | + void DrawScaled(DibSurface target, float scale) |
| 71 | + { |
| 72 | + using (var src = Wrap()) |
| 73 | + using (var dst = target.Wrap()) |
| 74 | + using (var g = Graphics.FromImage(dst)) |
| 75 | + { |
| 76 | + g.Clear(Color.Transparent); |
| 77 | + g.InterpolationMode = InterpolationMode.Bilinear; |
| 78 | + g.PixelOffsetMode = PixelOffsetMode.Half; |
| 79 | + |
| 80 | + float w = Width * scale, h = Height * scale; |
| 81 | + g.DrawImage(src, new RectangleF((Width - w) / 2, (Height - h) / 2, w, h)); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + Bitmap Wrap() |
| 86 | + { |
| 87 | + return new Bitmap(Width, Height, Width * 4, PixelFormat.Format32bppPArgb, Bits); |
| 88 | + } |
| 89 | + |
| 90 | + public void Dispose() |
| 91 | + { |
| 92 | + if (scratch != null) scratch.Dispose(); |
| 93 | + scratch = null; |
| 94 | + if (DC == IntPtr.Zero) return; |
| 95 | + Native.SelectObject(DC, previousBitmap); |
| 96 | + Native.DeleteObject(bitmap); |
| 97 | + Native.DeleteDC(DC); |
| 98 | + DC = Bits = bitmap = previousBitmap = IntPtr.Zero; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments