GlowDrawingContext.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using HandyControl.Tools.Interop;
  3. namespace HandyControl.Data;
  4. internal class GlowDrawingContext : DisposableObject
  5. {
  6. private readonly GlowBitmap _windowBitmap;
  7. internal InteropValues.BLENDFUNCTION Blend;
  8. internal GlowDrawingContext(int width, int height)
  9. {
  10. ScreenDC = InteropMethods.GetDC(IntPtr.Zero);
  11. if (ScreenDC == IntPtr.Zero) return;
  12. WindowDC = InteropMethods.CreateCompatibleDC(ScreenDC);
  13. if (WindowDC == IntPtr.Zero) return;
  14. BackgroundDC = InteropMethods.CreateCompatibleDC(ScreenDC);
  15. if (BackgroundDC == IntPtr.Zero) return;
  16. Blend.BlendOp = 0;
  17. Blend.BlendFlags = 0;
  18. Blend.SourceConstantAlpha = 255;
  19. Blend.AlphaFormat = 1;
  20. _windowBitmap = new GlowBitmap(ScreenDC, width, height);
  21. InteropMethods.SelectObject(WindowDC, _windowBitmap.Handle);
  22. }
  23. internal bool IsInitialized =>
  24. ScreenDC != IntPtr.Zero && WindowDC != IntPtr.Zero &&
  25. BackgroundDC != IntPtr.Zero && _windowBitmap != null;
  26. internal IntPtr ScreenDC { get; }
  27. internal IntPtr WindowDC { get; }
  28. internal IntPtr BackgroundDC { get; }
  29. internal int Width => _windowBitmap.Width;
  30. internal int Height => _windowBitmap.Height;
  31. protected override void DisposeManagedResources() => _windowBitmap.Dispose();
  32. protected override void DisposeNativeResources()
  33. {
  34. if (ScreenDC != IntPtr.Zero) InteropMethods.ReleaseDC(IntPtr.Zero, ScreenDC);
  35. if (WindowDC != IntPtr.Zero) InteropMethods.DeleteDC(WindowDC);
  36. if (BackgroundDC != IntPtr.Zero) InteropMethods.DeleteDC(BackgroundDC);
  37. }
  38. }