選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

157 行
7.3 KiB

  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace RenderHookAPI
  4. {
  5. [System.Security.SuppressUnmanagedCodeSecurity()]
  6. internal sealed class NativeMethods
  7. {
  8. private NativeMethods() { }
  9. internal static bool IsWindowInForeground(IntPtr hWnd)
  10. {
  11. return hWnd == GetForegroundWindow();
  12. }
  13. #region kernel32
  14. [DllImport("kernel32.dll")]
  15. public static extern IntPtr GetModuleHandle(string lpModuleName);
  16. #endregion
  17. #region user32
  18. #region ShowWindow
  19. /// <summary>Shows a Window</summary>
  20. /// <remarks>
  21. /// <para>To perform certain special effects when showing or hiding a
  22. /// window, use AnimateWindow.</para>
  23. ///<para>The first time an application calls ShowWindow, it should use
  24. ///the WinMain function's nCmdShow parameter as its nCmdShow parameter.
  25. ///Subsequent calls to ShowWindow must use one of the values in the
  26. ///given list, instead of the one specified by the WinMain function's
  27. ///nCmdShow parameter.</para>
  28. ///<para>As noted in the discussion of the nCmdShow parameter, the
  29. ///nCmdShow value is ignored in the first call to ShowWindow if the
  30. ///program that launched the application specifies startup information
  31. ///in the structure. In this case, ShowWindow uses the information
  32. ///specified in the STARTUPINFO structure to show the window. On
  33. ///subsequent calls, the application must call ShowWindow with nCmdShow
  34. ///set to SW_SHOWDEFAULT to use the startup information provided by the
  35. ///program that launched the application. This behavior is designed for
  36. ///the following situations: </para>
  37. ///<list type="">
  38. /// <item>Applications create their main window by calling CreateWindow
  39. /// with the WS_VISIBLE flag set. </item>
  40. /// <item>Applications create their main window by calling CreateWindow
  41. /// with the WS_VISIBLE flag cleared, and later call ShowWindow with the
  42. /// SW_SHOW flag set to make it visible.</item>
  43. ///</list></remarks>
  44. /// <param name="hWnd">Handle to the window.</param>
  45. /// <param name="nCmdShow">Specifies how the window is to be shown.
  46. /// This parameter is ignored the first time an application calls
  47. /// ShowWindow, if the program that launched the application provides a
  48. /// STARTUPINFO structure. Otherwise, the first time ShowWindow is called,
  49. /// the value should be the value obtained by the WinMain function in its
  50. /// nCmdShow parameter. In subsequent calls, this parameter can be one of
  51. /// the WindowShowStyle members.</param>
  52. /// <returns>
  53. /// If the window was previously visible, the return value is nonzero.
  54. /// If the window was previously hidden, the return value is zero.
  55. /// </returns>
  56. [DllImport("user32.dll")]
  57. internal static extern bool ShowWindow(IntPtr hWnd, WindowShowStyle nCmdShow);
  58. /// <summary>Enumeration of the different ways of showing a window using
  59. /// ShowWindow</summary>
  60. internal enum WindowShowStyle : uint
  61. {
  62. /// <summary>Hides the window and activates another window.</summary>
  63. /// <remarks>See SW_HIDE</remarks>
  64. Hide = 0,
  65. /// <summary>Activates and displays a window. If the window is minimized
  66. /// or maximized, the system restores it to its original size and
  67. /// position. An application should specify this flag when displaying
  68. /// the window for the first time.</summary>
  69. /// <remarks>See SW_SHOWNORMAL</remarks>
  70. ShowNormal = 1,
  71. /// <summary>Activates the window and displays it as a minimized window.</summary>
  72. /// <remarks>See SW_SHOWMINIMIZED</remarks>
  73. ShowMinimized = 2,
  74. /// <summary>Activates the window and displays it as a maximized window.</summary>
  75. /// <remarks>See SW_SHOWMAXIMIZED</remarks>
  76. ShowMaximized = 3,
  77. /// <summary>Maximizes the specified window.</summary>
  78. /// <remarks>See SW_MAXIMIZE</remarks>
  79. Maximize = 3,
  80. /// <summary>Displays a window in its most recent size and position.
  81. /// This value is similar to "ShowNormal", except the window is not
  82. /// actived.</summary>
  83. /// <remarks>See SW_SHOWNOACTIVATE</remarks>
  84. ShowNormalNoActivate = 4,
  85. /// <summary>Activates the window and displays it in its current size
  86. /// and position.</summary>
  87. /// <remarks>See SW_SHOW</remarks>
  88. Show = 5,
  89. /// <summary>Minimizes the specified window and activates the next
  90. /// top-level window in the Z order.</summary>
  91. /// <remarks>See SW_MINIMIZE</remarks>
  92. Minimize = 6,
  93. /// <summary>Displays the window as a minimized window. This value is
  94. /// similar to "ShowMinimized", except the window is not activated.</summary>
  95. /// <remarks>See SW_SHOWMINNOACTIVE</remarks>
  96. ShowMinNoActivate = 7,
  97. /// <summary>Displays the window in its current size and position. This
  98. /// value is similar to "Show", except the window is not activated.</summary>
  99. /// <remarks>See SW_SHOWNA</remarks>
  100. ShowNoActivate = 8,
  101. /// <summary>Activates and displays the window. If the window is
  102. /// minimized or maximized, the system restores it to its original size
  103. /// and position. An application should specify this flag when restoring
  104. /// a minimized window.</summary>
  105. /// <remarks>See SW_RESTORE</remarks>
  106. Restore = 9,
  107. /// <summary>Sets the show state based on the SW_ value specified in the
  108. /// STARTUPINFO structure passed to the CreateProcess function by the
  109. /// program that started the application.</summary>
  110. /// <remarks>See SW_SHOWDEFAULT</remarks>
  111. ShowDefault = 10,
  112. /// <summary>Windows 2000/XP: Minimizes a window, even if the thread
  113. /// that owns the window is hung. This flag should only be used when
  114. /// minimizing windows from a different thread.</summary>
  115. /// <remarks>See SW_FORCEMINIMIZE</remarks>
  116. ForceMinimized = 11
  117. }
  118. #endregion
  119. /// <summary>
  120. /// The GetForegroundWindow function returns a handle to the foreground window.
  121. /// </summary>
  122. [DllImport("user32.dll")]
  123. internal static extern IntPtr GetForegroundWindow();
  124. [DllImport("user32.dll")]
  125. [return: MarshalAs(UnmanagedType.Bool)]
  126. internal static extern bool SetForegroundWindow(IntPtr hWnd);
  127. [DllImport("user32.dll")]
  128. [return: MarshalAs(UnmanagedType.Bool)]
  129. internal static extern bool IsIconic(IntPtr hWnd);
  130. //Get window position
  131. [DllImport("user32.dll")]
  132. public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);
  133. public struct Rect
  134. {
  135. public int Left { get; set; }
  136. public int Top { get; set; }
  137. public int Right { get; set; }
  138. public int Bottom { get; set; }
  139. }
  140. #endregion
  141. }
  142. }