⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 10-1.frm

📁 vb6.0编程实例详解,很详细的介绍,对学习VB有帮助
💻 FRM
字号:
VERSION 5.00
Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.3#0"; "COMCTL32.OCX"
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form Form1 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "五子棋(串口)"
   ClientHeight    =   3825
   ClientLeft      =   150
   ClientTop       =   840
   ClientWidth     =   3450
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   3825
   ScaleWidth      =   3450
   StartUpPosition =   3  '窗口缺省
   Begin VB.Timer Timer1 
      Interval        =   50
      Left            =   840
      Top             =   240
   End
   Begin MSCommLib.MSComm Comm1 
      Left            =   120
      Top             =   120
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
      DTREnable       =   -1  'True
   End
   Begin ComctlLib.StatusBar Status1 
      Align           =   2  'Align Bottom
      Height          =   375
      Left            =   0
      TabIndex        =   1
      Top             =   3450
      Width           =   3450
      _ExtentX        =   6085
      _ExtentY        =   661
      Style           =   1
      SimpleText      =   ""
      _Version        =   327682
      BeginProperty Panels {0713E89E-850A-101B-AFC0-4210102A8DA7} 
         NumPanels       =   1
         BeginProperty Panel1 {0713E89F-850A-101B-AFC0-4210102A8DA7} 
            Object.Tag             =   ""
         EndProperty
      EndProperty
   End
   Begin VB.PictureBox Pic1 
      AutoRedraw      =   -1  'True
      BackColor       =   &H80000005&
      FillStyle       =   0  'Solid
      Height          =   495
      Left            =   960
      ScaleHeight     =   29
      ScaleMode       =   3  'Pixel
      ScaleWidth      =   77
      TabIndex        =   0
      Top             =   1440
      Width           =   1215
   End
   Begin VB.Menu File 
      Caption         =   "文件"
      Begin VB.Menu Game 
         Caption         =   "新游戏"
      End
      Begin VB.Menu Separate 
         Caption         =   "-"
      End
      Begin VB.Menu Exit 
         Caption         =   "退出"
      End
   End
   Begin VB.Menu Link 
      Caption         =   "连接"
      Begin VB.Menu SLink 
         Caption         =   "建立连接"
      End
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim DataArray(10, 10) As Integer
Dim LinkType As Integer
Dim OtherType As Integer
Dim Drawing As Boolean
Dim Buffer As String
Dim ErrorCode As Integer

Const SubWidth As Integer = 20


Private Sub Exit_Click()
    Unload Me
End Sub

Private Sub Form_Load()
    Dim X As Integer
    Dim Y As Integer
    Dim Port As Integer
    
    '获得屏幕大小
    X = Screen.Width
    Y = Screen.Height
    '计算左上角坐标
    X = (X - Me.Width) / 2
    Y = (Y - Me.Height) / 2
    '移动窗体
    Me.Move X, Y
    '初始化数据
    LinkType = 1
    OtherType = 2
    Drawing = False
    '打开串口1
    Comm1.CommPort = 1
    Port = Val(InputBox("输入串行端口号", "输入", 1))
    If Port = 2 Then
    '当用户输入2时,指定串口2
    '该判断保证只能打开串口1或串口2
        Comm1.CommPort = 2
    End If
    Comm1.PortOpen = True
End Sub

Private Sub Form_Resize()
    '调整绘图区大小 , 使用Move方法比直接设置属性效率较高
    Pic1.Move ScaleLeft, ScaleTop, _
            ScaleWidth, ScaleHeight
    '绘制棋盘
    For i = 1 To 10
        Pic1.Line (SubWidth, SubWidth * i)-(SubWidth * 10, SubWidth * i)
        Pic1.Line (SubWidth * i, SubWidth)-(SubWidth * i, SubWidth * 10)
    Next i
End Sub

Private Sub Game_Click()
    Call NewGame '开始新游戏
    '发送开始新游戏命令
    Comm1.Output = "NEW" + Chr(26)
End Sub

