namespace ZetaUploader.WindowsClient.Code.UI
{
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using WindowsClient.UI.Helper;
// See http://archive.msdn.microsoft.com/Windows7Taskbar/Release/ProjectReleases.aspx?ReleaseId=2246
// See http://stackoverflow.com/questions/1295890/windows-7-progress-bar-in-taskbar-in-c
internal static class Windows7Taskbar
{
public static uint TaskbarButtonCreatedMessage
{
get { return UnsafeNativeMethods.WM_TaskbarButtonCreated; }
}
internal enum TBPFLAG
{
TBPF_NOPROGRESS = 0,
TBPF_INDETERMINATE = 0x1,
TBPF_NORMAL = 0x2,
TBPF_ERROR = 0x4,
TBPF_PAUSED = 0x8
}
[ComImportAttribute()]
[GuidAttribute("ea1afb91-9e28-4b86-90e9-9e9f8a5eefaf")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
internal interface ITaskbarList3
{
// ITaskbarList
[PreserveSig]
void HrInit();
[PreserveSig]
void AddTab(IntPtr hwnd);
[PreserveSig]
void DeleteTab(IntPtr hwnd);
[PreserveSig]
void ActivateTab(IntPtr hwnd);
[PreserveSig]
void SetActiveAlt(IntPtr hwnd);
// ITaskbarList2
[PreserveSig]
void MarkFullscreenWindow(
IntPtr hwnd,
[MarshalAs(UnmanagedType.Bool)] bool fFullscreen);
// ITaskbarList3
void SetProgressValue(IntPtr hwnd, UInt64 ullCompleted, UInt64 ullTotal);
void SetProgressState(IntPtr hwnd, TBPFLAG tbpFlags);
void RegisterTab(IntPtr hwndTab, IntPtr hwndMDI);
void UnregisterTab(IntPtr hwndTab);
void SetTabOrder(IntPtr hwndTab, IntPtr hwndInsertBefore);
void SetTabActive(IntPtr hwndTab, IntPtr hwndMDI, TBATFLAG tbatFlags);
void ThumbBarAddButtons(
IntPtr hwnd,
uint cButtons,
[MarshalAs(UnmanagedType.LPArray)] THUMBBUTTON[] pButtons);
void ThumbBarUpdateButtons(
IntPtr hwnd,
uint cButtons,
[MarshalAs(UnmanagedType.LPArray)] THUMBBUTTON[] pButtons);
void ThumbBarSetImageList(IntPtr hwnd, IntPtr himl);
void SetOverlayIcon(
IntPtr hwnd,
IntPtr hIcon,
[MarshalAs(UnmanagedType.LPWStr)] string pszDescription);
void SetThumbnailTooltip(
IntPtr hwnd,
[MarshalAs(UnmanagedType.LPWStr)] string pszTip);
void SetThumbnailClip(
IntPtr hwnd,
/*[MarshalAs(UnmanagedType.LPStruct)]*/ ref Win32.RECT prcClip);
}
[StructLayout(LayoutKind.Sequential)]
internal struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
public RECT(int left, int top, int right, int bottom)
{
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
}
internal enum TBATFLAG
{
TBATF_USEMDITHUMBNAIL = 0x1,
TBATF_USEMDILIVEPREVIEW = 0x2
}
internal enum THBMASK
{
THB_BITMAP = 0x1,
THB_ICON = 0x2,
THB_TOOLTIP = 0x4,
THB_FLAGS = 0x8
}
internal enum THBFLAGS
{
THBF_ENABLED = 0,
THBF_DISABLED = 0x1,
THBF_DISMISSONCLICK = 0x2,
THBF_NOBACKGROUND = 0x4,
THBF_HIDDEN = 0x8
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct THUMBBUTTON
{
[MarshalAs(UnmanagedType.U4)]
public THBMASK dwMask;
public uint iId;
public uint iBitmap;
public IntPtr hIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szTip;
[MarshalAs(UnmanagedType.U4)]
public THBFLAGS dwFlags;
}
[GuidAttribute(@"56FDF344-FD6D-11d0-958A-006097C9A090")]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComImportAttribute]
internal class CTaskbarList { }
private static ITaskbarList3 _taskbarList;
private static bool _everTried;
private static bool _windows7;
private static bool _everCheckedWindows7;
private static ITaskbarList3 TaskbarList
{
get
{
if (_taskbarList == null && !_everTried)
{
lock (typeof(Windows7Taskbar))
{
if (_taskbarList == null)
{
try
{
_taskbarList = (ITaskbarList3)new CTaskbarList();
_taskbarList.HrInit();
}
catch (Exception x)
{
Trace.TraceError(x.Message);
_everTried = true;
}
}
}
}
return _taskbarList;
}
}
#region Taskbar Progress Bar
/// <summary>
/// Represents the thumbnail progress bar state.
/// </summary>
public enum ThumbnailProgressState
{
/// <summary>
/// No progress is displayed.
/// </summary>
NoProgress = 0,
/// <summary>
/// The progress is indeterminate (marquee).
/// </summary>
Indeterminate = 0x1,
/// <summary>
/// Normal progress is displayed.
/// </summary>
Normal = 0x2,
/// <summary>
/// An error occurred (red).
/// </summary>
Error = 0x4,
/// <summary>
/// The operation is paused (yellow).
/// </summary>
Paused = 0x8
}
private static bool isRunningOnWindows7
{
get
{
//return true;
if (!_everCheckedWindows7)
{
_windows7 =
Environment.OSVersion.Version.Major >= 6 &&
Environment.OSVersion.Version.Major >= 1 /*&&
TaskbarList != null*/;
_everCheckedWindows7 = true;
}
return _windows7;
}
}
public static void SetProgressState(IntPtr hwnd,
ThumbnailProgressState state)
{
if (isRunningOnWindows7 && TaskbarList!=null)
{
TaskbarList.SetProgressState(hwnd, (TBPFLAG)state);
}
}
public static void SetProgressValue(IntPtr hwnd,
ulong current, ulong maximum)
{
if (isRunningOnWindows7 && TaskbarList != null)
{
TaskbarList.SetProgressValue(hwnd, current, maximum);
}
}
#endregion
//public static void AllowTaskbarWindowMessagesThroughUIPI()
//{
// if (isRunningOnWindows7)
// {
// UnsafeNativeMethods.ChangeWindowMessageFilter(
// UnsafeNativeMethods.WM_TaskbarButtonCreated,
// SafeNativeMethods.MSGFLT_ADD);
// UnsafeNativeMethods.ChangeWindowMessageFilter(
// SafeNativeMethods.WM_DWMSENDICONICTHUMBNAIL,
// SafeNativeMethods.MSGFLT_ADD);
// UnsafeNativeMethods.ChangeWindowMessageFilter(
// SafeNativeMethods.WM_DWMSENDICONICLIVEPREVIEWBITMAP,
// SafeNativeMethods.MSGFLT_ADD);
// UnsafeNativeMethods.ChangeWindowMessageFilter(
// SafeNativeMethods.WM_COMMAND,
// SafeNativeMethods.MSGFLT_ADD);
// UnsafeNativeMethods.ChangeWindowMessageFilter(
// SafeNativeMethods.WM_SYSCOMMAND,
// SafeNativeMethods.MSGFLT_ADD);
// UnsafeNativeMethods.ChangeWindowMessageFilter(
// SafeNativeMethods.WM_ACTIVATE,
// SafeNativeMethods.MSGFLT_ADD);
// }
//}
//public static void SetCurrentProcessAppId(string appId)
//{
// if (isRunningOnWindows7)
// {
// UnsafeNativeMethods.SetCurrentProcessExplicitAppUserModelID(appId);
// }
//}
}
}
Comments