Attribute VB_Name = "Term_body"
' TCP/IP main module (c) Thunderbolt
Option Explicit
Public Opts(255) As Integer
Public Const ProgId As String = "TTcpIpTerm"
Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal _
hWndInsertAfter As Long, ByVal X As Long, ByVal y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long
Public Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Public Declare Function RegOcx Lib "Mswinsck.OCX" _
            Alias "DllRegisterServer" () As Long
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
        
Private Const SWP_NOMOVE = 2
Private Const SWP_NOSIZE = 1
Private Const flags = SWP_NOMOVE Or SWP_NOSIZE
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Public Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type
Public Type POINTAPI
        X As Long
        y As Long
End Type
Public ButBack As Long
Public BegTime As String
Public SendB As Long, TotalSendB As Long
Public RecB As Long, TotalRecB As Long
Public Addr As String
Public Port1 As String
Public Port2 As String
Public Fon As Long

Sub Main()
'---- Intro
On Error Resume Next
If Dir(SystemDir + "mswinsck.ocx") = "" Then
    FileCopy App.Path + "\mswinsck.ocx", SystemDir + "mswinsck.ocx"
    RegOcx
End If
Dim i As Long
frmTerm.tm1.Interval = 500
frmTerm.tm1.Enabled = False
Fon = frmTerm.Shape1(0).BackColor
Intro.Show
SetTop Intro.hWnd, True
frmTerm.Show
DoEvents

'----- Start prog
AddStat "Terminal ready"
SendB = 0: RecB = 0: BegTime = ""
Addr = "127.0.0.1": Port1 = "0": Port2 = "0"
ButBack = frmTerm.But(0).BackColor
LoadOpt
End Sub
Public Sub SetTop(hWnd As Long, Topmost As Boolean)
Dim i As Long
     If Topmost = True Then
        i = SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, flags)
     Else
        i = SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, flags)
     End If
End Sub
Public Sub AddStat(Mess As String)
Dim a As String, i As Long
a = Time
frmTerm.List1.AddItem a & " : " & Mess
i = frmTerm.List1.ListCount
frmTerm.List1.ListIndex = i - 1
End Sub
Sub ShowStat()
Dim k As String
Stat.lnStat(0).Caption = BegTime
If frmTerm.ws1.State = 0 Or frmTerm.ws1.State = 8 Then Stat.lnStat(0).Caption = Stat.lnStat(0).Caption & " (Closed)"
If BegTime = "" Then k = "Not available" Else k = Addr
Stat.lnStat(1).Caption = k
Stat.lnStat(2).Caption = SendB
Stat.lnStat(3).Caption = RecB
Stat.lnStat(4).Caption = TotalSendB
Stat.lnStat(5).Caption = TotalRecB
End Sub
Sub LoadOpt()
Dim i As Integer, i1 As Integer
i = Sets.Opt.Count
For i1 = 0 To i - 1
Opts(i1) = Val(GetSetting(ProgId, "Options", "Option_" + Trim(Str(i1)), "0"))
Next i1
Unload Sets
End Sub
'------Get Windows Directory---------
Public Function WindowsDir() As String
Dim a As String, i As Long
a = Space(1000)
i = GetWindowsDirectory(a, Len(a))
a = Trim(a)
If Len(a) > 0 Then a = Left(a, Len(a) - 1)
WindowsDir = Slash(a)
End Function

'------Get System Directory---------
Public Function SystemDir() As String
Dim a As String, i As Long
a = Space(1000)
i = GetSystemDirectory(a, Len(a))
a = Trim(a)
If Len(a) > 0 Then a = Left(a, Len(a) - 1)
SystemDir = Slash(a)
End Function

Function Slash(w As String) As String
w = Trim(w)
If Len(w) = 0 Then Exit Function
If Right(w, 1) <> "\" Then w = w + "\"
Slash = w
End Function
