繁体   English   中英

如何防止应用程序被固定在Windows 7中?

[英]How to prevent an app from being pinned in Windows 7?

我试图阻止用户将我的.NET应用程序固定到任务栏。 我在Old New Thing上发现了一些代码。 但是,它是在C ++中。

#include <shellapi.h>
#include <propsys.h>
#include <propkey.h>

HRESULT MarkWindowAsUnpinnable(HWND hwnd)
{
 IPropertyStore *pps;
 HRESULT hr = SHGetPropertyStoreForWindow(hwnd, IID_PPV_ARGS(&pps));
 if (SUCCEEDED(hr)) {
  PROPVARIANT var;
  var.vt = VT_BOOL;
  var.boolVal = VARIANT_TRUE;
  hr = pps->SetValue(PKEY_AppUserModel_PreventPinning, var);
  pps->Release();
 }
 return hr;
}


BOOL
OnCreate(HWND hwnd, LPCREATESTRUCT lpcs)
{
 MarkWindowAsUnpinnable(hwnd);
 return TRUE;
}

我很难将它转换为c#。 有人可以帮忙吗?

您可以下载Windows API代码包 ,该代码包具有将帖子中的代码转换为C#所需的必要p / invoke调用。

要么整体使用库,要么找到所需的特定调用和定义(搜索SHGetPropertyStoreForWindow ,然后搜索其他依赖项)。

在这个问题中,Old New Thing帖子还讨论了如何在每个应用程序的基础上设置一些注册表设置,以防止将应用程序固定到任务栏。

您所要做的就是将“NoStartPage”的值添加到Root \\ Applications下的应用程序密钥中。 该值可以为空白和任何类型,如果Windows只是看到它在那里它将不会显示固定应用程序的能力,当用户在任务栏中右键单击它时。

以下是Microsoft关于此功能的文档: 使用Registry来防止固定应用程序

需要注意的是,在Windows 7中,由于UAC,您必须以管理员身份运行才能更新注册表。 我是通过app.manifest做到的。

找到正确的代码并更新正确的注册表项的代码如下(希望它不是太冗长):

public static void Main(string[] args)
    {
        // Get Root
        var root = Registry.ClassesRoot;

        // Get the Applications key
        var applicationsSubKey = root.OpenSubKey("Applications", true);

        if (applicationsSubKey != null)
        {
            bool updateNoStartPageKey = false;

            // Check to see if your application already has a key created in the Applications key
            var appNameSubKey = applicationsSubKey.OpenSubKey("MyAppName.exe", true);

            if (appNameSubKey != null)
            {
                // Check to see if the NoStartPage value has already been created
                if (!appNameSubKey.GetValueNames().Contains("NoStartPage"))
                {
                    updateNoStartPageKey = true;
                }
            }
            else
            {
                // create key for your application in the Applications key under Root
                appNameSubKey = applicationsSubKey.CreateSubKey("MyAppName.exe", RegistryKeyPermissionCheck.Default);

                if (appNameSubKey != null)
                {
                    updateNoStartPageKey = true;
                }
            }

            if (updateNoStartPageKey)
            {
                // Create/update the value for NoStartPage so Windows will prevent the app from being pinned.
                appNameSubKey.SetValue("NoStartPage", string.Empty, RegistryValueKind.String);                    
            }
        }
    }

使用WindowsAPICodePack(通过NuGet),您需要类似的代码:

// Ensure the handle is available
new WindowInteropHelper(window).EnsureHandle();

// Prevent the window from being pinned to the task bars
var preventPinningProperty = new PropertyKey(
        new Guid("9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3"), 9);
WindowProperties.SetWindowProperty(window, preventPinningProperty, "1");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM