2011-06-16 59 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"); 
相關問題