BarrageCurtainWindow.xaml.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Windows;
  4. using System.Windows.Interop;
  5. namespace BarrageTransporter
  6. {
  7. /// <summary>
  8. /// BarrageCurtainWindow.xaml 的交互逻辑
  9. /// </summary>
  10. public partial class BarrageCurtainWindow : Window
  11. {
  12. public double screenHeight = SystemParameters.PrimaryScreenHeight;
  13. public double screenWidth = SystemParameters.PrimaryScreenWidth;
  14. private BarrageManager barrageManager = null;
  15. public BarrageCurtainWindow()
  16. {
  17. InitializeComponent();
  18. Top = 0;
  19. Left = 0;
  20. Width = screenWidth;
  21. Height = screenHeight;
  22. }
  23. /// <summary>
  24. /// Window penetration
  25. /// </summary>
  26. protected override void OnSourceInitialized(EventArgs e)
  27. {
  28. base.OnSourceInitialized(e);
  29. var hwnd = new WindowInteropHelper(this).Handle;
  30. WindowsServices.SetWindowExTransparent(hwnd);
  31. }
  32. public void Shoot(string text)
  33. {
  34. if (barrageManager == null)
  35. {
  36. barrageManager = new BarrageManager(curtain);
  37. }
  38. barrageManager.Shoot(text);
  39. }
  40. public int BarrageCount
  41. {
  42. get
  43. {
  44. if (barrageManager == null)
  45. {
  46. return 0;
  47. }
  48. else
  49. {
  50. return barrageManager.BarrageCount;
  51. }
  52. }
  53. }
  54. }
  55. /// <summary>
  56. /// Window penetration
  57. /// </summary>
  58. public static class WindowsServices
  59. {
  60. const int WS_EX_TRANSPARENT = 0x00000020;
  61. const int GWL_EXSTYLE = (-20);
  62. [DllImport("user32.dll")]
  63. static extern int GetWindowLong(IntPtr hwnd, int index);
  64. [DllImport("user32.dll")]
  65. static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
  66. public static void SetWindowExTransparent(IntPtr hwnd)
  67. {
  68. var extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
  69. SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
  70. }
  71. }
  72. }