/Urasandesu.NTroll.EnumerateWindowsSample/Window1.xaml.cs

https://github.com/urasandesu/NTroll · C# · 145 lines · 78 code · 10 blank · 57 comment · 5 complexity · eca937aaeacaa2edd1a642ec80bde689 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using Urasandesu.NTroll.PInvoke;
  15. using System.Diagnostics;
  16. using Urasandesu.NAnonym.Linq;
  17. using System.ComponentModel;
  18. using System.Runtime.InteropServices;
  19. using Urasandesu.NTroll.PInvoke.Structures;
  20. using Urasandesu.NTroll.PInvoke.Enums;
  21. using System.Drawing;
  22. using System.Windows.Interop;
  23. using System.Collections.ObjectModel;
  24. namespace Urasandesu.NTroll.EnumerateWindowsSample
  25. {
  26. /// <summary>
  27. /// Window1.xaml の相互作用ロジック
  28. /// </summary>
  29. public partial class Window1 : Window
  30. {
  31. public Window1()
  32. {
  33. InitializeComponent();
  34. }
  35. private void button1_Click(object sender, RoutedEventArgs e)
  36. {
  37. IntPtr window1HWnd = ((HwndSource)HwndSource.FromVisual(this)).Handle;
  38. //var zOrder = new LinkedList<IntPtr>();
  39. //var zOrder = new List<IntPtr>();
  40. bool success =
  41. User32.EnumWindows(
  42. (hWnd, lParam) =>
  43. {
  44. if (!User32.IsWindowVisible(hWnd)) return true;
  45. // MEMO: /(^o^)\ http://stackoverflow.com/questions/295996/is-the-order-in-which-handles-are-returned-by-enumwindows-meaningful
  46. string title = GetWindowTitle(hWnd);
  47. if (!string.IsNullOrEmpty(title))
  48. {
  49. Console.WriteLine("Window: {0}", title);
  50. }
  51. //IntPtr hWndPrev = IntPtr.Zero;
  52. //var hWndNode = default(LinkedListNode<IntPtr>);
  53. //var hWndPrevNode = default(LinkedListNode<IntPtr>);
  54. //if ((hWndPrev = User32.GetWindow(hWnd, GetWindow_Cmd.GW_HWNDPREV)) != IntPtr.Zero)
  55. //{
  56. // if (zOrder.Count == 0)
  57. // {
  58. // hWndNode = zOrder.AddFirst(hWnd);
  59. // zOrder.AddAfter(hWndNode, hWndPrev);
  60. // }
  61. // else
  62. // {
  63. // if ((hWndNode = zOrder.Find(hWnd)) != null)
  64. // {
  65. // if ((hWndPrevNode = zOrder.FindLast(hWndPrev)) != null)
  66. // {
  67. // zOrder.Remove(hWndPrevNode);
  68. // }
  69. // zOrder.AddAfter(hWndNode, hWndPrev);
  70. // }
  71. // else if ((hWndPrevNode = zOrder.Find(hWndPrev)) != null)
  72. // {
  73. // if ((hWndNode = zOrder.FindLast(hWnd)) != null)
  74. // {
  75. // zOrder.Remove(hWndNode);
  76. // }
  77. // zOrder.AddBefore(hWndPrevNode, hWnd);
  78. // }
  79. // else
  80. // {
  81. // hWndNode = zOrder.AddLast(hWnd);
  82. // zOrder.AddAfter(hWndNode, hWndPrev);
  83. // }
  84. // }
  85. //}
  86. //else
  87. //{
  88. // Console.WriteLine("Expected error at GetWindow. {0}", new Win32Exception(Marshal.GetLastWin32Error()).Message);
  89. //}
  90. return true;
  91. },
  92. IntPtr.Zero);
  93. if (!success)
  94. {
  95. Console.WriteLine("Expected error at EnumThreadWindows.");
  96. }
  97. //Console.WriteLine("Z-Order: ");
  98. //zOrder.ForEach(
  99. //hWnd =>
  100. //{
  101. // string title = GetWindowTitle(hWnd);
  102. // if (!string.IsNullOrEmpty(title))
  103. // {
  104. // Console.WriteLine("Window: {0}", title);
  105. // }
  106. //});
  107. }
  108. private void button2_Click(object sender, RoutedEventArgs e)
  109. {
  110. new Window2() { Owner = this, Title = "Hoge" }.Show();
  111. }
  112. string GetWindowTitle(IntPtr hWnd)
  113. {
  114. int length = User32.GetWindowTextLength(hWnd);
  115. if (0 < length)
  116. {
  117. var lpString = new StringBuilder(length + 1);
  118. if (0 < User32.GetWindowText(hWnd, lpString, lpString.Capacity))
  119. {
  120. return lpString.ToString();
  121. }
  122. else
  123. {
  124. //Console.WriteLine("Expected error at GetWindowText. {0}", new Win32Exception(Marshal.GetLastWin32Error()).Message);
  125. return null;
  126. }
  127. }
  128. else
  129. {
  130. //Console.WriteLine("Expected error at GetWindowTextLength. {0}", new Win32Exception(Marshal.GetLastWin32Error()).Message);
  131. return null;
  132. }
  133. }
  134. }
  135. }