Form5.vb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. 
  2. Option Explicit On
  3. Imports System.IO
  4. Imports System.Net.Sockets
  5. Public Class Form5
  6. Dim Listener As New TcpListener(8000)
  7. Dim Client As TcpClient
  8. Dim appPath As String = Application.StartupPath()
  9. Dim Newline As String
  10. Dim message As String
  11. Dim nStart As Integer
  12. Dim nLast As Integer
  13. Private IsFormBeingDragged As Boolean = False
  14. Private MouseDownX As Integer
  15. Private MouseDownY As Integer
  16. Dim z As String
  17. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  18. For counter = 10 To 90 Step +20
  19. Me.Opacity = counter / 100
  20. Me.Refresh()
  21. Threading.Thread.Sleep(50)
  22. Next
  23. Me.Opacity = 100
  24. End Sub
  25. Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseDown
  26. If e.Button = MouseButtons.Left Then
  27. IsFormBeingDragged = True
  28. MouseDownX = e.X
  29. MouseDownY = e.Y
  30. End If
  31. End Sub
  32. Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp
  33. If e.Button = MouseButtons.Left Then
  34. IsFormBeingDragged = False
  35. End If
  36. End Sub
  37. Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove
  38. If IsFormBeingDragged Then
  39. Dim temp As Point = New Point()
  40. temp.X = Me.Location.X + (e.X - MouseDownX)
  41. temp.Y = Me.Location.Y + (e.Y - MouseDownY)
  42. Me.Location = temp
  43. temp = Nothing
  44. End If
  45. End Sub
  46. Private Sub Form5_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
  47. Listener.Stop()
  48. End Sub
  49. Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  50. Timer1.Start()
  51. Listener.Start()
  52. End Sub
  53. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  54. If Listener.Pending = True Then
  55. message = ""
  56. Client = Listener.AcceptTcpClient()
  57. Dim reader As New StreamReader(Client.GetStream())
  58. While reader.Peek <> -1
  59. message &= Convert.ToChar(reader.Read()).ToString
  60. End While
  61. Dim Newline As String
  62. Newline = System.Environment.NewLine
  63. If message.Contains("</>") Then
  64. nStart = InStr(message, "</>") + 4
  65. nLast = InStr(message, "<\>")
  66. message = Newline & "對方:" & Mid(message, nStart, nLast - nStart)
  67. End If
  68. End If
  69. TextBox1.Text += message
  70. TextBox1.SelectionStart = TextBox1.TextLength
  71. TextBox1.ScrollToCaret()
  72. message = ""
  73. End Sub
  74. Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
  75. Dim iCount As Integer
  76. For iCount = 90 To 10 Step -10
  77. Me.Opacity = iCount / 100
  78. Threading.Thread.Sleep(50)
  79. Next
  80. Me.Close()
  81. End Sub
  82. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  83. Dim client As TcpClient
  84. If TextBox3.Text = "" Then
  85. MsgBox("未設定對方IP地址")
  86. End If
  87. Try
  88. client = New TcpClient(TextBox3.Text, 8000)
  89. Dim writer As New StreamWriter(client.GetStream())
  90. writer.Write("</>" & " " & TextBox2.Text & "<\>")
  91. writer.Flush()
  92. Catch ex As Exception
  93. MsgBox(ex.Message & "對方啟動了嗎?")
  94. End Try
  95. Newline = System.Environment.NewLine
  96. TextBox1.Text += Newline & "本地:" & TextBox2.Text
  97. TextBox1.SelectionStart = TextBox1.TextLength
  98. TextBox1.ScrollToCaret()
  99. TextBox2.Text = ""
  100. End Sub
  101. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  102. End Sub
  103. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  104. End Sub
  105. Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
  106. End Sub
  107. Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
  108. End Sub
  109. Private Sub Button2_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  110. Dim sb As New System.Text.StringBuilder
  111. Dim sfile As System.IO.FileStream
  112. If My.Computer.FileSystem.FileExists(appPath & "\聊天室對話" & ".txt") = False Then
  113. sfile = System.IO.File.Create(appPath & "\聊天室對話" & ".txt")
  114. sfile.Dispose()
  115. End If
  116. sb.AppendLine(TextBox1.Text)
  117. IO.File.WriteAllText((appPath & "\聊天室對話" & ".txt"), sb.ToString(), System.Text.Encoding.GetEncoding(950))
  118. MsgBox("對話已儲存")
  119. End Sub
  120. End Class