Advertisement
uwekeim

Windows 7 Taskbar in .NET 2.0

Nov 4th, 2011
884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.94 KB | None | 0 0
  1. namespace ZetaUploader.WindowsClient.Code.UI
  2. {
  3.     using System;
  4.     using System.Diagnostics;
  5.     using System.Runtime.InteropServices;
  6.     using WindowsClient.UI.Helper;
  7.  
  8.     // See http://archive.msdn.microsoft.com/Windows7Taskbar/Release/ProjectReleases.aspx?ReleaseId=2246
  9.     // See http://stackoverflow.com/questions/1295890/windows-7-progress-bar-in-taskbar-in-c
  10.  
  11.     internal static class Windows7Taskbar
  12.     {
  13.         public static uint TaskbarButtonCreatedMessage
  14.         {
  15.             get { return UnsafeNativeMethods.WM_TaskbarButtonCreated; }
  16.         }
  17.  
  18.         internal enum TBPFLAG
  19.         {
  20.             TBPF_NOPROGRESS = 0,
  21.             TBPF_INDETERMINATE = 0x1,
  22.             TBPF_NORMAL = 0x2,
  23.             TBPF_ERROR = 0x4,
  24.             TBPF_PAUSED = 0x8
  25.         }
  26.  
  27.         [ComImportAttribute()]
  28.         [GuidAttribute("ea1afb91-9e28-4b86-90e9-9e9f8a5eefaf")]
  29.         [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
  30.         internal interface ITaskbarList3
  31.         {
  32.             // ITaskbarList
  33.             [PreserveSig]
  34.             void HrInit();
  35.             [PreserveSig]
  36.             void AddTab(IntPtr hwnd);
  37.             [PreserveSig]
  38.             void DeleteTab(IntPtr hwnd);
  39.             [PreserveSig]
  40.             void ActivateTab(IntPtr hwnd);
  41.             [PreserveSig]
  42.             void SetActiveAlt(IntPtr hwnd);
  43.  
  44.             // ITaskbarList2
  45.             [PreserveSig]
  46.             void MarkFullscreenWindow(
  47.                 IntPtr hwnd,
  48.                 [MarshalAs(UnmanagedType.Bool)] bool fFullscreen);
  49.  
  50.             // ITaskbarList3
  51.             void SetProgressValue(IntPtr hwnd, UInt64 ullCompleted, UInt64 ullTotal);
  52.             void SetProgressState(IntPtr hwnd, TBPFLAG tbpFlags);
  53.             void RegisterTab(IntPtr hwndTab, IntPtr hwndMDI);
  54.             void UnregisterTab(IntPtr hwndTab);
  55.             void SetTabOrder(IntPtr hwndTab, IntPtr hwndInsertBefore);
  56.             void SetTabActive(IntPtr hwndTab, IntPtr hwndMDI, TBATFLAG tbatFlags);
  57.             void ThumbBarAddButtons(
  58.                 IntPtr hwnd,
  59.                 uint cButtons,
  60.                 [MarshalAs(UnmanagedType.LPArray)] THUMBBUTTON[] pButtons);
  61.             void ThumbBarUpdateButtons(
  62.                 IntPtr hwnd,
  63.                 uint cButtons,
  64.                 [MarshalAs(UnmanagedType.LPArray)] THUMBBUTTON[] pButtons);
  65.             void ThumbBarSetImageList(IntPtr hwnd, IntPtr himl);
  66.             void SetOverlayIcon(
  67.               IntPtr hwnd,
  68.               IntPtr hIcon,
  69.               [MarshalAs(UnmanagedType.LPWStr)] string pszDescription);
  70.             void SetThumbnailTooltip(
  71.                 IntPtr hwnd,
  72.                 [MarshalAs(UnmanagedType.LPWStr)] string pszTip);
  73.             void SetThumbnailClip(
  74.                 IntPtr hwnd,
  75.                 /*[MarshalAs(UnmanagedType.LPStruct)]*/ ref Win32.RECT prcClip);
  76.         }
  77.         [StructLayout(LayoutKind.Sequential)]
  78.         internal struct RECT
  79.         {
  80.             public int left;
  81.             public int top;
  82.             public int right;
  83.             public int bottom;
  84.  
  85.             public RECT(int left, int top, int right, int bottom)
  86.             {
  87.                 this.left = left;
  88.                 this.top = top;
  89.                 this.right = right;
  90.                 this.bottom = bottom;
  91.             }
  92.         }
  93.  
  94.         internal enum TBATFLAG
  95.         {
  96.             TBATF_USEMDITHUMBNAIL = 0x1,
  97.             TBATF_USEMDILIVEPREVIEW = 0x2
  98.         }
  99.  
  100.         internal enum THBMASK
  101.         {
  102.             THB_BITMAP = 0x1,
  103.             THB_ICON = 0x2,
  104.             THB_TOOLTIP = 0x4,
  105.             THB_FLAGS = 0x8
  106.         }
  107.  
  108.         internal enum THBFLAGS
  109.         {
  110.             THBF_ENABLED = 0,
  111.             THBF_DISABLED = 0x1,
  112.             THBF_DISMISSONCLICK = 0x2,
  113.             THBF_NOBACKGROUND = 0x4,
  114.             THBF_HIDDEN = 0x8
  115.         }
  116.  
  117.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  118.         internal struct THUMBBUTTON
  119.         {
  120.             [MarshalAs(UnmanagedType.U4)]
  121.             public THBMASK dwMask;
  122.             public uint iId;
  123.             public uint iBitmap;
  124.             public IntPtr hIcon;
  125.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  126.             public string szTip;
  127.             [MarshalAs(UnmanagedType.U4)]
  128.             public THBFLAGS dwFlags;
  129.         }
  130.  
  131.         [GuidAttribute(@"56FDF344-FD6D-11d0-958A-006097C9A090")]
  132.         [ClassInterfaceAttribute(ClassInterfaceType.None)]
  133.         [ComImportAttribute]
  134.         internal class CTaskbarList { }
  135.  
  136.         private static ITaskbarList3 _taskbarList;
  137.         private static bool _everTried;
  138.         private static bool _windows7;
  139.         private static bool _everCheckedWindows7;
  140.  
  141.         private static ITaskbarList3 TaskbarList
  142.         {
  143.             get
  144.             {
  145.                 if (_taskbarList == null && !_everTried)
  146.                 {
  147.                     lock (typeof(Windows7Taskbar))
  148.                     {
  149.                         if (_taskbarList == null)
  150.                         {
  151.                             try
  152.                             {
  153.                                 _taskbarList = (ITaskbarList3)new CTaskbarList();
  154.                                 _taskbarList.HrInit();
  155.                             }
  156.                             catch (Exception x)
  157.                             {
  158.                                 Trace.TraceError(x.Message);
  159.                                 _everTried = true;
  160.                             }
  161.                         }
  162.                     }
  163.                 }
  164.                 return _taskbarList;
  165.             }
  166.         }
  167.  
  168.  
  169.         #region Taskbar Progress Bar
  170.  
  171.         /// <summary>
  172.         /// Represents the thumbnail progress bar state.
  173.         /// </summary>
  174.         public enum ThumbnailProgressState
  175.         {
  176.             /// <summary>
  177.             /// No progress is displayed.
  178.             /// </summary>
  179.             NoProgress = 0,
  180.             /// <summary>
  181.             /// The progress is indeterminate (marquee).
  182.             /// </summary>
  183.             Indeterminate = 0x1,
  184.             /// <summary>
  185.             /// Normal progress is displayed.
  186.             /// </summary>
  187.             Normal = 0x2,
  188.             /// <summary>
  189.             /// An error occurred (red).
  190.             /// </summary>
  191.             Error = 0x4,
  192.             /// <summary>
  193.             /// The operation is paused (yellow).
  194.             /// </summary>
  195.             Paused = 0x8
  196.         }
  197.  
  198.         private static bool isRunningOnWindows7
  199.         {
  200.             get
  201.             {
  202.                 //return true;
  203.                 if (!_everCheckedWindows7)
  204.                 {
  205.                     _windows7 =
  206.                         Environment.OSVersion.Version.Major >= 6 &&
  207.                         Environment.OSVersion.Version.Major >= 1 /*&&
  208.                         TaskbarList != null*/;
  209.                     _everCheckedWindows7 = true;
  210.                 }
  211.  
  212.                 return _windows7;
  213.             }
  214.         }
  215.  
  216.         public static void SetProgressState(IntPtr hwnd,
  217.             ThumbnailProgressState state)
  218.         {
  219.             if (isRunningOnWindows7 && TaskbarList!=null)
  220.             {
  221.                 TaskbarList.SetProgressState(hwnd, (TBPFLAG)state);
  222.             }
  223.         }
  224.  
  225.         public static void SetProgressValue(IntPtr hwnd,
  226.             ulong current, ulong maximum)
  227.         {
  228.             if (isRunningOnWindows7 && TaskbarList != null)
  229.             {
  230.                 TaskbarList.SetProgressValue(hwnd, current, maximum);
  231.             }
  232.         }
  233.  
  234.         #endregion
  235.  
  236.         //public static void AllowTaskbarWindowMessagesThroughUIPI()
  237.         //{
  238.         //    if (isRunningOnWindows7)
  239.         //    {
  240.         //        UnsafeNativeMethods.ChangeWindowMessageFilter(
  241.         //            UnsafeNativeMethods.WM_TaskbarButtonCreated,
  242.         //            SafeNativeMethods.MSGFLT_ADD);
  243.         //        UnsafeNativeMethods.ChangeWindowMessageFilter(
  244.         //            SafeNativeMethods.WM_DWMSENDICONICTHUMBNAIL,
  245.         //            SafeNativeMethods.MSGFLT_ADD);
  246.         //        UnsafeNativeMethods.ChangeWindowMessageFilter(
  247.         //            SafeNativeMethods.WM_DWMSENDICONICLIVEPREVIEWBITMAP,
  248.         //            SafeNativeMethods.MSGFLT_ADD);
  249.         //        UnsafeNativeMethods.ChangeWindowMessageFilter(
  250.         //            SafeNativeMethods.WM_COMMAND,
  251.         //            SafeNativeMethods.MSGFLT_ADD);
  252.         //        UnsafeNativeMethods.ChangeWindowMessageFilter(
  253.         //            SafeNativeMethods.WM_SYSCOMMAND,
  254.         //            SafeNativeMethods.MSGFLT_ADD);
  255.         //        UnsafeNativeMethods.ChangeWindowMessageFilter(
  256.         //            SafeNativeMethods.WM_ACTIVATE,
  257.         //            SafeNativeMethods.MSGFLT_ADD);
  258.         //    }
  259.         //}
  260.  
  261.         //public static void SetCurrentProcessAppId(string appId)
  262.         //{
  263.         //    if (isRunningOnWindows7)
  264.         //    {
  265.         //        UnsafeNativeMethods.SetCurrentProcessExplicitAppUserModelID(appId);
  266.         //    }
  267.         //}
  268.     }
  269. }
  270.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement