Guest User

Untitled

a guest
Dec 17th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4. using System.Runtime.InteropServices;
  5. using System.Diagnostics;
  6.  
  7. namespace TopMostWindow
  8. {
  9. // Win32API
  10. public class WinAPI
  11. {
  12. [StructLayout(LayoutKind.Sequential)]
  13. public struct RECT
  14. {
  15. int left;
  16. int top;
  17. int right;
  18. int bottom;
  19. }
  20.  
  21. [StructLayout(LayoutKind.Sequential)]
  22. public struct WINDOWINFO
  23. {
  24. public uint cbSize;
  25. public RECT rcWindow;
  26. public RECT rcClient;
  27. public uint dwStyle;
  28. public uint dwExStyle;
  29. public uint dwWindowStatus;
  30. public uint cxWindowBorders;
  31. public uint cyWindowBorders;
  32. public ushort atomWindowType;
  33. public ushort wCreatorVersion;
  34. }
  35.  
  36. [DllImport("user32.dll")]
  37. public extern static IntPtr GetForegroundWindow();
  38.  
  39. [DllImport("user32.dll")]
  40. public extern static int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
  41.  
  42. [DllImport("user32.dll")]
  43. [return: MarshalAs(UnmanagedType.Bool)]
  44. public extern static bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
  45.  
  46. [DllImport("user32.dll")]
  47. public extern static int UnregisterHotKey(IntPtr hWnd, int id);
  48.  
  49. [DllImport("user32.dll")]
  50. [return: MarshalAs(UnmanagedType.Bool)]
  51. public extern static bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
  52.  
  53. [DllImport("user32.dll")]
  54. [return: MarshalAs(UnmanagedType.Bool)]
  55. public extern static bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO pwi);
  56.  
  57. // ホットキーで使用するフラグ
  58. public const int MOD_NONE = 0x0000;
  59. public const int MOD_ALT = 0x0001;
  60. public const int MOD_CONTROL = 0x0002;
  61. public const int MOD_SHIFT = 0x0004;
  62. public const int MOD_WIN = 0x0008;
  63.  
  64. // ウィンドウメッセージの定数
  65. public const int WM_HOTKEY = 0x0312;
  66.  
  67. // ウィンドウの位置の変更に関するフラグ
  68. public const uint SWP_NOSIZE = 0x0001;
  69. public const uint SWP_NOMOVE = 0x0002;
  70. public const uint SWP_SHOWWINDOW = 0x0040;
  71.  
  72. // ウィンドウのZオーダーに関する定数
  73. public const int HWND_TOPMOST = -1;
  74. public const int HWND_NOTOPMOST = -2;
  75.  
  76. // 拡張ウィンドウスタイルのフラグ
  77. public const int WS_EX_TOPMOST = 0x00000008;
  78. }
  79.  
  80. public class TopMostHotKey
  81. {
  82. // ホットキーの識別ID
  83. public const int HOTKEY_ID = 0x0001;
  84.  
  85. // ホットキーにするキーコード
  86. public const int MOD_KEY = WinAPI.MOD_NONE;
  87. public const int HOTKEY = (int)Keys.Pause;
  88.  
  89. // 最前面にしたウィンドウ一覧
  90. private LinkedList<IntPtr> _topMostWindows = new LinkedList<IntPtr>();
  91.  
  92. private NotifyIcon _icon = null;
  93. private IntPtr _hWind = IntPtr.Zero;
  94.  
  95. // コンストラクタ
  96. public TopMostHotKey(IntPtr hWind, NotifyIcon icon)
  97. {
  98. _hWind = hWind;
  99. _icon = icon;
  100. WinAPI.RegisterHotKey(_hWind, HOTKEY_ID, MOD_KEY, HOTKEY);
  101. }
  102.  
  103. // デストラクタ
  104. ~TopMostHotKey()
  105. {
  106. WinAPI.UnregisterHotKey(_hWind, HOTKEY_ID);
  107.  
  108. foreach( var hWnd in _topMostWindows ) {
  109. WinAPI.SetWindowPos(hWnd, (IntPtr)WinAPI.HWND_NOTOPMOST, 0, 0, 0, 0, WinAPI.SWP_SHOWWINDOW | WinAPI.SWP_NOMOVE | WinAPI.SWP_NOSIZE);
  110. }
  111. }
  112.  
  113. // ウィンドウプロシージャ
  114. public void WndProc(ref Message m)
  115. {
  116. if( m.Msg == WinAPI.WM_HOTKEY ) {
  117. if( (int)m.WParam == HOTKEY_ID ) {
  118. ToggleTopMostWindow(GetActiveProcess());
  119. }
  120. }
  121. }
  122.  
  123. // ウィンドウの最前面設定を切り替え
  124. public void ToggleTopMostWindow(Process process)
  125. {
  126. if( process == null ) return;
  127.  
  128. IntPtr hWnd = process.MainWindowHandle;
  129. if( IsTopmost(hWnd) ) {
  130. if( !WinAPI.SetWindowPos(hWnd, (IntPtr)WinAPI.HWND_NOTOPMOST, 0, 0, 0, 0, WinAPI.SWP_SHOWWINDOW | WinAPI.SWP_NOMOVE | WinAPI.SWP_NOSIZE) ) return;
  131. _icon.BalloonTipText = "NoTopMost";
  132. _icon.BalloonTipIcon = ToolTipIcon.None;
  133. _topMostWindows.Remove(hWnd);
  134. }
  135. else {
  136. if( !WinAPI.SetWindowPos(hWnd, (IntPtr)WinAPI.HWND_TOPMOST, 0, 0, 0, 0, WinAPI.SWP_NOMOVE | WinAPI.SWP_NOSIZE) ) return;
  137. _icon.BalloonTipText = "TopMost";
  138. _icon.BalloonTipIcon = ToolTipIcon.Warning;
  139. _topMostWindows.AddLast(hWnd);
  140. }
  141. // タスクトレイアイコンからツールチップの表示する
  142. _icon.BalloonTipTitle = process.MainWindowTitle;
  143. _icon.ShowBalloonTip(10000);
  144. }
  145.  
  146. // アクティブなプロセスを取得
  147. private Process GetActiveProcess()
  148. {
  149. IntPtr hWnd = WinAPI.GetForegroundWindow();
  150. int processId = 0;
  151. WinAPI.GetWindowThreadProcessId(hWnd, out processId);
  152. return Process.GetProcessById(processId);
  153. }
  154.  
  155. // ウィンドウが最前面設定されているか
  156. private bool IsTopmost(IntPtr hWnd)
  157. {
  158. WinAPI.WINDOWINFO info = new WinAPI.WINDOWINFO();
  159. info.cbSize = (uint)Marshal.SizeOf(info);
  160. WinAPI.GetWindowInfo(hWnd, ref info);
  161. return (info.dwExStyle & WinAPI.WS_EX_TOPMOST) != 0;
  162. }
  163. }
  164. }
Add Comment
Please, Sign In to add comment