Top most even for Modern Apps

336 views Asked by At

I have a recording program that stays TopMost all the time except when I open a Modern app (Windows 8) or the Start Screen.

It is possible to make a desktop application stay on top of modern apps, like the Magnifying Glass tool:

TopMost

Now, the problem is that using the TopMost option and/or the API call in a WPF window won't work with modern apps.

What I'm trying:

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);
1

There are 1 answers

0
almulo On BEST ANSWER

Only applications marked as Accessibility related can do this. To achieve it, follow these guidelines (taken from the comments section of this article):

  1. The application must demand uiAccess (app.manifest)
  2. The application must assert “topmost” window positioning (either in Win32/SetWindowPos or WinForms/WPF’s “Topmost” property, programmatically or otherwise)
  3. 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
  4. Said application cannot be ran in the debugger
  5. 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)
  6. The application must be signed with a trusted certificate.
  7. 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

... Not really a trivial task!