保持重要窗口始终置顶的实用工具






4.96/5 (8投票s)
你是否有时会错过 Outlook 提醒窗口?你是否希望聊天窗口始终位于其他窗口之上,这样你就不会错过任何消息?这里有一个应用程序可以帮你解决这个问题。
引言
你是否有时会错过 Outlook 提醒窗口?你是否希望聊天窗口始终位于其他窗口之上,这样你就不会错过任何消息?你是否希望始终置顶一个记事本,以便在处理其他应用程序时随时记录快速笔记?
我们为此提供了一个应用程序。
它在系统托盘中安静运行,并监视打开的窗口。每当它找到你想要始终置顶的窗口时,它就会执行此操作:
你可以使用这个 AlwaysOnTop 实用工具使任何窗口始终位于其他窗口之上。
使用代码
你只需下载二进制文件,解压缩并运行即可。
https://github.com/oazabir/AlwaysOnTop/blob/master/Binary/AlwaysOnTop.zip?raw=true
源代码在这里
https://github.com/oazabir/AlwaysOnTop
关注点
该应用程序会定期扫描打开的窗口,并将标题与一组用户定义的匹配项进行检查。如果窗口标题包含用户定义的词语,则它会调用 user32.dll 中的 SetWindowPos,使窗口始终置顶。
它的工作原理如下
public static void AlwaysOnTop(string titlePart)
{
User32.EnumDelegate filter = delegate(IntPtr hWnd, int lParam)
{
StringBuilder strbTitle = new StringBuilder(255);
int nLength = User32.GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
string strTitle = strbTitle.ToString();
if (User32.IsWindowVisible(hWnd) && string.IsNullOrEmpty(strTitle) == false)
{
if (strTitle.Contains(titlePart))
SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
return true;
};
User32.EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero);
}
EnumDesktopWindows 的定义如下
[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);
设置窗口始终置顶很容易。但取消始终置顶却很困难。如果你只是循环遍历窗口并调用 SetWindowPos 来移除始终置顶标志,它会卡住。你必须专门检查窗口是否为普通窗口。为此,需要一些额外的代码
public static void RemoveAlwaysOnTop()
{
User32.EnumDelegate filter = delegate(IntPtr hWnd, int lParam)
{
StringBuilder strbTitle = new StringBuilder(255);
int nLength = User32.GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
string strTitle = strbTitle.ToString();
if (!string.IsNullOrEmpty(strTitle))
{
WINDOWPLACEMENT placement = GetPlacement(hWnd);
if (placement.showCmd == SW_SHOWNORMAL) // Normal
SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
return true;
};
User32.EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero);
}
这就是所有的乐趣所在!