在调用的窗口里:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.TaskDialog1.OnTask(Nothing, Nothing)
End Sub
Private Sub TaskDialog1_Task(ByVal sender As Object, ByRef e As GaoControl20.TaskEventArgs) Handles TaskDialog1.Task
Dim i As Integer
Do
i += 1
Debug.Print(i)
'e.Title = i & "运行中..."
e.Percent = i
If i > 300 And i < 700 Then
e.CancelButtonVisible = True
Else
e.CancelButtonVisible = False
End If
If e.Cancel = True Then
MsgBox("取消")
Exit Do
End If
Application.DoEvents()
If i > 1000 Then Exit Do
Loop
''' <summary>
''' 一个运行任务的事件
''' </summary>
''' <remarks></remarks>
<System.Drawing.ToolboxBitmap("D:\GaoControl20\GaoControl20\Resources\Wait03.gif")> _
Public Class TaskEventArgs
Inherits EventArgs
Public Delegate Sub CancelButtonVisibleHandler(ByVal sender As Object, ByVal e As EventArgs)
Public Delegate Sub CancelHandler(ByVal sender As Object, ByVal e As EventArgs)
Public Delegate Sub TitleHandler(ByVal sender As Object, ByVal e As EventArgs)
Public Delegate Sub PercentHandler(ByVal sender As Object, ByVal e As EventArgs)
Public Event CancelButtonVisibleChange As CancelHandler
Public Event CancelChange As CancelHandler
Public Event TitleChange As CancelHandler
Public Event PercentChange As CancelHandler
Dim _CancelButtonVisible As Boolean
Dim _Cancel As Boolean
Dim _Title As String
Dim _Percent As Single
Public Sub New()
Me._CancelButtonVisible = False
Me._Cancel = False
Me._Title = "运行中,请稍候..."
Me._Percent = 0
End Sub
''' <summary>
''' 任务运行时提示条上是否显示终止按钮
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property CancelButtonVisible() As Boolean
Get
Return Me._CancelButtonVisible
End Get
Set(ByVal value As Boolean)
Me._CancelButtonVisible = value
RaiseEvent CancelButtonVisibleChange(Me, Nothing)
End Set
End Property
''' <summary>
''' 是否终止运行的任务
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Cancel() As Boolean
Get
Return Me._Cancel
End Get
Set(ByVal value As Boolean)
Me._Cancel = value
RaiseEvent CancelChange(Me, Nothing)
End Set
End Property
''' <summary>
''' 任务运行时提示条上显示的文本信息
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Title() As String
Get
Return Me._Title
End Get
Set(ByVal value As String)
Me._Title = value
RaiseEvent TitleChange(Me, Nothing)
End Set
End Property
''' <summary>
''' 任务运行时提示条上显示的百分比(显示于文本信息之后)
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Percent() As Single
Get
Return Me._Percent
End Get
Set(ByVal value As Single)
Me._Percent = value
RaiseEvent PercentChange(Me, Nothing)
End Set
End Property
End Class
''' <summary>
''' 一个运行任务
''' </summary>
''' <remarks></remarks>
<System.Drawing.ToolboxBitmap("D:\GaoControl20\GaoControl20\Resources\Wait03.gif")> _
<System.ComponentModel.DefaultEvent("Task")> _
Public Class TaskDialog
Public Enum WaitImageStyle
FINDCOMP
Wait01
Wait02
Wait03
Wait04
End Enum
Dim _ImageStyle As WaitImageStyle = WaitImageStyle.Wait03
Dim _FormBorder As FormBorderStyle = FormBorderStyle.FixedDialog
Dim _FormBackColor As Color
WithEvents frm As New Form
'Dim frm As New Form
WithEvents xTaskEventargs As New TaskEventArgs
'Dim xTaskEventargs As TaskEventArgs
Public Delegate Sub TaskHandler(ByVal sender As Object, ByRef e As TaskEventArgs)
''' <summary>
''' 运行任务事件
''' 在事件中添加要执行的代码段
''' </summary>
''' <remarks></remarks>
Public Event Task As TaskHandler
Public Property ImageStyle() As WaitImageStyle
Get
Return Me._ImageStyle
End Get
Set(ByVal value As WaitImageStyle)
Me._ImageStyle = value
End Set
End Property
Public Property FormBorder() As FormBorderStyle
Get
Return Me._FormBorder
End Get
Set(ByVal value As FormBorderStyle)
Me._FormBorder = value
End Set
End Property
Public Property FormBackColor() As Color
Get
Return Me._FormBackColor
End Get
Set(ByVal value As Color)
Me._FormBackColor = value
End Set
End Property
''' <summary>
''' 触发任务
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Public Sub OnTask(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As New Button
Dim pic As New PictureBox
Try
If Not sender Is Nothing Then
Try
sender.Enabled = False
Catch ex As Exception
End Try
End If
Me.frm = New Form
frm.UseWaitCursor = True
frm.Size = New Size(360, 60)
frm.StartPosition = FormStartPosition.CenterScreen
frm.ShowInTaskbar = False
frm.ControlBox = False
frm.Text = ""
frm.TopLevel = True
frm.TopMost = True
frm.FormBorderStyle = Me._FormBorder
frm.BackColor = Me._FormBackColor
With btn
.Text = "终止"
.Size = New Size(50, 22)
.Location = New Point(300, 30)
.Visible = False
.Name = "btnCancel"
End With
frm.Controls.Add(btn)
frm.CancelButton = btn
With pic
Select Case Me._ImageStyle
Case WaitImageStyle.FINDCOMP
.Image = My.Resources.FINDCOMP
Case WaitImageStyle.Wait01
.Image = My.Resources.Wait01
Case WaitImageStyle.Wait02
.Image = My.Resources.Wait02
Case WaitImageStyle.Wait03
.Image = My.Resources.Wait03
Case WaitImageStyle.Wait04
.Image = My.Resources.Wait04
Case Else
.Image = My.Resources.Wait04
End Select
.SizeMode = PictureBoxSizeMode.AutoSize
.Location = New Point(3, (frm.Height - 6 - .Height) * 0.5)
End With
frm.Controls.Add(pic)
AddHandler btn.Click, AddressOf CancelClick
frm.Show()
Application.DoEvents()
xTaskEventargs = New TaskEventArgs
RaiseEvent Task(sender, xTaskEventargs)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
Finally
If Not sender Is Nothing Then
Try
sender.Enabled = True
Catch ex As Exception
End Try
End If
frm.Dispose()
End Try
End Sub
Private Sub CancelClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
If MsgBox("真要终止当前任务?", MsgBoxStyle.YesNo, "提示") = MsgBoxResult.Yes Then
xTaskEventargs.Cancel = True
End If
End Sub
Private Sub xTaskEventargs_CancelButtonVisibleChange(ByVal sender As Object, ByVal e As System.EventArgs) Handles xTaskEventargs.CancelButtonVisibleChange
Me.frm.Refresh()
End Sub
Private Sub xTaskEventargs_CancelChange(ByVal sender As Object, ByVal e As System.EventArgs) Handles xTaskEventargs.CancelChange
End Sub
Private Sub xTaskEventargs_PercentChange(ByVal sender As Object, ByVal e As System.EventArgs) Handles xTaskEventargs.PercentChange
Me.frm.Refresh()
End Sub
Private Sub xTaskEventargs_TitleChange(ByVal sender As Object, ByVal e As System.EventArgs) Handles xTaskEventargs.TitleChange
Me.frm.Refresh()
End Sub
Private Sub frm_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles frm.Paint
Try
Dim xFont As New Drawing.Font("宋体", 9, Drawing.FontStyle.Regular)
Dim sText As String
Dim xBrush As Drawing.Brush = Drawing.Brushes.Black
Dim xLayoutRectangle As RectangleF
Dim xFormat As StringFormat
sText = Me.xTaskEventargs.Title
If Me.xTaskEventargs.Percent <> 0 Then
sText += " " & Me.xTaskEventargs.Percent & "%"
End If
xFont = New Font("", 9, FontStyle.Bold) '字体
xBrush = Drawing.Brushes.Black '填充色
xLayoutRectangle = New RectangleF '位置
With xLayoutRectangle
.Location = New PointF(60, 0)
.Size = New SizeF(300, 60)
End With
xFormat = New StringFormat() '对齐方式
With xFormat
.Alignment = StringAlignment.Near '水平对齐方式
.LineAlignment = StringAlignment.Center '垂直对齐方式
End With
e.Graphics.DrawString(sText, xFont, xBrush, xLayoutRectangle, xFormat) '开始输出
frm.Controls("btnCancel").Visible = Me.xTaskEventargs.CancelButtonVisible
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
End Class