Private Sub Pic1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim X0 As Integer
    Dim Y0 As Integer
    
    
    If Not Drawing Then
        Exit Sub
    End If
    
    '计算棋格位置
    X = X + SubWidth / 2
    Y = Y + SubWidth / 2
    X0 = X \ SubWidth
    Y0 = Y \ SubWidth
    
    If DataArray(X0, Y0) Then
    '当前位置已经有棋子了
        Exit Sub
    End If
    
    DataArray(X0, Y0) = LinkType '记录当前走棋
    
    '绘棋子
    If LinkType = 1 Then
        Pic1.FillColor = &HFFFFFF
    Else
        Pic1.FillColor = 0
    End If
    Pic1.Circle (X0 * SubWidth, Y0 * SubWidth), SubWidth / 3
    Drawing = False '停止我方走棋
    Status1.SimpleText = "对方走,请稍候 ..."
    '发送走棋位置
    Comm1.Output = "DATA" + Str(X0) + "," + Str(Y0) + "," + Chr(26) + Chr(13)
End Sub

Private Sub SLink_Click()
'申请建立连接
    Dim Wait As String
    
    Comm1.Output = "Linking" + Chr(26) '发送连接申请
    
    Wait = Waiting("CONNECTED", 5) '等待响应
    If ErrorCode Then
        Status1.SimpleText = "连接失败。"
    Else
    '连接成功
        LinkType = 2
        OtherType = 1
        Drawing = True
        Status1.SimpleText = "连接成功,你先走"
    End If
End Sub

Private Sub Timer1_Timer()
'定时查询串口状态
    If Comm1.InBufferCount Then
        Buffer = Buffer + Comm1.Input
    End If
    
    If InStr(1, Buffer, Chr(26)) Then
    '收到一个完整命令,Chr(26)在本程序中定义为命令行结束符
        RemoteControl
    End If
End Sub

Private Function Waiting(Strings As String, WaitTime As Integer) As String
    Dim EndTime As Long
    
    '计算结束时间
    EndTime = Timer + WaitTime
    
    '初始化数据
    Buffer = ""
    ErrorCode = 0
    
    Do
        DoEvents '处理其它事件
        If Comm1.InBufferCount Then
            '接收数据
            Buffer = Buffer + Comm1.Input
            
            If InStr(1, Buffer, Strings) Then
                '接收到等待的字符串
                Exit Do
            End If
        End If
        If Timer >= EndTime Or ErrorCode Then
            '等待超时
            ErrorCode = 1
            Exit Do
        End If
    Loop
    
    '返回接收的字符串
    Waiting = Buffer
End Function

'远程数据处理函数
Private Sub RemoteControl()
    Dim TempStart As Integer
    Dim TempEnd As Integer
    Dim X0 As Integer
    Dim Y0 As Integer
    
    '判断是否有连接申请
    'Buffer字符串由定时器处理生成
    TempStart = InStr(1, Buffer, "Linking")
    If TempStart Then
        Call NewGame
        
        Comm1.Output = "CONNECTED" + Chr(26)
        Buffer = ""
        Exit Sub
    End If
    
    '判断是否收到重新开始命令
    TempStart = InStr(1, Buffer, "NEW")
    If TempStart Then
        Call NewGame
        Buffer = ""
    End If
    
    '判断是否收到对方走棋数据
    TempStart = InStr(1, Buffer, "DATA")
    If TempStart Then
        '解码走棋位置,该位置信息是通过一定的编码( Pic1_MouseDown 程序),以字符串形式传送
        TempEnd = InStr(1, Buffer, ",")
        X0 = Val(Mid(Buffer, TempStart + 4, TempEnd - TempStart - 4))
        TempStart = TempEnd + 1
        TempEnd = InStr(TempStart, Buffer, ",")
        Y0 = Val(Mid(Buffer, TempStart, TempEnd - TempStart))
        DataArray(X0, Y0) = OtherType
        '设置棋子颜色
        If OtherType = 1 Then
            Pic1.FillColor = &HFFFFFF
        Else
            Pic1.FillColor = 0
        End If
        '绘制棋子
        Pic1.Circle (X0 * SubWidth, Y0 * SubWidth), SubWidth / 3
        Drawing = True
        Status1.SimpleText = "你走 !"
        
        Buffer = ""
        Exit Sub
    End If
    
    TempStart = InStr(1, Buffer, "LOSS")
    If TempStart Then
        Status1.SimpleText = "你输了 !"
        Buffer = ""
        Exit Sub
    End If
End Sub

Private Sub NewGame()
    Dim i As Integer
    Dim j As Integer
    
    '清空数据区
    For i = 0 To 10
      For j = 0 To 10
        DataArray(i, j) = 0
    Next j, i
    
    If LinkType = 2 Then
        Drawing = True
    End If
    
    Pic1.Cls '清屏
    Form_Resize
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -