在 Visual Basic .NET 中实现后台进程__教程 |
|
日期:2007-5-20 1:14:28 人气:359 [大 中 小] |
|
|
|
For index = 0 To 6 mBoxes.Add(CreateBox(index)) Next End If mCount = 0
End Sub
Private Function CreateBox(ByVal index As Integer) As PictureBox Dim box As New PictureBox()
With box SetPosition(box, index) .BorderStyle = BorderStyle.Fixed3D .Parent = Me .Visible = True End With Return box End Function
Private Sub GrayDisplay() Dim index As Integer
For index = 0 To 6 CType(mBoxes(index), PictureBox).BackColor = Me.BackColor Next End Sub
Private Sub SetPosition(ByVal Box As PictureBox, ByVal Index As Integer) Dim left As Integer = CInt(Me.Width / 2 - 7 * 14 / 2) Dim top As Integer = CInt(Me.Height / 2 - 5)
With Box .Height = 10 .Width = 10 .Top = top .Left = left + Index * 14 End With End Sub
Private Sub tmAnim_Tick(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles tmAnim.Tick
CType(mBoxes((mCount + 1) Mod 7), PictureBox).BackColor = _ Color.LightGreen CType(mBoxes(mCount Mod 7), PictureBox).BackColor = Me.BackColor
mCount += 1 If mCount > 6 Then mCount = 0 End Sub
Public Sub Start() CType(mBoxes(0), PictureBox).BackColor = Color.LightGreen tmAnim.Enabled = True End Sub
Public Sub [Stop]() tmAnim.Enabled = False GrayDisplay() End Sub
Private Sub ActivityBar_Resize(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.Resize
Dim index As Integer
For index = 0 To mBoxes.Count - 1 SetPosition(CType(mBoxes(index), PictureBox), index) Next End Sub
窗体的 Load 事件创建 PictureBox 控件并将它们放入数组,这样便于我们在它们之间循环。Timer 控件的 Tick 事件循环显示,使各个控件依次呈绿色。
所有操作由 Start 方法开始,由 Stop 事件结束。由于 Stop 是一个保留字,因此把这个方法名放在方括号内:[Stop]。Stop 方法不仅可以停止计时器,还可以灰显所有框,告诉用户这些框中当前没有活动。
创建 Worker 类 本文前面已简单介绍了 Worker 类。因为我们已经定义了 IWorker 接口,所以可以增强该类,以利用我们创建的 Controller。
首先创建 Background.dll 文件。此步骤很重要,因为如果不完成此步骤,ActivityBar 控件将无法在我们建立测试窗体时显示在工具箱上。
在解决方案中添加名为 bgTest 的 Windows Forms Application(Windows 窗体应用程序)。在 Solution Explorer(解决方案资源浏览器)中用右键单击该项目并选择相应的菜单项,将该程序设置为启动项目。
然后使用 Add References(添加引用)对话框中的 Projects(项目)选项卡,添加对 Background 项目的引用。
现在,在名为 Worker 的项目中添加一个类。其中部分代码与前面所述的代码相同,但还包含一些不同的代码,用以实现 IWorker 接口(此处突出显示的部分):
Imports Background
Public Class Worker Implements IWorker
Private mController As IController Private mInner As Integer Private mOuter As Integer
Public Sub New(ByVal InnerSize As Integer, ByVal OuterSize As Integer) mInner = InnerSize mOuter = OuterSize End Sub
' 由 Controller 调用,以便获取 ' Controller 的引用 Private Sub Init(ByVal Controller As IController) _ Implements IWorker.Initialize
mController = Controller
End Sub
Private Sub Work() Implements IWorker.Start Dim innerIndex As Integer Dim outerIndex As Integer
|
|
出处:本站原创 作者:佚名 |
|
|