【问题标题】:How to prevent an app from being pinned in Windows 7?如何防止应用程序被固定在 Windows 7 中?
【发布时间】:2011-09-16 17:05:08
【问题描述】:

我试图阻止用户将我的 .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# 的运气很差。有人可以帮忙吗?

【问题讨论】:

  • 你有这方面的 C# 代码吗?我只是在引用 Windows API 代码包,所以我认为我只需要你的主要形式的代码吗?提前致谢。

标签: c# .net c++ windows-7 pinning


【解决方案1】:

您可以下载Windows API Code Pack,它有必要的 p/invoke 调用,您需要将帖子中的代码翻译成 C#。

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

【讨论】:

  • 那个杯子只有半满,TaskbarNativeMethods是一个内部类。
  • @Hans Passant:这是与许可问题相关的评论吗?他可以使用它们的实现作为他自己的提示。无论如何,内部类是通过公开的 WindowProperties 公开的。
  • 与许可无关。是的,他必须从代码包源代码中复制粘贴声明。
  • @Hans, @Andrei 我复制并粘贴了仅此一个功能所需的大量代码。结果是大约 50k 的 c# 代码。
  • @AngryHacker:按原样使用它可能会更容易。您将来可能会发现它的其他用途,它将为您节省另一个复制粘贴会话。
【解决方案2】:

在这个问题中,旧新事物帖子还讨论了如何根据每个应用程序设置一些注册表设置,从而防止将应用程序固定到任务栏。

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

以下是 Microsoft 关于此功能的文档:Use Registry to prevent pinning of an application

对此的一个警告是,在 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);                    
            }
        }
    }

【讨论】:

    【解决方案3】:

    使用 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");
    

    【讨论】:

      猜你喜欢
      • 2010-11-18
      • 1970-01-01
      • 2021-11-14
      • 2011-11-24
      • 1970-01-01
      • 2012-01-13
      • 2014-04-18
      • 1970-01-01
      • 2010-12-22
      相关资源
      最近更新 更多