Ethernal_Five

[C#] IProgressDialog Class

Feb 12th, 2012
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.62 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Runtime.InteropServices;
  4.  
  5. //Credits to: Pietschsoft
  6. //Origionally found on: http://pietschsoft.com/post/2009/08/17/CSharp-IProgressDialog-Show-Native-Progress-Dialog-from-dotNet-in-Windows.aspx
  7.  
  8. public class IProgressDialog
  9. {
  10.     private IntPtr _parentHandle;
  11.     private Win32IProgressDialog pd = null;
  12.     public IProgressDialog(IntPtr parentHandle)
  13.     {
  14.         this._parentHandle = parentHandle;
  15.     }
  16.  
  17.     public void ShowDialog(params PROGDLG[] flags)
  18.     {
  19.         if (pd == null)
  20.         {
  21.             pd = (Win32IProgressDialog)new Win32ProgressDialog();
  22.  
  23.             pd.SetTitle(this._Title);
  24.             pd.SetCancelMsg(this._CancelMessage, null);
  25.             pd.SetLine(1, this._Line1, false, IntPtr.Zero);
  26.             pd.SetLine(2, this._Line2, false, IntPtr.Zero);
  27.             pd.SetLine(3, this._Line3, false, IntPtr.Zero);
  28.  
  29.             PROGDLG dialogFlags = PROGDLG.Normal;
  30.             if (flags.Length != 0)
  31.             {
  32.                 dialogFlags = flags[0];
  33.                 for (var i = 1; i < flags.Length; i++)
  34.                 {
  35.                     dialogFlags = dialogFlags | flags[i];
  36.                 }
  37.             }
  38.  
  39.             pd.StartProgressDialog(this._parentHandle, null, dialogFlags, IntPtr.Zero);
  40.         }
  41.     }
  42.  
  43.     public void CloseDialog()
  44.     {
  45.         if (pd != null)
  46.         {
  47.             pd.StopProgressDialog();
  48.             //Marshal.ReleaseComObject(pd);
  49.             pd = null;
  50.         }
  51.     }
  52.  
  53.     #region Properties
  54.     private string _Title = string.Empty;
  55.     public string Title
  56.     {
  57.         get
  58.         {
  59.             return this._Title;
  60.         }
  61.         set
  62.         {
  63.             this._Title = value;
  64.             if (pd != null)
  65.             {
  66.                 pd.SetTitle(this._Title);
  67.             }
  68.         }
  69.     }
  70.  
  71.     private string _CancelMessage = string.Empty;
  72.     public string CancelMessage
  73.     {
  74.         get
  75.         {
  76.             return this._CancelMessage;
  77.         }
  78.         set
  79.         {
  80.             this._CancelMessage = value;
  81.             if (pd != null)
  82.             {
  83.                 pd.SetCancelMsg(this._CancelMessage, null);
  84.             }
  85.         }
  86.     }
  87.  
  88.     private string _Line1 = string.Empty;
  89.     public string Line1
  90.     {
  91.         get
  92.         {
  93.             return this._Line1;
  94.         }
  95.         set
  96.         {
  97.             this._Line1 = value;
  98.             if (pd != null)
  99.             {
  100.                 pd.SetLine(1, this._Line1, false, IntPtr.Zero);
  101.             }
  102.         }
  103.     }
  104.  
  105.     private string _Line2 = string.Empty;
  106.     public string Line2
  107.     {
  108.         get
  109.         {
  110.             return this._Line2;
  111.         }
  112.         set
  113.         {
  114.             this._Line2 = value;
  115.             if (pd != null)
  116.             {
  117.                 pd.SetLine(2, this._Line2, false, IntPtr.Zero);
  118.             }
  119.         }
  120.     }
  121.  
  122.     private string _Line3 = string.Empty;
  123.     public string Line3
  124.     {
  125.         get
  126.         {
  127.             return this._Line3;
  128.         }
  129.         set
  130.         {
  131.             this._Line3 = value;
  132.             if (pd != null)
  133.             {
  134.                 pd.SetLine(3, this._Line3, false, IntPtr.Zero);
  135.             }
  136.         }
  137.     }
  138.  
  139.     private uint _value = 0;
  140.     public uint Value
  141.     {
  142.         get
  143.         {
  144.             return this._value;
  145.         }
  146.         set
  147.         {
  148.             this._value = value;
  149.             if (pd != null)
  150.             {
  151.                 pd.SetProgress(this._value, this._maximum);
  152.             }
  153.         }
  154.     }
  155.  
  156.     private uint _maximum = 100;
  157.     public uint Maximum
  158.     {
  159.         get
  160.         {
  161.             return this._maximum;
  162.         }
  163.         set
  164.         {
  165.             this._maximum = value;
  166.             if (pd != null)
  167.             {
  168.                 pd.SetProgress(this._value, this._maximum);
  169.             }
  170.         }
  171.     }
  172.     public bool HasUserCancelled
  173.     {
  174.         get
  175.         {
  176.             if (pd != null)
  177.             {
  178.                 return pd.HasUserCancelled();
  179.             }
  180.             else
  181.                 return false;
  182.         }
  183.     }
  184.     #endregion
  185.  
  186.     #region Win32 Stuff
  187.     // The below was copied from: http://pinvoke.net/default.aspx/Interfaces/IProgressDialog.html
  188.  
  189.     public static class shlwapi
  190.     {
  191.         [DllImport("shlwapi.dll", CharSet = CharSet.Auto)]
  192.         static extern bool PathCompactPath(IntPtr hDC, [In, Out] StringBuilder pszPath, int dx);
  193.     }
  194.  
  195.     [ComImport]
  196.     [Guid("EBBC7C04-315E-11d2-B62F-006097DF5BD4")]
  197.     [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  198.     public interface Win32IProgressDialog
  199.     {
  200.         /// <summary>
  201.         /// Starts the progress dialog box.
  202.         /// </summary>
  203.         /// <param name="hwndParent">A handle to the dialog box's parent window.</param>
  204.         /// <param name="punkEnableModless">Reserved. Set to null.</param>
  205.         /// <param name="dwFlags">Flags that control the operation of the progress dialog box. </param>
  206.         /// <param name="pvResevered">Reserved. Set to IntPtr.Zero</param>
  207.         void StartProgressDialog(
  208.             IntPtr hwndParent, //HWND
  209.             [MarshalAs(UnmanagedType.IUnknown)]    object punkEnableModless, //IUnknown
  210.             PROGDLG dwFlags,  //DWORD
  211.             IntPtr pvResevered //LPCVOID
  212.             );
  213.  
  214.         /// <summary>
  215.         /// Stops the progress dialog box and removes it from the screen.
  216.         /// </summary>
  217.         void StopProgressDialog();
  218.  
  219.         /// <summary>
  220.         /// Sets the title of the progress dialog box.
  221.         /// </summary>
  222.         /// <param name="pwzTitle">A pointer to a null-terminated Unicode string that contains the dialog box title.</param>
  223.         void SetTitle(
  224.             [MarshalAs(UnmanagedType.LPWStr)] string pwzTitle //LPCWSTR
  225.             );
  226.  
  227.         /// <summary>
  228.         /// Specifies an Audio-Video Interleaved (AVI) clip that runs in the dialog box. Note: Note  This method is not supported in Windows Vista or later versions.
  229.         /// </summary>
  230.         /// <param name="hInstAnimation">An instance handle to the module from which the AVI resource should be loaded.</param>
  231.         /// <param name="idAnimation">An AVI resource identifier. To create this value, use the MAKEINTRESOURCE macro. The control loads the AVI resource from the module specified by hInstAnimation.</param>
  232.         void SetAnimation(
  233.             IntPtr hInstAnimation, //HINSTANCE
  234.             ushort idAnimation //UINT
  235.             );
  236.  
  237.         /// <summary>
  238.         /// Checks whether the user has canceled the operation.
  239.         /// </summary>
  240.         /// <returns>TRUE if the user has cancelled the operation; otherwise, FALSE.</returns>
  241.         /// <remarks>
  242.         /// The system does not send a message to the application when the user clicks the Cancel button.
  243.         /// You must periodically use this function to poll the progress dialog box object to determine
  244.         /// whether the operation has been canceled.
  245.         /// </remarks>
  246.         [PreserveSig]
  247.         [return: MarshalAs(UnmanagedType.Bool)]
  248.         bool HasUserCancelled();
  249.  
  250.         /// <summary>
  251.         /// Updates the progress dialog box with the current state of the operation.
  252.         /// </summary>
  253.         /// <param name="dwCompleted">An application-defined value that indicates what proportion of the operation has been completed at the time the method was called.</param>
  254.         /// <param name="dwTotal">An application-defined value that specifies what value dwCompleted will have when the operation is complete.</param>
  255.         void SetProgress(
  256.             uint dwCompleted, //DWORD
  257.             uint dwTotal //DWORD
  258.             );
  259.  
  260.         /// <summary>
  261.         /// Updates the progress dialog box with the current state of the operation.
  262.         /// </summary>
  263.         /// <param name="ullCompleted">An application-defined value that indicates what proportion of the operation has been completed at the time the method was called.</param>
  264.         /// <param name="ullTotal">An application-defined value that specifies what value ullCompleted will have when the operation is complete.</param>
  265.         void SetProgress64(
  266.             ulong ullCompleted, //ULONGLONG
  267.             ulong ullTotal //ULONGLONG
  268.             );
  269.  
  270.         /// <summary>
  271.         /// Displays a message in the progress dialog.
  272.         /// </summary>
  273.         /// <param name="dwLineNum">The line number on which the text is to be displayed. Currently there are three lines—1, 2, and 3. If the PROGDLG_AUTOTIME flag was included in the dwFlags parameter when IProgressDialog::StartProgressDialog was called, only lines 1 and 2 can be used. The estimated time will be displayed on line 3.</param>
  274.         /// <param name="pwzString">A null-terminated Unicode string that contains the text.</param>
  275.         /// <param name="fCompactPath">TRUE to have path strings compacted if they are too large to fit on a line. The paths are compacted with PathCompactPath.</param>
  276.         /// <param name="pvResevered"> Reserved. Set to IntPtr.Zero.</param>
  277.         /// <remarks>This function is typically used to display a message such as "Item XXX is now being processed." typically, messages are displayed on lines 1 and 2, with line 3 reserved for the estimated time.</remarks>
  278.         void SetLine(
  279.             uint dwLineNum, //DWORD
  280.             [MarshalAs(UnmanagedType.LPWStr)] string pwzString, //LPCWSTR
  281.             [MarshalAs(UnmanagedType.VariantBool)] bool fCompactPath, //BOOL
  282.             IntPtr pvResevered //LPCVOID
  283.             );
  284.  
  285.         /// <summary>
  286.         /// Sets a message to be displayed if the user cancels the operation.
  287.         /// </summary>
  288.         /// <param name="pwzCancelMsg">A pointer to a null-terminated Unicode string that contains the message to be displayed.</param>
  289.         /// <param name="pvResevered">Reserved. Set to NULL.</param>
  290.         /// <remarks>Even though the user clicks Cancel, the application cannot immediately call
  291.         /// IProgressDialog::StopProgressDialog to close the dialog box. The application must wait until the
  292.         /// next time it calls IProgressDialog::HasUserCancelled to discover that the user has canceled the
  293.         /// operation. Since this delay might be significant, the progress dialog box provides the user with
  294.         /// immediate feedback by clearing text lines 1 and 2 and displaying the cancel message on line 3.
  295.         /// The message is intended to let the user know that the delay is normal and that the progress dialog
  296.         /// box will be closed shortly.
  297.         /// It is typically is set to something like "Please wait while ...". </remarks>
  298.         void SetCancelMsg(
  299.             [MarshalAs(UnmanagedType.LPWStr)] string pwzCancelMsg, //LPCWSTR
  300.             object pvResevered //LPCVOID
  301.             );
  302.  
  303.         /// <summary>
  304.         /// Resets the progress dialog box timer to zero.
  305.         /// </summary>
  306.         /// <param name="dwTimerAction">Flags that indicate the action to be taken by the timer.</param>
  307.         /// <param name="pvResevered">Reserved. Set to NULL.</param>
  308.         /// <remarks>
  309.         /// The timer is used to estimate the remaining time. It is started when your application
  310.         /// calls IProgressDialog::StartProgressDialog. Unless your application will start immediately,
  311.         /// it should call Timer just before starting the operation.
  312.         /// This practice ensures that the time estimates will be as accurate as possible. This method
  313.         /// should not be called after the first call to IProgressDialog::SetProgress.</remarks>
  314.         void Timer(
  315.             PDTIMER dwTimerAction, //DWORD
  316.             object pvResevered //LPCVOID
  317.             );
  318.  
  319.     }
  320.  
  321.     [ComImport]
  322.     [Guid("F8383852-FCD3-11d1-A6B9-006097DF5BD4")]
  323.     public class Win32ProgressDialog
  324.     {
  325.     }
  326.  
  327.     /// <summary>
  328.     /// Flags that indicate the action to be taken by the ProgressDialog.SetTime() method.
  329.     /// </summary>
  330.     public enum PDTIMER : uint //DWORD
  331.     {
  332.         /// <summary>Resets the timer to zero. Progress will be calculated from the time this method is called.</summary>
  333.         Reset = (0x01),
  334.         /// <summary>Progress has been suspended.</summary>
  335.         Pause = (0x02),
  336.         /// <summary>Progress has been resumed.</summary>
  337.         Resume = (0x03)
  338.     }
  339.  
  340.     [Flags]
  341.     public enum PROGDLG : uint //DWORD
  342.     {
  343.         /// <summary>Normal progress dialog box behavior.</summary>
  344.         Normal = 0x00000000,
  345.         /// <summary>The progress dialog box will be modal to the window specified by hwndParent. By default, a progress dialog box is modeless.</summary>
  346.         Modal = 0x00000001,
  347.         /// <summary>Automatically estimate the remaining time and display the estimate on line 3. </summary>
  348.         /// <remarks>If this flag is set, IProgressDialog::SetLine can be used only to display text on lines 1 and 2.</remarks>
  349.         AutoTime = 0x00000002,
  350.         /// <summary>Do not show the "time remaining" text.</summary>
  351.         NoTime = 0x00000004,
  352.         /// <summary>Do not display a minimize button on the dialog box's caption bar.</summary>
  353.         NoMinimize = 0x00000008,
  354.         /// <summary>Do not display a progress bar.</summary>
  355.         /// <remarks>Typically, an application can quantitatively determine how much of the operation remains and periodically pass that value to IProgressDialog::SetProgress. The progress dialog box uses this information to update its progress bar. This flag is typically set when the calling application must wait for an operation to finish, but does not have any quantitative information it can use to update the dialog box.</remarks>
  356.         NoProgressBar = 0x00000010
  357.     }
  358.     #endregion
  359. }
Advertisement
Add Comment
Please, Sign In to add comment