Ethernal_Five

[VB.Net] IProgressDialog Class

Feb 12th, 2012
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 13.69 KB | None | 0 0
  1. Imports System.Text
  2. Imports System.Runtime.InteropServices
  3.  
  4. 'Credits to: Pietschsoft
  5. 'Origionally found on: http://pietschsoft.com/post/2009/08/17/CSharp-IProgressDialog-Show-Native-Progress-Dialog-from-dotNet-in-Windows.aspx
  6.  
  7. Public Class IProgressDialog
  8.     Private _parentHandle As IntPtr
  9.     Private pd As Win32IProgressDialog = Nothing
  10.     Public Sub New(parentHandle As IntPtr)
  11.         Me._parentHandle = parentHandle
  12.     End Sub
  13.  
  14.     Public Sub ShowDialog(ParamArray flags As PROGDLG())
  15.         If pd Is Nothing Then
  16.             pd = DirectCast(New Win32ProgressDialog(), Win32IProgressDialog)
  17.  
  18.             pd.SetTitle(Me._Title)
  19.             pd.SetCancelMsg(Me._CancelMessage, Nothing)
  20.             pd.SetLine(1, Me._Line1, False, IntPtr.Zero)
  21.             pd.SetLine(2, Me._Line2, False, IntPtr.Zero)
  22.             pd.SetLine(3, Me._Line3, False, IntPtr.Zero)
  23.  
  24.             Dim dialogFlags As PROGDLG = PROGDLG.Normal
  25.             If flags.Length <> 0 Then
  26.                 dialogFlags = flags(0)
  27.                 For i As Integer = 1 To flags.Length - 1
  28.                     dialogFlags = dialogFlags Or flags(i)
  29.                 Next
  30.             End If
  31.  
  32.             pd.StartProgressDialog(Me._parentHandle, Nothing, dialogFlags, IntPtr.Zero)
  33.         End If
  34.     End Sub
  35.  
  36.     Public Sub CloseDialog()
  37.         If pd IsNot Nothing Then
  38.             pd.StopProgressDialog()
  39.             'Marshal.ReleaseComObject(pd);
  40.             pd = Nothing
  41.         End If
  42.     End Sub
  43.  
  44. #Region "Properties"
  45.     Private _Title As String = String.Empty
  46.     Public Property Title() As String
  47.         Get
  48.             Return Me._Title
  49.         End Get
  50.         Set(value As String)
  51.             Me._Title = value
  52.             If pd IsNot Nothing Then
  53.                 pd.SetTitle(Me._Title)
  54.             End If
  55.         End Set
  56.     End Property
  57.  
  58.     Private _CancelMessage As String = String.Empty
  59.     Public Property CancelMessage() As String
  60.         Get
  61.             Return Me._CancelMessage
  62.         End Get
  63.         Set(value As String)
  64.             Me._CancelMessage = value
  65.             If pd IsNot Nothing Then
  66.                 pd.SetCancelMsg(Me._CancelMessage, Nothing)
  67.             End If
  68.         End Set
  69.     End Property
  70.  
  71.     Private _Line1 As String = String.Empty
  72.     Public Property Line1() As String
  73.         Get
  74.             Return Me._Line1
  75.         End Get
  76.         Set(value As String)
  77.             Me._Line1 = value
  78.             If pd IsNot Nothing Then
  79.                 pd.SetLine(1, Me._Line1, False, IntPtr.Zero)
  80.             End If
  81.         End Set
  82.     End Property
  83.  
  84.     Private _Line2 As String = String.Empty
  85.     Public Property Line2() As String
  86.         Get
  87.             Return Me._Line2
  88.         End Get
  89.         Set(value As String)
  90.             Me._Line2 = value
  91.             If pd IsNot Nothing Then
  92.                 pd.SetLine(2, Me._Line2, False, IntPtr.Zero)
  93.             End If
  94.         End Set
  95.     End Property
  96.  
  97.     Private _Line3 As String = String.Empty
  98.     Public Property Line3() As String
  99.         Get
  100.             Return Me._Line3
  101.         End Get
  102.         Set(value As String)
  103.             Me._Line3 = value
  104.             If pd IsNot Nothing Then
  105.                 pd.SetLine(3, Me._Line3, False, IntPtr.Zero)
  106.             End If
  107.         End Set
  108.     End Property
  109.  
  110.     Private _value As UInteger = 0
  111.     Public Property Value() As UInteger
  112.         Get
  113.             Return Me._value
  114.         End Get
  115.         Set(value As UInteger)
  116.             Me._value = value
  117.             If pd IsNot Nothing Then
  118.                 pd.SetProgress(Me._value, Me._maximum)
  119.             End If
  120.         End Set
  121.     End Property
  122.  
  123.     Private _maximum As UInteger = 100
  124.     Public Property Maximum() As UInteger
  125.         Get
  126.             Return Me._maximum
  127.         End Get
  128.         Set(value As UInteger)
  129.             Me._maximum = value
  130.             If pd IsNot Nothing Then
  131.                 pd.SetProgress(Me._value, Me._maximum)
  132.             End If
  133.         End Set
  134.     End Property
  135.     Public ReadOnly Property HasUserCancelled() As Boolean
  136.         Get
  137.             If pd IsNot Nothing Then
  138.                 Return pd.HasUserCancelled()
  139.             Else
  140.                 Return False
  141.             End If
  142.         End Get
  143.     End Property
  144. #End Region
  145.  
  146. #Region "Win32 Stuff"
  147.     ' The below was copied from: http://pinvoke.net/default.aspx/Interfaces/IProgressDialog.html
  148.  
  149.     Public NotInheritable Class shlwapi
  150.         Private Sub New()
  151.         End Sub
  152.         <DllImport("shlwapi.dll", CharSet:=CharSet.Auto)> _
  153.         Private Shared Function PathCompactPath(hDC As IntPtr, <[In](), Out()> pszPath As StringBuilder, dx As Integer) As Boolean
  154.         End Function
  155.     End Class
  156.  
  157.     <ComImport()> _
  158.     <Guid("EBBC7C04-315E-11d2-B62F-006097DF5BD4")> _
  159.     <InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
  160.     Public Interface Win32IProgressDialog
  161.         ''' <summary>
  162.         ''' Starts the progress dialog box.
  163.         ''' </summary>
  164.         ''' <param name="hwndParent">A handle to the dialog box's parent window.</param>
  165.         ''' <param name="punkEnableModless">Reserved. Set to null.</param>
  166.         ''' <param name="dwFlags">Flags that control the operation of the progress dialog box. </param>
  167.         ''' <param name="pvResevered">Reserved. Set to IntPtr.Zero</param>
  168.         'HWND
  169.         'IUnknown
  170.         'DWORD
  171.         'LPCVOID
  172.         Sub StartProgressDialog(hwndParent As IntPtr, <MarshalAs(UnmanagedType.IUnknown)> punkEnableModless As Object, dwFlags As PROGDLG, pvResevered As IntPtr)
  173.  
  174.         ''' <summary>
  175.         ''' Stops the progress dialog box and removes it from the screen.
  176.         ''' </summary>
  177.         Sub StopProgressDialog()
  178.  
  179.         ''' <summary>
  180.         ''' Sets the title of the progress dialog box.
  181.         ''' </summary>
  182.         ''' <param name="pwzTitle">A pointer to a null-terminated Unicode string that contains the dialog box title.</param>
  183.         'LPCWSTR
  184.         Sub SetTitle(<MarshalAs(UnmanagedType.LPWStr)> pwzTitle As String)
  185.  
  186.         ''' <summary>
  187.         ''' 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.
  188.         ''' </summary>
  189.         ''' <param name="hInstAnimation">An instance handle to the module from which the AVI resource should be loaded.</param>
  190.         ''' <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>
  191.         'HINSTANCE
  192.         'UINT
  193.         Sub SetAnimation(hInstAnimation As IntPtr, idAnimation As UShort)
  194.  
  195.         ''' <summary>
  196.         ''' Checks whether the user has canceled the operation.
  197.         ''' </summary>
  198.         ''' <returns>TRUE if the user has cancelled the operation; otherwise, FALSE.</returns>
  199.         ''' <remarks>
  200.         ''' The system does not send a message to the application when the user clicks the Cancel button.
  201.         ''' You must periodically use this function to poll the progress dialog box object to determine
  202.         ''' whether the operation has been canceled.
  203.         ''' </remarks>
  204.         <PreserveSig()> _
  205.         Function HasUserCancelled() As <MarshalAs(UnmanagedType.Bool)> Boolean
  206.  
  207.         ''' <summary>
  208.         ''' Updates the progress dialog box with the current state of the operation.
  209.         ''' </summary>
  210.         ''' <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>
  211.         ''' <param name="dwTotal">An application-defined value that specifies what value dwCompleted will have when the operation is complete.</param>
  212.         'DWORD
  213.         'DWORD
  214.         Sub SetProgress(dwCompleted As UInteger, dwTotal As UInteger)
  215.  
  216.         ''' <summary>
  217.         ''' Updates the progress dialog box with the current state of the operation.
  218.         ''' </summary>
  219.         ''' <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>
  220.         ''' <param name="ullTotal">An application-defined value that specifies what value ullCompleted will have when the operation is complete.</param>
  221.         'ULONGLONG
  222.         'ULONGLONG
  223.         Sub SetProgress64(ullCompleted As ULong, ullTotal As ULong)
  224.  
  225.         ''' <summary>
  226.         ''' Displays a message in the progress dialog.
  227.         ''' </summary>
  228.         ''' <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>
  229.         ''' <param name="pwzString">A null-terminated Unicode string that contains the text.</param>
  230.         ''' <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>
  231.         ''' <param name="pvResevered"> Reserved. Set to IntPtr.Zero.</param>
  232.         ''' <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>
  233.         'DWORD
  234.         'LPCWSTR
  235.         'BOOL
  236.         'LPCVOID
  237.         Sub SetLine(dwLineNum As UInteger, <MarshalAs(UnmanagedType.LPWStr)> pwzString As String, <MarshalAs(UnmanagedType.VariantBool)> fCompactPath As Boolean, pvResevered As IntPtr)
  238.  
  239.         ''' <summary>
  240.         ''' Sets a message to be displayed if the user cancels the operation.
  241.         ''' </summary>
  242.         ''' <param name="pwzCancelMsg">A pointer to a null-terminated Unicode string that contains the message to be displayed.</param>
  243.         ''' <param name="pvResevered">Reserved. Set to NULL.</param>
  244.         ''' <remarks>Even though the user clicks Cancel, the application cannot immediately call
  245.         ''' IProgressDialog::StopProgressDialog to close the dialog box. The application must wait until the
  246.         ''' next time it calls IProgressDialog::HasUserCancelled to discover that the user has canceled the
  247.         ''' operation. Since this delay might be significant, the progress dialog box provides the user with
  248.         ''' immediate feedback by clearing text lines 1 and 2 and displaying the cancel message on line 3.
  249.         ''' The message is intended to let the user know that the delay is normal and that the progress dialog
  250.         ''' box will be closed shortly.
  251.         ''' It is typically is set to something like "Please wait while ...". </remarks>
  252.         'LPCWSTR
  253.         'LPCVOID
  254.         Sub SetCancelMsg(<MarshalAs(UnmanagedType.LPWStr)> pwzCancelMsg As String, pvResevered As Object)
  255.  
  256.         ''' <summary>
  257.         ''' Resets the progress dialog box timer to zero.
  258.         ''' </summary>
  259.         ''' <param name="dwTimerAction">Flags that indicate the action to be taken by the timer.</param>
  260.         ''' <param name="pvResevered">Reserved. Set to NULL.</param>
  261.         ''' <remarks>
  262.         ''' The timer is used to estimate the remaining time. It is started when your application
  263.         ''' calls IProgressDialog::StartProgressDialog. Unless your application will start immediately,
  264.         ''' it should call Timer just before starting the operation.
  265.         ''' This practice ensures that the time estimates will be as accurate as possible. This method
  266.         ''' should not be called after the first call to IProgressDialog::SetProgress.</remarks>
  267.         'DWORD
  268.         'LPCVOID
  269.         Sub Timer(dwTimerAction As PDTIMER, pvResevered As Object)
  270.  
  271.     End Interface
  272.  
  273.     <ComImport()> _
  274.     <Guid("F8383852-FCD3-11d1-A6B9-006097DF5BD4")> _
  275.     Public Class Win32ProgressDialog
  276.     End Class
  277.  
  278.     ''' <summary>
  279.     ''' Flags that indicate the action to be taken by the ProgressDialog.SetTime() method.
  280.     ''' </summary>
  281.     Public Enum PDTIMER As UInteger
  282.         'DWORD
  283.         ''' <summary>Resets the timer to zero. Progress will be calculated from the time this method is called.</summary>
  284.         Reset = (&H1)
  285.         ''' <summary>Progress has been suspended.</summary>
  286.         Pause = (&H2)
  287.         ''' <summary>Progress has been resumed.</summary>
  288.         [Resume] = (&H3)
  289.     End Enum
  290.  
  291.     <Flags()> _
  292.     Public Enum PROGDLG As UInteger
  293.         'DWORD
  294.         ''' <summary>Normal progress dialog box behavior.</summary>
  295.         Normal = &H0
  296.         ''' <summary>The progress dialog box will be modal to the window specified by hwndParent. By default, a progress dialog box is modeless.</summary>
  297.         Modal = &H1
  298.         ''' <summary>Automatically estimate the remaining time and display the estimate on line 3. </summary>
  299.         ''' <remarks>If this flag is set, IProgressDialog::SetLine can be used only to display text on lines 1 and 2.</remarks>
  300.         AutoTime = &H2
  301.         ''' <summary>Do not show the "time remaining" text.</summary>
  302.         NoTime = &H4
  303.         ''' <summary>Do not display a minimize button on the dialog box's caption bar.</summary>
  304.         NoMinimize = &H8
  305.         ''' <summary>Do not display a progress bar.</summary>
  306.         ''' <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>
  307.         NoProgressBar = &H10
  308.     End Enum
  309. #End Region
  310. End Class
Advertisement
Add Comment
Please, Sign In to add comment