即使对于现代应用程序也是最重要的
Top most even for Modern Apps
我有一个录音程序一直保持 TopMost
,除非我打开 Modern app (Windows 8)
或 Start Screen
。
可以使桌面应用程序保持在现代应用程序之上,例如 Magnifying Glass
工具:
现在,问题是使用 TopMost 选项 and/or WPF 中的 API 调用 window 不适用于现代应用程序。
我在尝试什么:
static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
static readonly IntPtr HWND_TOP = new IntPtr(0);
static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;
const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
//OnLoaded event handler:
var source = PresentationSource.FromVisual(this) as HwndSource;
SetWindowPos(source.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
只有标记为辅助功能相关的应用程序才能执行此操作。要实现它,请遵循以下准则(摘自 this article 的评论部分):
- The application must demand uiAccess (app.manifest)
- The application must assert “topmost” window positioning (either in Win32/SetWindowPos or WinForms/WPF’s “Topmost” property,
programmatically or otherwise)
- Without making changes to the group policy setting, it must be installed to some trusted location [C:\Windows, C:\Program Files,
C:\Program Files (x86)]. a. Note: If you want to be able to run it out
of an arbitrary location, you must disable the security setting: “User
Account Control: Only elevate UIAccess applications that are installed
in secure locations”. b. Note2: This is the same as setting
HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System\ValidateAdminCodeSignatures
to 0
- Said application cannot be ran in the debugger
- If it’s a .NET application a. The manifest must be embedded in a post-build step b. The application must have “delayed signing”
(meaning it cannot be ran from the built-in debugger, although you can
build and attach – this is what Microsoft does)
- The application must be signed with a trusted certificate.
- Said trusted certificate must be installed to the Trusted Root Certificate Authority (this is important! It must not just simply
installed) For more info see:
http://msdn.microsoft.com/en-us/library/ms726294
...真的不是一件小事!
我有一个录音程序一直保持 TopMost
,除非我打开 Modern app (Windows 8)
或 Start Screen
。
可以使桌面应用程序保持在现代应用程序之上,例如 Magnifying Glass
工具:
现在,问题是使用 TopMost 选项 and/or WPF 中的 API 调用 window 不适用于现代应用程序。
我在尝试什么:
static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
static readonly IntPtr HWND_TOP = new IntPtr(0);
static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;
const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
//OnLoaded event handler:
var source = PresentationSource.FromVisual(this) as HwndSource;
SetWindowPos(source.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
只有标记为辅助功能相关的应用程序才能执行此操作。要实现它,请遵循以下准则(摘自 this article 的评论部分):
- The application must demand uiAccess (app.manifest)
- The application must assert “topmost” window positioning (either in Win32/SetWindowPos or WinForms/WPF’s “Topmost” property, programmatically or otherwise)
- Without making changes to the group policy setting, it must be installed to some trusted location [C:\Windows, C:\Program Files, C:\Program Files (x86)]. a. Note: If you want to be able to run it out of an arbitrary location, you must disable the security setting: “User Account Control: Only elevate UIAccess applications that are installed in secure locations”. b. Note2: This is the same as setting HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System\ValidateAdminCodeSignatures to 0
- Said application cannot be ran in the debugger
- If it’s a .NET application a. The manifest must be embedded in a post-build step b. The application must have “delayed signing” (meaning it cannot be ran from the built-in debugger, although you can build and attach – this is what Microsoft does)
- The application must be signed with a trusted certificate.
- Said trusted certificate must be installed to the Trusted Root Certificate Authority (this is important! It must not just simply installed) For more info see: http://msdn.microsoft.com/en-us/library/ms726294
...真的不是一件小事!