OSDN Git Service

AppliStation-GUI,Windows 7について、進捗をタスクバーに表示するように
authorttp <ttp@users.sourceforge.jp>
Thu, 31 Dec 2009 05:17:40 +0000 (14:17 +0900)
committerttp <ttp@users.sourceforge.jp>
Thu, 31 Dec 2009 05:17:40 +0000 (14:17 +0900)
AppliStation/AppliStation.Util/ExecutionProgressViewer.cs
AppliStation/AppliStation.Util/NativeMethods.cs

index b6d90d0..42f9e43 100644 (file)
@@ -49,14 +49,6 @@ namespace AppliStation.Util
                                return;\r
                        }\r
                        \r
-                       \r
-                       if (a.TaskProgressPercent >= 0) {\r
-                               progressBarSub.Value = (int) a.TaskProgressPercent;\r
-                               progressBarSub.Style = ProgressBarStyle.Continuous;\r
-                       } else {\r
-                               progressBarSub.Style = ProgressBarStyle.Marquee;\r
-                       }\r
-                       \r
                        switch (a.Type) {\r
                                case DownloadEventType.INITED:\r
                                case DownloadEventType.CONNECTED:\r
@@ -81,6 +73,14 @@ namespace AppliStation.Util
                                        logBox.SelectionColor = logBox.ForeColor;\r
                                        break;\r
                        }\r
+                       \r
+                       // ダウンロードの進捗を表示\r
+                       if (a.TaskProgressPercent >= 0) {\r
+                               progressBarSub.Value = (int) a.TaskProgressPercent;\r
+                               progressBarSub.Style = ProgressBarStyle.Continuous;\r
+                       } else {\r
+                               progressBarSub.Style = ProgressBarStyle.Marquee;\r
+                       }\r
                }\r
                \r
                #region NaGetTaskSet関連\r
@@ -88,13 +88,7 @@ namespace AppliStation.Util
                private void onTaskSetRaised(object sender, NaGetTaskSetEventArgs e)\r
                {\r
                        NaGetTaskSet taskSet = (NaGetTaskSet) sender;\r
-                       \r
-                       if (e.TaskProgressPercent >= 0) {\r
-                               progressBar.Value = (int) e.TaskProgressPercent;\r
-                               progressBar.Style = ProgressBarStyle.Continuous;\r
-                       } else {\r
-                               progressBar.Style = ProgressBarStyle.Marquee;\r
-                       }\r
+                       NativeMethods.ProgressBarState progressState = NativeMethods.ProgressBarState.Normal;\r
                        \r
                        cancelButton.Enabled = taskSet.Running && taskSet.Cancelable;\r
                        \r
@@ -117,7 +111,7 @@ namespace AppliStation.Util
                                        if (progressBarSub.Visible) progressBarSub.Hide();\r
                                        if (progressSubLabel.Visible) progressSubLabel.Hide();\r
                                        \r
-                                       NativeMethods.ProgressBar_SetState(progressBar, 1); // VistaProgress色:ノーマル\r
+                                       progressState = NativeMethods.ProgressBarState.Normal;\r
                                        \r
                                        logBox.AppendText(string.Format(" ... 完了. [{0}%]", (int) e.TaskProgressPercent));\r
                                        logBox.AppendText(System.Environment.NewLine);\r
@@ -130,7 +124,7 @@ namespace AppliStation.Util
                                        logBox.AppendText("  [エラー] " + e.TaskMessage + System.Environment.NewLine);\r
                                        logBox.SelectionColor = logBox.ForeColor;\r
                                        \r
-                                       NativeMethods.ProgressBar_SetState(progressBar, 2); // VistaProgress色:エラー\r
+                                       progressState = NativeMethods.ProgressBarState.Error;\r
                                        \r
                                        autoCloseCheckBox.Enabled = false;\r
                                        okButton.Enabled = true;\r
@@ -141,8 +135,7 @@ namespace AppliStation.Util
                                        logBox.AppendText(e.TaskMessage + System.Environment.NewLine);\r
                                        logBox.SelectionColor = logBox.ForeColor;\r
                                        \r
-                                       NativeMethods.ProgressBar_SetState(progressBar, 1); // VistaProgress色:中断\r
-                                       NativeMethods.ProgressBar_SetState(progressBarSub, 1); // VistaProgress色:中断\r
+                                       progressState = NativeMethods.ProgressBarState.Error;\r
                                        \r
                                        autoCloseCheckBox.Enabled = false;\r
                                        okButton.Enabled = true;\r
@@ -153,15 +146,29 @@ namespace AppliStation.Util
                                        logBox.AppendText("  [エラー] " + e.TaskMessage + System.Environment.NewLine);\r
                                        logBox.SelectionColor = logBox.ForeColor;\r
                                        \r
-                                       NativeMethods.ProgressBar_SetState(progressBar, 2); // VistaProgress色:エラー\r
+                                       progressState = NativeMethods.ProgressBarState.Error;\r
                                        \r
                                        autoCloseCheckBox.Enabled = false;\r
                                        break;\r
                        }\r
                        \r
