記錄win11下,WPF設置(zhi) System.AppUserModel.PreventPinning 屬性用(yong)于阻止用(yong)戶將應用(yong)程序固(gu)定到任(ren)務欄(lan)
本篇博文我僅僅是作為記錄者,實際上發現并編碼的是我們組的小伙伴天保同學。
我們組小伙伴有個需求,需要將WPF應用的 任務欄右鍵菜單菜單,不顯示“固定到任務欄”。
如下圖:
普通窗口的任務欄右鍵菜單:


但是發現部分窗口有只顯示 “關閉窗口”
如“微信公眾號”的窗口

微軟的(de)官方文(wen)檔(dang): 禁用將快捷方式或窗口固定到任務(wu)欄(lan)或 開始(shi) 菜單的(de)功能。 此屬性還使(shi)項無法包含在 “開始(shi)” 菜單的(de)“最(zui)常用的(de)”(MFU)列表(biao)中(zhong)。
1、首先定義System.AppUserModel.PreventPinning:ROPERTYKEY(new Guid("{9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3}"), 9):詳(xiang)情看propertyKey的參數解釋:
2、給IPropertyStore 定義 IID Guid IID_IPropertyStore = new Guid("886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99");
3、調用shell32 的 SHGetPropertyStoreForWindow實(shi)現 禁用的功(gong)能
代碼如下:
public class NativeWin32 { [DllImport("shell32.dll")] public static extern int SHGetPropertyStoreForWindow(IntPtr hwnd, ref Guid riid, out IPropertyStore propertyStore); [DllImport("ole32.dll")] public static extern int PropVariantClear(ref PROPVARIANT pvar); // Define IPropertyStore interface [ComImport] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] [Guid("886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99")] public interface IPropertyStore { void GetCount(out uint cProps); void GetAt(uint iProp, out PROPERTYKEY pkey); void GetValue(ref PROPERTYKEY key, out PROPVARIANT pv); void SetValue(ref PROPERTYKEY key, ref PROPVARIANT pv); void Commit(); } // Define IID for IPropertyStore public static Guid IID_IPropertyStore = new Guid("886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99"); // Define PROPERTYKEY struct [StructLayout(LayoutKind.Sequential, Pack = 4)] public struct PROPERTYKEY { public Guid fmtid; public uint pid; public PROPERTYKEY(Guid fmtid, uint pid) { this.fmtid = fmtid; this.pid = pid; } } // Define PROPVARIANT structure (simplified, we'll use a simple one for bool) [StructLayout(LayoutKind.Explicit)] public struct PROPVARIANT { // We'll only implement the necessary part for boolean [FieldOffset(0)] public ushort vt; [FieldOffset(8)] public byte boolVal; public void SetValue(bool value) { // VT_BOOL vt = 11; boolVal = value ? (byte)1 : (byte)0; } } }
在窗口加載的時候調用方法:
public MainWindow() { InitializeComponent(); SourceInitialized += MainWindow_SourceInitialized; } private void MainWindow_SourceInitialized(object? sender, EventArgs e) { SetPinning(); } private void SetPinning() { IntPtr hwnd = new WindowInteropHelper(this).Handle; // Define the property key for System.AppUserModel.PreventPinning NativeWin32.PROPERTYKEY propKey = new NativeWin32.PROPERTYKEY(new Guid("{9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3}"), 9); // Get the IPropertyStore for the window NativeWin32.IPropertyStore propStore; int hr = NativeWin32.SHGetPropertyStoreForWindow(hwnd, ref NativeWin32.IID_IPropertyStore, out propStore); if (hr != 0) // if failed { Marshal.ThrowExceptionForHR(hr); } try { // Create a PROPVARIANT with bool value: true NativeWin32.PROPVARIANT pv = new NativeWin32.PROPVARIANT(); pv.SetValue(true); // Set the property propStore.SetValue(ref propKey, ref pv); // We must free the PROPVARIANT NativeWin32.PropVariantClear(ref pv); } finally { // Release the IPropertyStore Marshal.ReleaseComObject(propStore); } }
結果如下:

代碼鏈接:
參考資料:
