?? 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 + -