Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Text
- Imports System.Runtime.InteropServices
- 'Credits to: Pietschsoft
- 'Origionally found on: http://pietschsoft.com/post/2009/08/17/CSharp-IProgressDialog-Show-Native-Progress-Dialog-from-dotNet-in-Windows.aspx
- Public Class IProgressDialog
- Private _parentHandle As IntPtr
- Private pd As Win32IProgressDialog = Nothing
- Public Sub New(parentHandle As IntPtr)
- Me._parentHandle = parentHandle
- End Sub
- Public Sub ShowDialog(ParamArray flags As PROGDLG())
- If pd Is Nothing Then
- pd = DirectCast(New Win32ProgressDialog(), Win32IProgressDialog)
- pd.SetTitle(Me._Title)
- pd.SetCancelMsg(Me._CancelMessage, Nothing)
- pd.SetLine(1, Me._Line1, False, IntPtr.Zero)
- pd.SetLine(2, Me._Line2, False, IntPtr.Zero)
- pd.SetLine(3, Me._Line3, False, IntPtr.Zero)
- Dim dialogFlags As PROGDLG = PROGDLG.Normal
- If flags.Length <> 0 Then
- dialogFlags = flags(0)
- For i As Integer = 1 To flags.Length - 1
- dialogFlags = dialogFlags Or flags(i)
- Next
- End If
- pd.StartProgressDialog(Me._parentHandle, Nothing, dialogFlags, IntPtr.Zero)
- End If
- End Sub
- Public Sub CloseDialog()
- If pd IsNot Nothing Then
- pd.StopProgressDialog()
- 'Marshal.ReleaseComObject(pd);
- pd = Nothing
- End If
- End Sub
- #Region "Properties"
- Private _Title As String = String.Empty
- Public Property Title() As String
- Get
- Return Me._Title
- End Get
- Set(value As String)
- Me._Title = value
- If pd IsNot Nothing Then
- pd.SetTitle(Me._Title)
- End If
- End Set
- End Property
- Private _CancelMessage As String = String.Empty
- Public Property CancelMessage() As String
- Get
- Return Me._CancelMessage
- End Get
- Set(value As String)
- Me._CancelMessage = value
- If pd IsNot Nothing Then
- pd.SetCancelMsg(Me._CancelMessage, Nothing)
- End If
- End Set
- End Property
- Private _Line1 As String = String.Empty
- Public Property Line1() As String
- Get
- Return Me._Line1
- End Get
- Set(value As String)
- Me._Line1 = value
- If pd IsNot Nothing Then
- pd.SetLine(1, Me._Line1, False, IntPtr.Zero)
- End If
- End Set
- End Property
- Private _Line2 As String = String.Empty
- Public Property Line2() As String
- Get
- Return Me._Line2
- End Get
- Set(value As String)
- Me._Line2 = value
- If pd IsNot Nothing Then
- pd.SetLine(2, Me._Line2, False, IntPtr.Zero)
- End If
- End Set
- End Property
- Private _Line3 As String = String.Empty
- Public Property Line3() As String
- Get
- Return Me._Line3
- End Get
- Set(value As String)
- Me._Line3 = value
- If pd IsNot Nothing Then
- pd.SetLine(3, Me._Line3, False, IntPtr.Zero)
- End If
- End Set
- End Property
- Private _value As UInteger = 0
- Public Property Value() As UInteger
- Get
- Return Me._value
- End Get
- Set(value As UInteger)
- Me._value = value
- If pd IsNot Nothing Then
- pd.SetProgress(Me._value, Me._maximum)
- End If
- End Set
- End Property
- Private _maximum As UInteger = 100
- Public Property Maximum() As UInteger
- Get
- Return Me._maximum
- End Get
- Set(value As UInteger)
- Me._maximum = value
- If pd IsNot Nothing Then
- pd.SetProgress(Me._value, Me._maximum)
- End If
- End Set
- End Property
- Public ReadOnly Property HasUserCancelled() As Boolean
- Get
- If pd IsNot Nothing Then
- Return pd.HasUserCancelled()
- Else
- Return False
- End If
- End Get
- End Property
- #End Region
- #Region "Win32 Stuff"
- ' The below was copied from: http://pinvoke.net/default.aspx/Interfaces/IProgressDialog.html
- Public NotInheritable Class shlwapi
- Private Sub New()
- End Sub
- <DllImport("shlwapi.dll", CharSet:=CharSet.Auto)> _
- Private Shared Function PathCompactPath(hDC As IntPtr, <[In](), Out()> pszPath As StringBuilder, dx As Integer) As Boolean
- End Function
- End Class
- <ComImport()> _
- <Guid("EBBC7C04-315E-11d2-B62F-006097DF5BD4")> _
- <InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
- Public Interface Win32IProgressDialog
- ''' <summary>
- ''' Starts the progress dialog box.
- ''' </summary>
- ''' <param name="hwndParent">A handle to the dialog box's parent window.</param>
- ''' <param name="punkEnableModless">Reserved. Set to null.</param>
- ''' <param name="dwFlags">Flags that control the operation of the progress dialog box. </param>
- ''' <param name="pvResevered">Reserved. Set to IntPtr.Zero</param>
- 'HWND
- 'IUnknown
- 'DWORD
- 'LPCVOID
- Sub StartProgressDialog(hwndParent As IntPtr, <MarshalAs(UnmanagedType.IUnknown)> punkEnableModless As Object, dwFlags As PROGDLG, pvResevered As IntPtr)
- ''' <summary>
- ''' Stops the progress dialog box and removes it from the screen.
- ''' </summary>
- Sub StopProgressDialog()
- ''' <summary>
- ''' Sets the title of the progress dialog box.
- ''' </summary>
- ''' <param name="pwzTitle">A pointer to a null-terminated Unicode string that contains the dialog box title.</param>
- 'LPCWSTR
- Sub SetTitle(<MarshalAs(UnmanagedType.LPWStr)> pwzTitle As String)
- ''' <summary>
- ''' 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.
- ''' </summary>
- ''' <param name="hInstAnimation">An instance handle to the module from which the AVI resource should be loaded.</param>
- ''' <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>
- 'HINSTANCE
- 'UINT
- Sub SetAnimation(hInstAnimation As IntPtr, idAnimation As UShort)
- ''' <summary>
- ''' Checks whether the user has canceled the operation.
- ''' </summary>
- ''' <returns>TRUE if the user has cancelled the operation; otherwise, FALSE.</returns>
- ''' <remarks>
- ''' The system does not send a message to the application when the user clicks the Cancel button.
- ''' You must periodically use this function to poll the progress dialog box object to determine
- ''' whether the operation has been canceled.
- ''' </remarks>
- <PreserveSig()> _
- Function HasUserCancelled() As <MarshalAs(UnmanagedType.Bool)> Boolean
- ''' <summary>
- ''' Updates the progress dialog box with the current state of the operation.
- ''' </summary>
- ''' <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>
- ''' <param name="dwTotal">An application-defined value that specifies what value dwCompleted will have when the operation is complete.</param>
- 'DWORD
- 'DWORD
- Sub SetProgress(dwCompleted As UInteger, dwTotal As UInteger)
- ''' <summary>
- ''' Updates the progress dialog box with the current state of the operation.
- ''' </summary>
- ''' <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>
- ''' <param name="ullTotal">An application-defined value that specifies what value ullCompleted will have when the operation is complete.</param>
- 'ULONGLONG
- 'ULONGLONG
- Sub SetProgress64(ullCompleted As ULong, ullTotal As ULong)
- ''' <summary>
- ''' Displays a message in the progress dialog.
- ''' </summary>
- ''' <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>
- ''' <param name="pwzString">A null-terminated Unicode string that contains the text.</param>
- ''' <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>
- ''' <param name="pvResevered"> Reserved. Set to IntPtr.Zero.</param>
- ''' <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>
- 'DWORD
- 'LPCWSTR
- 'BOOL
- 'LPCVOID
- Sub SetLine(dwLineNum As UInteger, <MarshalAs(UnmanagedType.LPWStr)> pwzString As String, <MarshalAs(UnmanagedType.VariantBool)> fCompactPath As Boolean, pvResevered As IntPtr)
- ''' <summary>
- ''' Sets a message to be displayed if the user cancels the operation.
- ''' </summary>
- ''' <param name="pwzCancelMsg">A pointer to a null-terminated Unicode string that contains the message to be displayed.</param>
- ''' <param name="pvResevered">Reserved. Set to NULL.</param>
- ''' <remarks>Even though the user clicks Cancel, the application cannot immediately call
- ''' IProgressDialog::StopProgressDialog to close the dialog box. The application must wait until the
- ''' next time it calls IProgressDialog::HasUserCancelled to discover that the user has canceled the
- ''' operation. Since this delay might be significant, the progress dialog box provides the user with
- ''' immediate feedback by clearing text lines 1 and 2 and displaying the cancel message on line 3.
- ''' The message is intended to let the user know that the delay is normal and that the progress dialog
- ''' box will be closed shortly.
- ''' It is typically is set to something like "Please wait while ...". </remarks>
- 'LPCWSTR
- 'LPCVOID
- Sub SetCancelMsg(<MarshalAs(UnmanagedType.LPWStr)> pwzCancelMsg As String, pvResevered As Object)
- ''' <summary>
- ''' Resets the progress dialog box timer to zero.
- ''' </summary>
- ''' <param name="dwTimerAction">Flags that indicate the action to be taken by the timer.</param>
- ''' <param name="pvResevered">Reserved. Set to NULL.</param>
- ''' <remarks>
- ''' The timer is used to estimate the remaining time. It is started when your application
- ''' calls IProgressDialog::StartProgressDialog. Unless your application will start immediately,
- ''' it should call Timer just before starting the operation.
- ''' This practice ensures that the time estimates will be as accurate as possible. This method
- ''' should not be called after the first call to IProgressDialog::SetProgress.</remarks>
- 'DWORD
- 'LPCVOID
- Sub Timer(dwTimerAction As PDTIMER, pvResevered As Object)
- End Interface
- <ComImport()> _
- <Guid("F8383852-FCD3-11d1-A6B9-006097DF5BD4")> _
- Public Class Win32ProgressDialog
- End Class
- ''' <summary>
- ''' Flags that indicate the action to be taken by the ProgressDialog.SetTime() method.
- ''' </summary>
- Public Enum PDTIMER As UInteger
- 'DWORD
- ''' <summary>Resets the timer to zero. Progress will be calculated from the time this method is called.</summary>
- Reset = (&H1)
- ''' <summary>Progress has been suspended.</summary>
- Pause = (&H2)
- ''' <summary>Progress has been resumed.</summary>
- [Resume] = (&H3)
- End Enum
- <Flags()> _
- Public Enum PROGDLG As UInteger
- 'DWORD
- ''' <summary>Normal progress dialog box behavior.</summary>
- Normal = &H0
- ''' <summary>The progress dialog box will be modal to the window specified by hwndParent. By default, a progress dialog box is modeless.</summary>
- Modal = &H1
- ''' <summary>Automatically estimate the remaining time and display the estimate on line 3. </summary>
- ''' <remarks>If this flag is set, IProgressDialog::SetLine can be used only to display text on lines 1 and 2.</remarks>
- AutoTime = &H2
- ''' <summary>Do not show the "time remaining" text.</summary>
- NoTime = &H4
- ''' <summary>Do not display a minimize button on the dialog box's caption bar.</summary>
- NoMinimize = &H8
- ''' <summary>Do not display a progress bar.</summary>
- ''' <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>
- NoProgressBar = &H10
- End Enum
- #End Region
- End Class
Advertisement
Add Comment
Please, Sign In to add comment