📄 chat_frmmain.frm
字号:
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.3#0"; "COMCTL32.OCX"
Begin VB.Form frmMain
BorderStyle = 1 'Fixed Single
Caption = "局域网聊天程序"
ClientHeight = 6750
ClientLeft = 45
ClientTop = 330
ClientWidth = 5205
Icon = "Chat_frmMain.frx":0000
KeyPreview = -1 'True
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 6750
ScaleWidth = 5205
StartUpPosition = 2 'CenterScreen
Begin ComctlLib.StatusBar StatusBar1
Align = 2 'Align Bottom
Height = 285
Left = 0
TabIndex = 12
Top = 6465
Width = 5205
_ExtentX = 9181
_ExtentY = 503
SimpleText = ""
_Version = 327682
BeginProperty Panels {0713E89E-850A-101B-AFC0-4210102A8DA7}
NumPanels = 2
BeginProperty Panel1 {0713E89F-850A-101B-AFC0-4210102A8DA7}
AutoSize = 2
Text = "没有连接"
TextSave = "没有连接"
Key = "STATUS"
Object.Tag = ""
Object.ToolTipText = "The current status of the connection"
EndProperty
BeginProperty Panel2 {0713E89F-850A-101B-AFC0-4210102A8DA7}
AutoSize = 1
Object.Width = 6562
Key = "DATA"
Object.Tag = ""
Object.ToolTipText = "The last data transfer through the modem"
EndProperty
EndProperty
BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851}
Name = "宋体"
Size = 9.75
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
End
Begin VB.CommandButton cmdClear
Caption = "清除对话框"
BeginProperty Font
Name = "宋体"
Size = 9.75
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 285
Left = 90
TabIndex = 11
Top = 6090
Width = 1545
End
Begin VB.TextBox txtRemotePort
BackColor = &H00C0C0C0&
Height = 285
Left = 2010
TabIndex = 3
Top = 720
Width = 1665
End
Begin VB.TextBox txtLocalPort
BackColor = &H00C0C0C0&
Height = 285
Left = 2010
TabIndex = 2
Top = 420
Width = 1665
End
Begin VB.CommandButton cmdConnect
Caption = "连接"
BeginProperty Font
Name = "宋体"
Size = 14.25
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 885
Left = 3720
TabIndex = 8
Top = 120
Width = 1365
End
Begin VB.TextBox txtRemoteIP
BackColor = &H00C0C0C0&
Height = 285
Left = 2010
TabIndex = 1
Top = 120
Width = 1665
End
Begin VB.Frame Frame2
Caption = "本地IP"
Enabled = 0 'False
BeginProperty Font
Name = "宋体"
Size = 9.75
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 2415
Left = 90
TabIndex = 6
Top = 3600
Width = 5025
Begin VB.TextBox Text2
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00400000&
Height = 2115
Left = 120
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 5
Top = 210
Width = 4785
End
End
Begin VB.Frame Frame1
Caption = "远程IP"
Enabled = 0 'False
BeginProperty Font
Name = "宋体"
Size = 9.75
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 2325
Left = 90
TabIndex = 0
Top = 1260
Width = 5025
Begin VB.TextBox Text1
BackColor = &H00E0E0E0&
BeginProperty Font
Name = "宋体"
Size = 9.75
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H000000FF&
Height = 2025
Left = 120
Locked = -1 'True
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 4
Top = 210
Width = 4785
End
End
Begin MSWinsockLib.Winsock Winsock1
Left = 30
Top = 0
_ExtentX = 741
_ExtentY = 741
_Version = 393216
Protocol = 1
End
Begin VB.Label Label4
Alignment = 2 'Center
AutoSize = -1 'True
Caption = "输入要发送的文字:"
BeginProperty Font
Name = "宋体"
Size = 9.75
Charset = 134
Weight = 400
Underline = 0 'False
Italic = -1 'True
Strikethrough = 0 'False
EndProperty
ForeColor = &H00008000&
Height = 195
Left = 1710
TabIndex = 13
Top = 1050
Visible = 0 'False
Width = 1785
End
Begin VB.Label Label3
BorderStyle = 1 'Fixed Single
Caption = " Remote Port :"
BeginProperty Font
Name = "Arial"
Size = 9.75
Charset = 161
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FF0000&
Height = 285
Left = 90
TabIndex = 10
Top = 720
Width = 1905
End
Begin VB.Label Label2
AutoSize = -1 'True
BorderStyle = 1 'Fixed Single
Caption = " Local Port :"
BeginProperty Font
Name = "Arial"
Size = 9.75
Charset = 161
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FF0000&
Height = 285
Left = 90
TabIndex = 9
Top = 420
Width = 1905
End
Begin VB.Label Label1
BorderStyle = 1 'Fixed Single
Caption = "被连接的IP:"
BeginProperty Font
Name = "宋体"
Size = 9.75
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FF0000&
Height = 285
Left = 90
TabIndex = 7
Top = 120
Width = 1905
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'---------------------------------------------
' 局域网聊天程序
'---------------------------------------------
' 洪恩在线 求知无限
'---------------------------------------------
Option Explicit
Private IgnoreText As Boolean
'----------------------各控件说明----------------------------
'--名称-------------类型---------------作用------------------
'frmMain Form CHAT主窗体
'Winsock1 Winsock 连接控件
'Label1 Label CONNECT WITH IP标签
'Label2 Label LOCAL PORT标签
'Label3 Label REMOTE PORT标签
'txtRemoteIP TextBox 远程IP地址输入框
'txtLocalPort TextBox 本地PORT输入框
'txtRemotePort TextBox 远程PORT输入框
'cmdConnect CommandButton 连接CONNECT按钮
'Label4 Label Type your text and hit Enter to send it.标签
'Frame1(remoteip) Frame REMOTE IP 框架
'Frame2(host ip) Frame HOST IP 框架
'Text1 TextBox 显示对方(远程主机)发送的CHAT内容
'Text2 TextBox 输入己方(本地主机)要发送的CHAT内容,按ENTER键发送
'cmdClear CommandButton 清空输入框(TEXT2)和显示框(TEXT1)中的内容
'StatusBar1 StatusBar 状态栏
'当CLEAR按钮按下时,清空TEXT1和TEXT2中的内容
Private Sub cmdClear_Click()
On Error Resume Next
Text1 = ""
With Text2
'清空输入框
.Text = " "
'并把焦点置于TEXT2
.SetFocus
End With
End Sub
'当CONNECT按钮按下时,进行以下操作
Private Sub cmdConnect_Click()
On Error GoTo ErrHandler
With Winsock1
'设置 RemoteHost 属性
.RemoteHost = Trim(txtRemoteIP)
'设置 RemotePort 属性
'RemotePort 属性的值应该等于 远程主机上的 LocalHost 属性的值
.RemotePort = Trim(txtRemotePort)
'LocalPort 属性的值是不能改变的,必须检查它是否已经被设置
'如果 LocalPort 属性为空(没有被设置),将其设为在LocalPort输入框中输入的数值
If .LocalPort = Empty Then
.LocalPort = Trim(txtLocalPort)
Frame2.Caption = .LocalIP
'这是必须的,Bind 方法的作用是为控件“保留”一个本地端口,防止被其他用户调用。
.Bind .LocalPort
End If
End With
'为了保证使用者不能改变LocalPort的值,将txtLocalPort输入框锁定
txtLocalPort.Locked = True
'在状态栏中显示“正在连接”的状态
StatusBar1.Panels(1).Text = " 正在连接到 " & Winsock1.RemoteHost & " "
'如果连接正常,做以下设置
Frame1.Enabled = True
Frame2.Enabled = True
Label4.Visible = True
Text2.SetFocus
StatusBar1.Panels(1).Text = " 连接成功 "
Exit Sub
'如果在连接过程中出现错误,则转向ErrHandler:,并显示错误提示
ErrHandler:
MsgBox "建立连接失败,按 F1 以获得帮助信息", vbCritical
End Sub
'当按下“F1”键时显示帮助信息
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyF1 Then
ChDir App.Path
'调用外部程序notepad.exe来打开帮助文本文件
Shell "notepad.exe readme.txt", vbNormalFocus
End If
End Sub
'当窗体加载时显示提示信息并在 txtRemoteIP 框中显示本地主机的IP
Private Sub Form_Load()
Show
txtRemoteIP = Winsock1.LocalIP
End Sub
'接收TEXT2输入框的按键,并做响应
Private Sub Text2_KeyPress(KeyAscii As Integer)
'定义变量 Last_Line_Feed 来记录最后输入行的位置
Static Last_Line_Feed As Long
'定义 New_Line 字符串记录新键入的一行文本的内容
Dim New_Line As String
'如果使用者按下CLEAR按钮对输入框内容清空,这时TEXT2为空,则重设最后输入行的位置为0
If Trim(Text2) = vbNullString Then Last_Line_Feed = 0
'当使用者按下ENTER键时
If KeyAscii = 13 Then
'取得最后输入行的内容并赋值给 New_Line 字符串
New_Line = Mid(Text2, Last_Line_Feed + 1)
'重设最后输入行的位置
Last_Line_Feed = Text2.SelStart
'通过 WINSOCK 发送新输入的一行文本的内容
Winsock1.SendData New_Line
'在状态栏显示发送信息
StatusBar1.Panels(2).Text = " 发送 " & (LenB(New_Line) / 2) & " byte的消息 "
End If
End Sub
'当 WINSOCK 接收到新的数据(信息)时,进行以下响应
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
'定义 New_Text 字符串来记录新接收的信息
Dim New_Text As String
'接收信息并赋值给 New_Text
Winsock1.GetData New_Text
'在TEXT1显示框中显示新接收到的信息
Text1.SelText = New_Text
Frame1.Caption = Winsock1.RemoteHostIP
'在状态栏中显示接收信息
StatusBar1.Panels(2).Text = " 接收到 " & bytesTotal & " byte的消息 "
End Sub
'---------------------------------------------------------------------------
'这就是一个最简单的CHAT程序,你可以在它的基础上加以改进,做出更实用的CHAT小软件。
'---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -