13

我用 C# 语言 (.net) 编写了一个小型桌面应用程序(主窗体)。而且我想让我的应用程序位于开始菜单(Windows 8)的顶部,就像“camtasia studio screenrecoder”一样。

查看我想要的小应用程序的屏幕截图。

在此处输入图像描述

我必须在我的应用程序中添加什么代码?

注意:我尝试设置topMost = true, 但这不起作用。

4

2 回答 2

6

If you want a window on top of Metro, you need it to declare accessibility. Here are the key points:

  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

  1. Said application cannot be ran in the debugger

  2. 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)

  1. The application must be signed with a trusted certificate.

  2. 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

于 2013-02-02T20:38:09.383 回答
4

为了强制您的应用程序高于 Metro 的用户界面,您需要执行以下操作:

  1. 创建一个 Win32 项目
  2. 完成向导而不做任何更改。
  3. 更改CreateWindowEXset WS_EX_TOPMOST
  4. 转到Project.Properties并链接到清单文件。
  5. 更改 UAC 以绕过 UI 保护;应该/uiAccess = "true"
  6. 构建您的项目。
  7. 使用SignTool对应用程序进行签名。
  8. 确保应用程序存储在Program FilesProgram Files (x86)
  9. 运行您的应用程序。
  10. 加载您Start Menu的应用程序,您的应用程序应该在 Metro 之上运行。

您的清单应如下所示:

<trustInfo xmlns="urn:0073chemas-microsoft-com:asm.v3">
    <security>
        <requestedPrivileges>
        <requestedExecutionLevel
            level="highestAvailable"
            UIAccess="true" />
        </requestedPrivileges>
    </security>
</trustInfo>

默认情况下,false如果省略该属性,或者您的程序集不存在清单,则将其设置为。有了它,false您将无法访问ProtectedUI.

更多关于安全的信息可以在这里找到:

这是一个可以工作或允许修改以测试 UAC 的脚本:

class Elevated_Rights
{

    // Token Bool:
    private bool _level = false;

    #region Constructor:

    protected Elevated_Rights()
    {
         // Invoke Method On Creation:
         Elevate();
     }

     #endregion

     public void Elevate()
     {
         // Get Identity:
         WindowsIdentity user = WindowsIdentity.GetCurrent();

         // Set Principal
         WindowsPrincipal role = new WindowsPrincipal(user);

         #region Test Operating System for UAC:

         if (Environment.OSVersion.Platform != PlatformID.Win32NT || Environment.OSVersion.Version.Major < 6)
         {
              // False:
              _level = false;
         }
         #endregion

         else
         {
              #region Test Identity Not Null:

              if (user == null)
              {
                    // False:
                    _level = false;
              }
              #endregion

              else
              {
                    #region Ensure Security Role:

                    if (!(role.IsInRole(WindowsBuiltInRole.Administrator)))
                    {
                        // False:
                        _level = false;
                    }
                    else
                    {
                        // True:
                        _level = true;
                    }
                    #endregion
               }
          }
 } 

类似的东西可以确保您可以处理或至少提醒用户该功能可能无法正常工作。请注意,在上面我实际上保护了调用并调用了方法;这样我就可以随时访问该_level值以确保身份验证仍然存在。并且仅在需要时继承或使用它以避免不必要的调用。希望这会有所帮助。


更新评论:

这适用于您的 C# 项目,您可以调用以下代码:

using System.Diagnostics;

上述组件将为您提供该功能。然后在一个方法中调用以下内容。

Process command = new Process();
command.StartInfo.FileName = "notepad.exe";
command.Start();

正如您所看到的,它不是技术性的,但它允许您batch调用msiexec. 希望这会有所帮助。

于 2013-02-05T23:21:27.513 回答