Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
- namespace TopMostWindow
- {
- // Win32API
- public class WinAPI
- {
- [StructLayout(LayoutKind.Sequential)]
- public struct RECT
- {
- int left;
- int top;
- int right;
- int bottom;
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct WINDOWINFO
- {
- public uint cbSize;
- public RECT rcWindow;
- public RECT rcClient;
- public uint dwStyle;
- public uint dwExStyle;
- public uint dwWindowStatus;
- public uint cxWindowBorders;
- public uint cyWindowBorders;
- public ushort atomWindowType;
- public ushort wCreatorVersion;
- }
- [DllImport("user32.dll")]
- public extern static IntPtr GetForegroundWindow();
- [DllImport("user32.dll")]
- public extern static int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
- [DllImport("user32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- public extern static bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
- [DllImport("user32.dll")]
- public extern static int UnregisterHotKey(IntPtr hWnd, int id);
- [DllImport("user32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- public extern static bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
- [DllImport("user32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- public extern static bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO pwi);
- // ホットキーで使用するフラグ
- public const int MOD_NONE = 0x0000;
- public const int MOD_ALT = 0x0001;
- public const int MOD_CONTROL = 0x0002;
- public const int MOD_SHIFT = 0x0004;
- public const int MOD_WIN = 0x0008;
- // ウィンドウメッセージの定数
- public const int WM_HOTKEY = 0x0312;
- // ウィンドウの位置の変更に関するフラグ
- public const uint SWP_NOSIZE = 0x0001;
- public const uint SWP_NOMOVE = 0x0002;
- public const uint SWP_SHOWWINDOW = 0x0040;
- // ウィンドウのZオーダーに関する定数
- public const int HWND_TOPMOST = -1;
- public const int HWND_NOTOPMOST = -2;
- // 拡張ウィンドウスタイルのフラグ
- public const int WS_EX_TOPMOST = 0x00000008;
- }
- public class TopMostHotKey
- {
- // ホットキーの識別ID
- public const int HOTKEY_ID = 0x0001;
- // ホットキーにするキーコード
- public const int MOD_KEY = WinAPI.MOD_NONE;
- public const int HOTKEY = (int)Keys.Pause;
- // 最前面にしたウィンドウ一覧
- private LinkedList<IntPtr> _topMostWindows = new LinkedList<IntPtr>();
- private NotifyIcon _icon = null;
- private IntPtr _hWind = IntPtr.Zero;
- // コンストラクタ
- public TopMostHotKey(IntPtr hWind, NotifyIcon icon)
- {
- _hWind = hWind;
- _icon = icon;
- WinAPI.RegisterHotKey(_hWind, HOTKEY_ID, MOD_KEY, HOTKEY);
- }
- // デストラクタ
- ~TopMostHotKey()
- {
- WinAPI.UnregisterHotKey(_hWind, HOTKEY_ID);
- foreach( var hWnd in _topMostWindows ) {
- WinAPI.SetWindowPos(hWnd, (IntPtr)WinAPI.HWND_NOTOPMOST, 0, 0, 0, 0, WinAPI.SWP_SHOWWINDOW | WinAPI.SWP_NOMOVE | WinAPI.SWP_NOSIZE);
- }
- }
- // ウィンドウプロシージャ
- public void WndProc(ref Message m)
- {
- if( m.Msg == WinAPI.WM_HOTKEY ) {
- if( (int)m.WParam == HOTKEY_ID ) {
- ToggleTopMostWindow(GetActiveProcess());
- }
- }
- }
- // ウィンドウの最前面設定を切り替え
- public void ToggleTopMostWindow(Process process)
- {
- if( process == null ) return;
- IntPtr hWnd = process.MainWindowHandle;
- if( IsTopmost(hWnd) ) {
- if( !WinAPI.SetWindowPos(hWnd, (IntPtr)WinAPI.HWND_NOTOPMOST, 0, 0, 0, 0, WinAPI.SWP_SHOWWINDOW | WinAPI.SWP_NOMOVE | WinAPI.SWP_NOSIZE) ) return;
- _icon.BalloonTipText = "NoTopMost";
- _icon.BalloonTipIcon = ToolTipIcon.None;
- _topMostWindows.Remove(hWnd);
- }
- else {
- if( !WinAPI.SetWindowPos(hWnd, (IntPtr)WinAPI.HWND_TOPMOST, 0, 0, 0, 0, WinAPI.SWP_NOMOVE | WinAPI.SWP_NOSIZE) ) return;
- _icon.BalloonTipText = "TopMost";
- _icon.BalloonTipIcon = ToolTipIcon.Warning;
- _topMostWindows.AddLast(hWnd);
- }
- // タスクトレイアイコンからツールチップの表示する
- _icon.BalloonTipTitle = process.MainWindowTitle;
- _icon.ShowBalloonTip(10000);
- }
- // アクティブなプロセスを取得
- private Process GetActiveProcess()
- {
- IntPtr hWnd = WinAPI.GetForegroundWindow();
- int processId = 0;
- WinAPI.GetWindowThreadProcessId(hWnd, out processId);
- return Process.GetProcessById(processId);
- }
- // ウィンドウが最前面設定されているか
- private bool IsTopmost(IntPtr hWnd)
- {
- WinAPI.WINDOWINFO info = new WinAPI.WINDOWINFO();
- info.cbSize = (uint)Marshal.SizeOf(info);
- WinAPI.GetWindowInfo(hWnd, ref info);
- return (info.dwExStyle & WinAPI.WS_EX_TOPMOST) != 0;
- }
- }
- }
Add Comment
Please, Sign In to add comment