簡體   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