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

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

#include  #include  #include  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代码包 ,它具有将post中的代码转换为C#所需的必要p / invoke调用。

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

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

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

以下是Microsoft关于此function的文档: 使用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");