-                       #if DEBUG\r
+                       // タスクセット全体の進捗をプログレスバーに表示する\r
+                       if (e.TaskProgressPercent >= 0) {\r
+                               progressBar.Value = (int) e.TaskProgressPercent;\r
+                               progressBar.Style = ProgressBarStyle.Continuous;\r
+                       } else {\r
+                               progressBar.Style = ProgressBarStyle.Marquee;\r
+                       }\r
+                       NativeMethods.ProgressBar_SetState(progressBar, progressState);\r
+                       NativeMethods.ProgressBar_SetState(progressBarSub, progressState);\r
+                       // タスクセット全体の進捗をタスクバーに表示する(Windows 7以降のみ)\r
+                       if (e.TaskProgressPercent >= 0 && e.TaskProgressPercent < 100) {\r
+                               NativeMethods.Form_SetTaskbarProgressParams(this, progressBar.Style, progressState, (ulong) e.TaskProgressPercent, 100);\r
+                       } else {\r
+                               NativeMethods.Form_SetTaskbarProgressParams(this, progressBar.Style, progressState, 0, 0);\r
+                       }\r
+                       \r
                        logBox.ScrollToCaret();\r
-                       #endif\r
                        \r
                        if (taskSet.Done) {\r
                                if ((ActionOnDone & ActionOnDoneFlags.FlashWindow) != 0) {\r
@@ -257,7 +264,7 @@ namespace AppliStation.Util
                        if (taskSet != null && taskSet.Running && taskSet.Cancelable) {\r
                                cancelButton.Enabled = false;\r
                                \r
-                               NativeMethods.ProgressBar_SetState(progressBar, 3); // VistaProgress色:中断\r
+                               NativeMethods.ProgressBar_SetState(progressBar, NativeMethods.ProgressBarState.Paused);\r
                                \r
                                taskSet.Cancel();\r
                        }\r
index 2bd8d06..cc83ae1 100644 (file)
@@ -1,6 +1,9 @@
 // Vistaの効果を有効にするフラグ(不要の場合はコメントアウト)\r
 #define USE_VISTA_EFFECTS\r
 \r
+// Sevenの効果を有効にするフラグ(不要の場合はコメントアウト)\r
+#define USE_SEVEN_EFFECTS\r
+\r
 using System;\r
 using System.Runtime.InteropServices;\r
 using System.Windows.Forms;\r
@@ -20,23 +23,126 @@ namespace AppliStation.Util
                {\r
                }\r
 \r
+               #region 進捗の表示\r
+               \r
                /// <summary>\r
-               /// WindowsVista向け、プログレスバーステータス(色)を設定する\r
+               /// プログレスバーステータス(色)\r
+               /// </summary>\r
+               public enum ProgressBarState\r
+               {\r
+                       /// <summary>\r
+                       /// 通常の状態(PBST_NORMAL)。デフォルトの設定では緑色。\r
+                       /// </summary>\r
+                       Normal = 0,\r
+                       /// <summary>\r
+                       /// エラーの状態(PBST_ERROR)。デフォルトの設定では赤色。\r
+                       /// </summary>\r
+                       Error = 1,\r
+                       /// <summary>\r
+                       /// 停止の状態(PBST_PAUSED)。デフォルトの設定では黄色。\r
+                       /// </summary>\r
+                       Paused = 2,\r
+               }\r
+               \r
+               /// <summary>\r
+               /// WindowsVista以降向け、プログレスバーステータス(色)を設定する。\r
                /// </summary>\r
                /// <param name="progBar">対象のプログレスバー</param>\r
-               /// <param name="state">状態。(1:Normal,2:Error,3:Paused)</param>\r
-               public static void ProgressBar_SetState(ProgressBar progBar, uint state)\r
+               /// <param name="state">状態(色)</param>\r
+               public static void ProgressBar_SetState(ProgressBar progBar, ProgressBarState state)\r
                {\r
 #if USE_VISTA_EFFECTS\r
                        try {\r
-                               // status := (PBST_NORMAL | PBST_ERROR | PBST_PAUSED)\r
                                // SendMessage(progressBar.Handle, PBM_SETSTATE, state, 0);\r
-                               SendMessage(progBar.Handle, 0x410, new IntPtr(state), IntPtr.Zero);\r
+                               SendMessage(progBar.Handle, 0x410, new IntPtr((int) state), IntPtr.Zero);\r
                        } catch (Exception) {\r
                        }\r
 #endif\r
                }\r
                \r
+               #region タスクバー上の進捗表示のためのP/Invoke\r
+                       \r
+               internal enum TBPFLAG\r
+               {\r
+                       TBPF_NOPROGRESS = 0,\r
+                       TBPF_INDETERMINATE = 0x1,\r
+                       TBPF_NORMAL = 0x2,\r
+                       TBPF_ERROR = 0x4,\r
+                       TBPF_PAUSED = 0x8,\r
+               }\r
+               \r
+               [ComImport()]\r
+               [Guid("EA1AFB91-9E28-4B86-90E9-9E9F8A5EEFAF")]\r
+               [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]\r
+               internal interface ITaskList3\r
+               {\r
+                       void HrInit();\r
+                       void AddTab(IntPtr hWnd);\r
+                       void DeleteTab(IntPtr hWnd);\r
+                       void ActivateTab(IntPtr hWnd);\r
+                       void SetActiveAlt(IntPtr hWnd);\r
+                       void MarkFullscreenWindow(IntPtr hWnd, int fFullscreen);\r
+                       void SetProgressValue(IntPtr hWnd, ulong ullCompleted, ulong ullTotal);\r
+                       void SetProgressState(IntPtr hWnd, TBPFLAG tbpFlags);\r
+                       // more functions follows, but we do not need these.\r
+               }\r
+               \r
+               [ComImport()]\r
+               [Guid("56FDF344-FD6D-11d0-958A-006097C9A090")]\r
+               [ClassInterface(ClassInterfaceType.None)]\r
+               internal class CTaskbarList\r
+               {\r
+               }\r
+               \r
+               #endregion\r
+               \r
+               /// <summary>\r
+               /// タスクバに進捗を設定する。\r
+               /// </summary>\r
+               /// <param name="form">対象フォーム</param>\r
+               /// <param name="style">表示スタイル</param>\r
+               /// <param name="state">ステータス(色)</param>\r
+               /// <param name="val">プログレスバーの値。0を指定すると進捗表示をしない</param>\r
+               /// <param name="maximum">プログレスバーの最大値。</param>\r
+               public static void Form_SetTaskbarProgressParams(Form form, ProgressBarStyle style, ProgressBarState state, ulong val, ulong maximum)\r
+               {\r
+#if USE_VISTA_EFFECT\r
+#if USE_SEVEN_EFFECT\r
+                       ITaskList3 tasklist = null;\r
+                       \r
+                       try {\r
+                               TBPFLAG tbpFlag = TBPFLAG.TBPF_NOPROGRESS;\r
+                               \r
+                               if (style == ProgressBarStyle.Marquee) {\r
+                                       tbpFlag |= TBPFLAG.TBPF_INDETERMINATE;\r
+                               } else if (val > 0 && maximum > 0) {\r
+                                       tbpFlag |= TBPFLAG.TBPF_NORMAL;\r
+                               }\r
+                               \r
+                               if (state == ProgressBarState.Error) {\r
+                                       tbpFlag |= TBPFLAG.TBPF_ERROR;\r
+                               }\r
+                               if (state == ProgressBarState.Paused) {\r
+                                       tbpFlag |= TBPFLAG.TBPF_PAUSED;\r
+                               }\r
+                               \r
+                               tasklist = (ITaskList3) new CTaskbarList();\r
+                               tasklist.HrInit();\r
+                               \r
+                               tasklist.SetProgressState(form.Handle, tbpFlag);\r
+                               tasklist.SetProgressValue(form.Handle, val, maximum);\r
+                       } finally {\r
+                               if (tasklist != null) {\r
+                                       Marshal.ReleaseComObject(tasklist);\r
+                                       tasklist = null;\r
+                               }\r
+                       }\r
+#endif\r
+#endif\r
+               }\r
+               \r
+               #endregion\r
+                       \r
                #region タスクバーおよびタイトルバーのフラッシュ\r
                \r
                /// <summary>\r