Last active
November 29, 2018 07:14
-
-
Save JakobFerdinand/b0a59114621d7cb24c7833e01c4fb3ac to your computer and use it in GitHub Desktop.
An AttachedProperty that lets you display a different windows in the same process in different taskbar groups in windows.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.CompilerServices; | |
using System.Runtime.InteropServices; | |
using System.Windows; | |
using System.Windows.Interop; | |
namespace TasbarSample | |
{ | |
public static class TaskbarHelper | |
{ | |
public static string GetApplicationUserModelId(DependencyObject obj) => (string)obj.GetValue(ApplicationUserModelIdProperty); | |
public static void SetApplicationUserModelId(DependencyObject obj, string value) => obj.SetValue(ApplicationUserModelIdProperty, value); | |
public static readonly DependencyProperty ApplicationUserModelIdProperty = | |
DependencyProperty.RegisterAttached("ApplicationUserModelId", typeof(string), typeof(TaskbarHelper), new PropertyMetadata(null, ApplicationUserModelIdCahnged)); | |
private static void ApplicationUserModelIdCahnged(DependencyObject d, DependencyPropertyChangedEventArgs e) | |
{ | |
if (!(d is Window window)) | |
return; | |
if (!(e.NewValue is string applicationUserModelId)) | |
return; | |
window.Loaded += (s, eargs) => | |
{ | |
var handle = new WindowInteropHelper(window).Handle; | |
IPropertyStore propertyStore = null; | |
SHGetPropertyStoreForWindow(handle, ref IID_PropertyStore, ref propertyStore); | |
var key = new PROPERTYKEY() { pid = 5, fmtid = new Guid("9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3") }; | |
object id = applicationUserModelId; | |
propertyStore.SetValue(ref key, ref id); | |
}; | |
} | |
public static Guid IID_PropertyStore = new Guid("886d8eeb-8cf2-4446-8d02-cdba1dbdcf99"); | |
[StructLayout(LayoutKind.Sequential, Pack = 4)] | |
public struct PROPERTYKEY | |
{ | |
public Guid fmtid; | |
public uint pid; | |
} | |
[ComImport, Guid("886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] | |
public interface IPropertyStore | |
{ | |
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] | |
void GetCount([Out] out uint cProps); | |
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] | |
void GetAt([In] uint iProp, out PROPERTYKEY pkey); | |
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] | |
void GetValue([In] ref PROPERTYKEY key, out object pv); | |
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] | |
void SetValue([In] ref PROPERTYKEY key, [In] ref object pv); | |
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] | |
void Commit(); | |
} | |
[DllImport("Shell32.dll")] | |
private static extern uint SHGetPropertyStoreForWindow(IntPtr hwnd, ref Guid riid, ref IPropertyStore ppv); | |
[DllImport("Shell32.dll")] | |
private static extern uint GetCurrentProcessExplicitAppUserModelID(out string appID); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment