2011-06-16 63 views
5

我想阻止用户将我的.NET应用程序固定到任务栏。我在Old New Thing上发现了一些代码。但是,它在C++中。如何防止应用程序被固定在Windows 7中?

#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#。有人可以帮忙吗?

+0

你有这个C#代码吗?我只是引用了Windows API代码包,所以我认为我只需要代码是您的主窗体?提前致谢。 – 2013-03-13 23:05:53

回答

8

您可以下载Windows API Code Pack,其中包含必要的p/invoke调用,您需要将您的帖子中的代码转换为C#。

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

+0

该玻璃只是半满,TaskbarNativeMethods是一个内部类。 – 2011-06-16 21:47:32

+0

@Hans Passant:这是一个授权问题相关的评论?他可以使用他们的实现作为他自己的暗示。无论如何,内部类通过公开的WindowProperties公开。 – Andrei 2011-06-17 16:54:13

+0

与许可无关。是的,他必须从代码包源代码中复制粘贴声明。 – 2011-06-17 18:39:44

0

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

您只需在根\应用程序下将“NoStartPage”的值添加到您的应用程序的密钥。该值可以是空白的,也可以是任何类型的值,如果Windows只是看到它的存在,那么当用户在任务栏中右键单击时,它不会显示锁定应用程序的能力。

下面是来自微软对这个功能的文档: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);      
      } 
     } 
    } 
0

使用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"); 
相关问题