扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:陶刚编译 来源:天极网 2007年10月14日
关键字:
Option Explicit Private stack As Collection Private Const MOVEMENT_AMOUNT As Integer = 50 Private Sub AboutMenu_Click() Const About As String = "Command Pattern Demo" + vbCrLf + _ "Copyright (c) 2004. All Rights Reserved" + vbCrLf + _ "Written By Paul Kimmel. pkimmel@softconcepts.com" MsgBox About, vbInformation + vbOKOnly, "About" End Sub Private Sub ExitMenu_Click() End End Sub Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) Select Case KeyCode Case vbKeyBack UndoCommand Case vbKeyLeft Call ProcessCommand(New LeftCommand) Case vbKeyUp Call ProcessCommand(New UpCommand) Case vbKeyRight Call ProcessCommand(New RightCommand) Case vbKeyDown Call ProcessCommand(New DownCommand) End Select End Sub Private Function PopStack() As ICommand On Error GoTo Catch Debug.Print "Stack count: " & stack.Count If (stack.Count = 0) Then Beep Set PopStack = Nothing Exit Function End If Set PopStack = stack.Item(stack.Count) Call stack.Remove(stack.Count) Exit Function Catch: Debug.Print Err.Description Set PopStack = Nothing End Function Private Sub PushStack(ByVal command As ICommand) On Error GoTo Catch Call stack.Add(command) Debug.Print "Stack count: " & stack.Count Exit Sub Catch: Debug.Print Err.Description End Sub Public Sub UndoCommand() Dim command As ICommand Set command = PopStack() If (command Is Nothing) Then Exit Sub command.Undo End Sub Private Sub ProcessCommand(ByVal command As ICommand) Set command.Form = Me command.Execute Call PushStack(command) End Sub Public Sub MoveDown() Debug.Print "Moving Down" If (Image1.Top > Height) Then Image1.Top = 0 - Image1.Height + MOVEMENT_AMOUNT Else Image1.Top = Image1.Top + MOVEMENT_AMOUNT End If End Sub Public Sub MoveUp() Debug.Print "Moving Up" If (Image1.Top + Image1.Height < 0) Then Image1.Top = Height - MOVEMENT_AMOUNT Else Image1.Top = Image1.Top - MOVEMENT_AMOUNT End If End Sub Public Sub MoveLeft() Debug.Print "Moving Left" If (Image1.Left + Image1.Width < 0) Then Image1.Left = Width - MOVEMENT_AMOUNT Else Image1.Left = Image1.Left - MOVEMENT_AMOUNT End If End Sub Public Sub MoveRight() Debug.Print "Moving Right" If (Image1.Left > Width) Then Image1.Left = 0 - Image1.Width + MOVEMENT_AMOUNT Else Image1.Left = Image1.Left + MOVEMENT_AMOUNT End If End Sub Private Sub Form_Load() Set stack = New Collection End Sub Private Sub RewindMenu_Click() While stack.Count > 0 UndoCommand Wend End Sub Private Sub UndoMenu_Click() Call UndoCommand End Sub |
图1:命令中的类:描述我们的命令模式中关系的UML类图表 |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者