6

ユーザーが自分の.NETアプリをタスクバーに固定できないようにしようとしています。OldNewThingでそれを実行するコードを見つけました。ただし、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#に変換する運がほとんどありません。誰かが助けることができますか?

4

3 に答える 3

9

投稿内のコードをC#に変換するために必要なp/invoke呼び出しを含むWindowsAPIコードパックをダウンロードできます。

ライブラリ全体を使用するか、必要な特定の呼び出しと定義を見つけます(ライブラリを検索してSHGetPropertyStoreForWindowから、他の依存関係を検索します)。

于 2011-06-16T20:48:32.657 に答える
0

質問では、Old New Thingの投稿で、アプリケーションをタスクバーに固定するのを防ぐために、アプリケーションごとにレジストリ設定を設定する方法についても説明しています。

あなたがしなければならないのは、Root\Applicationsの下にあるアプリケーションのキーに「NoStartPage」の値を追加することだけです。値は空白でも、どのタイプでもかまいません。Windowsがその値を認識しただけでは、ユーザーがタスクバーで右クリックしたときにアプリを固定する機能は表示されません。

この機能に関するMicrosoftのドキュメントは次のとおりです。レジストリを使用して、アプリケーションの固定を防止します

これに対する1つの注意点は、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);                    
            }
        }
    }
于 2013-05-03T04:30:22.150 に答える
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");
于 2016-04-29T15:36:21.290 に答える