home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / library / library.bas < prev    next >
Encoding:
BASIC Source File  |  1995-11-09  |  24.2 KB  |  712 lines

  1. Option Explicit
  2.  
  3. Type PointAPI
  4.     x As Integer
  5.     y As Integer
  6. End Type
  7.  
  8. Global Const WM_USER = &H400
  9.  
  10. Global Const EM_SETREADONLY = (WM_USER + 31)
  11. Global Const HTCAPTION = 2
  12. Global Const LB_SELECTSTRING = (WM_USER + 13)
  13. Global Const WM_NCLBUTTONDOWN = &HA1
  14.  
  15. Declare Function ExitWindows Lib "User" (ByVal dwReturnCode As Long, ByVal wReserved As Integer) As Integer
  16. Declare Function ExitWindowsExec Lib "User" (ByVal DosExe As String, ByVal Params As String) As Integer
  17.      Declare Sub GetCursorPos Lib "User" (lpPoint As PointAPI)
  18. Declare Function GetDriveType Lib "kernel" (ByVal nDrive As Integer) As Integer
  19. Declare Function GetFreeSystemResources Lib "User" (ByVal fuSysResource As Integer) As Integer
  20. Declare Function GetSystemDirectory Lib "Kernel" (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer
  21. Declare Function GetWindowsDirectory Lib "Kernel" (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer
  22.      Declare Sub HideCaret Lib "User" (ByVal hWnd As Integer)
  23.      Declare Sub ReleaseCapture Lib "User" ()
  24. Declare Function SendMessage Lib "User" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, lParam As Any) As Long
  25. Declare Function SetWindowPos Lib "user" (ByVal h%, ByVal hb%, ByVal x%, ByVal y%, ByVal cx%, ByVal cy%, ByVal F%) As Integer
  26.      Declare Sub SetWindowWord Lib "User" (ByVal hWnd%, ByVal nCmd%, ByVal nVal%)
  27. Declare Function ShowCursor Lib "User" (ByVal bShow%)
  28. Declare Function ShowWindow% Lib "User" (ByVal hWnd%, ByVal nCmdShow%)
  29. Declare Function WinHelp% Lib "User" (ByVal hWnd%, ByVal HelpFile$, ByVal HelpCode%, ByVal HelpData&)
  30. Declare Function GetVersion& Lib "Kernel" ()
  31.  
  32. Sub AlwaysOnTop (frmID As Form, OnTop As Integer)
  33. ' Pass any non-zero value to Place on top
  34. ' Pass zero to remove top-mostness
  35.     Const SWP_NOMOVE = 2
  36.     Const SWP_NOSIZE = 1
  37.     Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
  38.     Const HWND_TOPMOST = -1
  39.     Const HWND_NOTOPMOST = -2
  40.     If OnTop Then
  41.     OnTop = SetWindowPos(frmID.hWnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
  42.     Else
  43.     OnTop = SetWindowPos(frmID.hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, FLAGS)
  44.     End If
  45. End Sub
  46.  
  47. Function AnotherInstance () As Integer
  48. '   This routine determines if the currently running program is already
  49. '   running (running twice).  It shifts the focus to the previous app
  50. '   and returns true.  It does NOT close the duplicated instance.
  51.  
  52.     Dim AppTitle$
  53.     If App.PrevInstance Then
  54.     AppTitle$ = App.Title
  55.     App.Title = "No longer want this app running..."
  56.     AppActivate AppTitle$   ' Activate the previous instance
  57.     AnotherInstance = True
  58.     Else
  59.     AnotherInstance = False
  60.     End If
  61. End Function
  62.  
  63. Sub CenterForm (frm As Form)
  64. 'Center A Form On The Screen
  65. 'Centers form on the screen (before showing it works best)
  66.     Dim x As Integer
  67.     Dim y As Integer
  68.     x = (Screen.Width - frm.Width) / 2
  69.     y = (Screen.Height - frm.Height) / 2
  70.     frm.Move x, y       'Change the location of the form
  71. End Sub
  72.  
  73. Function CreatePath (ByVal DestPath$) As Integer
  74. '   Note - This function returns false if not successful
  75.  
  76. ' Create the path contained in DestPath$
  77. ' First char must be drive letter, followed by
  78. ' a ":\" followed by the path, if any.
  79.  
  80.     Dim BackPos As Integer
  81.     Dim forePos As Integer
  82.     Dim Temp$
  83.     
  84.     Screen.MousePointer = 11
  85.     
  86.     '---------------------------------------------
  87.     ' Add slash to end of path if not there already
  88.     '---------------------------------------------
  89.     If Right$(DestPath$, 1) <> "\" Then DestPath$ = DestPath$ + "\"
  90.     
  91.     '-----------------------------------
  92.     ' Change to the root dir of the drive
  93.     '-----------------------------------
  94.     On Error Resume Next
  95.     ChDrive DestPath$
  96.     If Err <> 0 Then GoTo errorOut
  97.     ChDir "\"
  98.  
  99.     '-------------------------------------------------
  100.     ' Attempt to make each directory, then change to it
  101.     '-------------------------------------------------
  102.     BackPos = 3
  103.     forePos = InStr(4, DestPath$, "\")
  104.     Do While forePos <> 0
  105.     Temp$ = Mid$(DestPath$, BackPos + 1, forePos - BackPos - 1)
  106.  
  107.     Err = 0
  108.     MkDir Temp$
  109.     If Err <> 0 And Err <> 75 Then GoTo errorOut
  110.  
  111.     Err = 0
  112.     ChDir Temp$
  113.     If Err <> 0 Then GoTo errorOut
  114.  
  115.     BackPos = forePos
  116.     forePos = InStr(BackPos + 1, DestPath$, "\")
  117.     Loop
  118.          
  119.     CreatePath = True
  120.     Screen.MousePointer = 0
  121.     Exit Function
  122.          
  123. errorOut:
  124.     MsgBox "Error While Attempting to Create Directories on Destination Drive.", 48, "SETUP"
  125.     CreatePath = False
  126.     Screen.MousePointer = 0
  127.  
  128. End Function
  129.  
  130. Sub CreateProgManGroup (x As Form, GroupName$, GroupPath$)
  131. '-------------------------------------------------------------
  132. ' Procedure: CreateProgManGroup
  133. ' Arguments: X           The Form where a Label1 exist
  134. '            GroupName$  A string that contains the group name
  135. '            GroupPath$  A string that contains the group file
  136. '                        name  ie 'myapp.grp'
  137. '-------------------------------------------------------------
  138.     Dim i%, z%
  139.     
  140.     Screen.MousePointer = 11
  141.     
  142.     '----------------------------------------------------------------------
  143.     ' Windows requires DDE in order to create a program group and item.
  144.     ' Here, a Visual Basic label control is used to generate the DDE messages
  145.     '----------------------------------------------------------------------
  146.     On Error Resume Next
  147.     
  148.     '--------------------------------
  149.     ' Set LinkTopic to PROGRAM MANAGER
  150.     '--------------------------------
  151.     x.Label1.LinkTopic = "ProgMan|Progman"
  152.     x.Label1.LinkMode = 2
  153.     For i% = 1 To 10                                         ' Loop to ensure that there is enough time to
  154.       z% = DoEvents()                                        ' process DDE Execute.  This is redundant but needed
  155.     Next                                                     ' for debug windows.
  156.     x.Label1.LinkTimeout = 100
  157.     
  158.     '---------------------
  159.     ' Create program group
  160.     '---------------------
  161.     x.Label1.LinkExecute "[CreateGroup(" + GroupName$ + Chr$(44) + GroupPath$ + ")]"
  162.     
  163.     '-----------------
  164.     ' Reset properties
  165.     '-----------------
  166.     x.Label1.LinkTimeout = 50
  167.     x.Label1.LinkMode = 0
  168.     Screen.MousePointer = 0
  169. End Sub
  170.  
  171. Sub CreateProgManItem (x As Form, CmdLine$, IconTitle$)
  172. '----------------------------------------------------------
  173. ' Procedure: CreateProgManItem
  174. '
  175. ' Arguments: X           The form where Label1 exists
  176. '
  177. '            CmdLine$    A string that contains the command
  178. '                        line for the item/icon.
  179. '                        ie 'c:\myapp\setup.exe'
  180. '
  181. '            IconTitle$  A string that contains the item's
  182. '                        caption
  183. '----------------------------------------------------------
  184.     
  185.     Dim i%, z%
  186.     Screen.MousePointer = 11
  187.     
  188.     '----------------------------------------------------------------------
  189.     ' Windows requires DDE in order to create a program group and item.
  190.     ' Here, a Visual Basic label control is used to generate the DDE messages
  191.     '----------------------------------------------------------------------
  192.     On Error Resume Next
  193.  
  194.     '---------------------------------
  195.     ' Set LinkTopic to PROGRAM MANAGER
  196.     '---------------------------------
  197.     x.Label1.LinkTopic = "ProgMan|Progman"
  198.     x.Label1.LinkMode = 2
  199.     For i% = 1 To 10                                         ' Loop to ensure that there is enough time to
  200.       z% = DoEvents()                                        ' process DDE Execute.  This is redundant but needed
  201.     Next                                                     ' for debug windows.
  202.     x.Label1.LinkTimeout = 100
  203.  
  204.     '------------------------------------------------
  205.     ' Create Program Item, one of the icons to launch
  206.     ' an application from Program Manager
  207.     '------------------------------------------------
  208.     x.Label1.LinkExecute "[AddItem(" + CmdLine$ + Chr$(44) + IconTitle$ + Chr$(44) + ",,)]"
  209.     
  210.     '-----------------
  211.     ' Reset properties
  212.     '-----------------
  213.     x.Label1.LinkTimeout = 50
  214.     x.Label1.LinkMode = 0
  215.     
  216.     Screen.MousePointer = 0
  217. End Sub
  218.  
  219. Function DecimalValue (BinaryByte As String) As Integer
  220. '   This routine is called from the GetWindowsVersion routine
  221.     
  222.     Dim x As Integer
  223.     Dim v As Integer
  224.     Dim TempVal As Integer
  225.     
  226.     v = 128
  227.     For x = 1 To 8
  228.     If Mid$(BinaryByte, x, 1) = "1" Then TempVal = TempVal + v
  229.     v = v / 2
  230.     Next x
  231.     DecimalValue = TempVal
  232. End Function
  233.  
  234. Sub FloatingWindow (FrmChildhWnd%, FrmParenthWnd%)
  235.     Call SetWindowWord(FrmChildhWnd%, -8, FrmParenthWnd%)
  236. End Sub
  237.  
  238. Function FreeDrive () As String
  239. '   This function returns the drive letter of the next available drive
  240.     Dim DriveNum As Integer
  241.     Dim FirstDrive As Integer
  242.     DriveNum = -1
  243.     Do
  244.     DriveNum = DriveNum + 1   ' start at drive zero.
  245.     FirstDrive% = GetDriveType(DriveNum)  ' Requires API declaration
  246.     ' GetDriveType returns zero if it cannot determine drive
  247.     ' type or returns 1 if the specified drive does not exist.
  248.     Loop Until FirstDrive% = 0
  249.     ' DriveNum of 0 means Drive A, 1=B, 2=C, 3=D, 4=E, 5=F, and so on:
  250.     FreeDrive = Chr$(DriveNum + 65) + ":"
  251. End Function
  252.  
  253. Function GetSystemDir ()
  254.     Dim Sys As String * 256
  255.     Dim x As Integer
  256.     x = GetSystemDirectory(Sys, Len(Sys))
  257.     x = InStr(1, Sys, Chr$(0))
  258.     GetSystemDir = Left$(Sys, InStr(Sys, Chr$(0)) - 1)
  259. End Function
  260.  
  261. Function GetWindowsDir () As String
  262. '   Calls the windows API to get the windows directory
  263.     Dim Temp$, x As Integer
  264.     
  265.     Temp$ = String$(145, 0)              ' Size Buffer
  266.     x = GetWindowsDirectory(Temp$, 145)  ' Make API Call
  267.     Temp$ = Left$(Temp$, x)              ' Trim Buffer
  268.  
  269.     If Right$(Temp$, 1) <> "\" Then      ' Add \ if necessary
  270.     GetWindowsDir$ = Temp$ + "\"
  271.     Else
  272.     GetWindowsDir$ = Temp$
  273.     End If
  274.  
  275. End Function
  276.  
  277. Function GetWindowsVersion () As String
  278. '   This routine determines what version of Windows is being used
  279. '   This routine also requires the following two functions:
  280. '       MakeNybble()
  281. '       DecimalValue()
  282.  
  283.     Dim x As Long
  284.     Dim y As String
  285.     Dim z As Integer
  286.     Dim t As String
  287.     Dim HN As String
  288.     Dim LN As String
  289.  
  290.     x = GetVersion()
  291.     x = x - 117440512     ' Don't need High Word returned for Windows version
  292.     t = Hex$(x)           ' Make the numeric value a Hexadecimal string
  293.     y = Str$(Val(Right$(t, 2))) + "."' Hold the Major release # (ie  3.xx)
  294.     HN = MakeNybble(Left$(t, 1))
  295.     LN = MakeNybble(Mid$(t, 2, 1))
  296.     z = DecimalValue(HN + LN)
  297.     If z = 95 Then
  298.     GetWindowsVersion = "Windows 95"
  299.     Else
  300.     GetWindowsVersion = "Windows " + y + Trim$(Str$(z))
  301.     End If
  302. End Function
  303.  
  304. Sub Help_Click (HelpFile As String, frm As Form)
  305.   '<HelpFile> is the name of the application's helpfile
  306.   '<frm> is simply the form calling this function (pass ME)
  307.  
  308.   MsgBox "This routine would activate Help if a help file existed", 48, "VB Library"
  309.   Exit Sub
  310.   Dim i As Integer
  311.   i = WinHelp(frm.hWnd, App.HelpFile, 3, 0&)        'for Contents
  312.   i = WinHelp(frm.hWnd, App.HelpFile, &H105, 0&)    'for Search
  313.   i = WinHelp(frm.hWnd, App.HelpFile, 4, 0&)        'for HelponHelp
  314. End Sub
  315.  
  316. Sub HideMDIChild (frmID As Form)
  317.     Dim HiddenMDIChild As Integer
  318.     HiddenMDIChild = ShowWindow(frmID.hWnd, 0)
  319. End Sub
  320.  
  321. Sub HighlightText ()
  322. '   This routine will automatically highlight all text in a given textbox
  323. '   whenever called  (ie at GotFocus or when validating entries, etc)
  324.     Screen.ActiveForm.ActiveControl.SelStart = 0
  325.     Screen.ActiveForm.ActiveControl.SelLength = Len(Screen.ActiveForm.ActiveControl.Text)
  326. End Sub
  327.  
  328. Function IfFileExists (FileName As String) As Integer
  329. ' This function attempts to determine whether or not a
  330. ' given file exists based on the file's length
  331.     Dim x As Long
  332.     On Error Resume Next
  333.     x = FileLen(FileName)
  334.     If x Then IfFileExists = True Else IfFileExists = False
  335. End Function
  336.  
  337. Function IsPathValid (DestPath$, ByVal DefaultDrive$) As Integer
  338. '------------------------------------------------------
  339. ' Function:   IsPathValid as integer
  340. ' arguments:  DestPath$         a string that is a full path
  341. '             DefaultDrive$     the default drive.  eg.  "C:"
  342. '
  343. '  If DestPath$ does not include a drive specification,
  344. '  IsValidPath uses Default Drive
  345. '
  346. '  When IsValidPath is finished, DestPath$ is reformated
  347. '  to the format "X:\dir\dir\dir\"
  348. '
  349. ' Result:  True (-1) if path is valid.
  350. '          False (0) if path is invalid
  351. '-------------------------------------------------------
  352.     Dim tmp$
  353.     Dim drive$
  354.     Dim legalChar$
  355.     Dim BackPos As Integer
  356.     Dim forePos As Integer
  357.     Dim Temp$
  358.     Dim i As Integer
  359.     Dim periodPos As Integer
  360.     Dim length As Integer
  361.  
  362.     '----------------------------
  363.     ' Remove left and right spaces
  364.     '----------------------------
  365.     DestPath$ = Trim$(DestPath$)
  366.  
  367.     '-----------------------------
  368.     ' Check Default Drive Parameter
  369.     '-----------------------------
  370.     If Right$(DefaultDrive$, 1) <> ":" Or Len(DefaultDrive$) <> 2 Then
  371.     MsgBox "Bad default drive parameter specified in IsValidPath Function.  You passed,  """ + DefaultDrive$ + """.  Must be one drive letter and "":"".  For example, ""C:"", ""D:""...", 64, "Setup Kit Error"
  372.     GoTo parseErr
  373.     End If
  374.  
  375.     '-------------------------------------------------------
  376.     ' Insert default drive if path begins with root backslash
  377.     '-------------------------------------------------------
  378.     If Left$(DestPath$, 1) = "\" Then DestPath$ = DefaultDrive + DestPath$
  379.     
  380.     '-----------------------------
  381.     ' check for invalid characters
  382.     '-----------------------------
  383.     On Error Resume Next
  384.     tmp$ = Dir$(DestPath$)
  385.     If Err <> 0 Then GoTo parseErr
  386.     
  387.     '-----------------------------------------
  388.     ' Check for wildcard characters and spaces
  389.     '-----------------------------------------
  390.     If (InStr(DestPath$, "*") <> 0) GoTo parseErr
  391.     If (InStr(DestPath$, "?") <> 0) GoTo parseErr
  392.     If (InStr(DestPath$, " ") <> 0) GoTo parseErr
  393.     
  394.     '------------------------------------------
  395.     ' Make Sure colon is in second char position
  396.     '------------------------------------------
  397.     If Mid$(DestPath$, 2, 1) <> Chr$(58) Then GoTo parseErr
  398.     
  399.     '-------------------------------
  400.     ' Insert root backslash if needed
  401.     '-------------------------------
  402.     If Len(DestPath$) > 2 Then
  403.       If Right$(Left$(DestPath$, 3), 1) <> "\" Then
  404.     DestPath$ = Left$(DestPath$, 2) + "\" + Right$(DestPath$, Len(DestPath$) - 2)
  405.       End If
  406.     End If
  407.  
  408.     '-------------------------
  409.     ' Check drive to install on
  410.     '-------------------------
  411.     drive$ = Left$(DestPath$, 1)
  412.     ChDrive (drive$)                                                        ' Try to change to the dest drive
  413.     If Err <> 0 Then GoTo parseErr
  414.     
  415.     '-----------
  416.     ' Add final \
  417.     '-----------
  418.     If Right$(DestPath$, 1) <> "\" Then DestPath$ = DestPath$ + "\"
  419.     
  420.     '-------------------------------------
  421.     ' Root dir is a valid dir
  422.     '-------------------------------------
  423.     If Len(DestPath$) = 3 Then
  424.     If Right$(DestPath$, 2) = ":\" Then GoTo ParseOK
  425.     End If
  426.  
  427.     '------------------------
  428.     ' Check for repeated Slash
  429.     '------------------------
  430.     If InStr(DestPath$, "\\") <> 0 Then GoTo parseErr
  431.     
  432.     '--------------------------------------
  433.     ' Check for illegal directory names
  434.     '--------------------------------------
  435.     legalChar$ = "!#$%&'()-0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`{}~."
  436.     BackPos = 3
  437.     forePos = InStr(4, DestPath$, "\")
  438.     Do
  439.     Temp$ = Mid$(DestPath$, BackPos + 1, forePos - BackPos - 1)
  440.     
  441.     '----------------------------
  442.     ' Test for illegal characters
  443.     '----------------------------
  444.     For i = 1 To Len(Temp$)
  445.         If InStr(legalChar$, UCase$(Mid$(Temp$, i, 1))) = 0 Then GoTo parseErr
  446.     Next i
  447.  
  448.     '-------------------------------------------
  449.     ' Check combinations of periods and lengths
  450.     '-------------------------------------------
  451.     periodPos = InStr(Temp$, ".")
  452.     length = Len(Temp$)
  453.     If periodPos = 0 Then
  454.         If length > 8 Then GoTo parseErr                         ' Base too long
  455.     Else
  456.         If periodPos > 9 Then GoTo parseErr                      ' Base too long
  457.         If length > periodPos + 3 Then GoTo parseErr             ' Extension too long
  458.         If InStr(periodPos + 1, Temp$, ".") <> 0 Then GoTo parseErr' Two periods not allowed
  459.     End If
  460.  
  461.     BackPos = forePos
  462.     forePos = InStr(BackPos + 1, DestPath$, "\")
  463.     Loop Until forePos = 0
  464.  
  465. ParseOK:
  466.     IsPathValid = True
  467. Exit Function
  468.  
  469. parseErr:
  470.     IsPathValid = False
  471. End Function
  472.  
  473. Function LocatePointer ()
  474. '   A way to find out the x and y coordinates of the mouse pointer
  475. '   when the event DblClick or Click is invoked:
  476. '   This routine needs to:
  477. '       A)  Be placed in an event procedure
  478. '                   -- OR --
  479. '       B)  Utilize a GLOBAL variable in place of "Dim Pnt"
  480.  
  481. '----------------------
  482. '   Dim pnt As PointAPI
  483. '   GetCursorPos Pnt
  484. '----------------------
  485.  
  486. '   Pnt.x is the x coordinate in pixels, Pnt.y y-coordinate in pixels.
  487. End Function
  488.  
  489. Function MakeNybble (HexCharacter As String)
  490. '   This routine is called from the GetWindowsVersion routine
  491.     Select Case Left$(HexCharacter, 1)
  492.     Case "0": MakeNybble = "0000"
  493.     Case "1": MakeNybble = "0001"
  494.     Case "2": MakeNybble = "0010"
  495.     Case "3": MakeNybble = "0011"
  496.     Case "4": MakeNybble = "0100"
  497.     Case "5": MakeNybble = "0101"
  498.     Case "6": MakeNybble = "0110"
  499.     Case "7": MakeNybble = "0111"
  500.     Case "8": MakeNybble = "1000"
  501.     Case "9": MakeNybble = "1001"
  502.     Case "A": MakeNybble = "1010"
  503.     Case "B": MakeNybble = "1011"
  504.     Case "C": MakeNybble = "1100"
  505.     Case "D": MakeNybble = "1101"
  506.     Case "E": MakeNybble = "1110"
  507.     Case "F": MakeNybble = "1111"
  508.     End Select
  509.  
  510. End Function
  511.  
  512. Sub MakeReadOnly (TextBoxhWnd%)
  513. '   This routine will cause a textbox to become read-only with no cursor
  514. '   but will NOT gray out the text and the scrollbars will still work.
  515.     Dim x As Integer
  516.  
  517.     x = SendMessage(TextBoxhWnd%, EM_SETREADONLY, 1, 0)
  518.     HideCaret TextBoxhWnd%
  519.     
  520. End Sub
  521.  
  522. Sub PaintForm (FormName As Form, Orientation%, RStart%, GStart%, BStart%, RInc%, GInc%, BInc%)
  523. '   This routine does NOT use API calls
  524.     On Error Resume Next
  525.     Dim x As Integer, y As Integer, z As Integer, Cycles As Integer
  526.     Dim R%, G%, B%
  527.     R% = RStart%: G% = GStart%: B% = BStart%
  528.     If Orientation% = 0 Then
  529.     Cycles = FormName.ScaleHeight \ 100
  530.     Else
  531.     Cycles = FormName.ScaleWidth \ 100
  532.     End If
  533.     For z = 1 To 100
  534.     x = x + 1
  535.     Select Case Orientation
  536.         Case 0: 'Top to Bottom
  537.         If x > FormName.ScaleHeight Then Exit For
  538.         FormName.Line (0, x)-(FormName.Width, x + Cycles - 1), RGB(R%, G%, B%), BF
  539.         Case 1: 'Left to Right
  540.         If x > FormName.ScaleWidth Then Exit For
  541.         FormName.Line (x, 0)-(x + Cycles - 1, FormName.Height), RGB(R%, G%, B%), BF
  542.     End Select
  543.     x = x + Cycles
  544.     R% = R% + RInc%: G% = G% + GInc%: B% = B% + BInc%
  545.     If R% > 255 Then R% = 255
  546.     If R% < 0 Then R% = 0
  547.     If G% > 255 Then G% = 255
  548.     If G% < 0 Then G% = 0
  549.     If B% > 255 Then B% = 255
  550.     If B% < 0 Then B% = 0
  551.     Next z
  552. End Sub
  553.  
  554. Function PurgeNumericInput (StringVal As Variant) As Variant
  555. '   This routine can be used in place of the masked-edit control.
  556. '   It takes a string of text and purges out ALL non-numeric characters
  557. '   (allows periods to remain).  Then returns that string in the form
  558. '   of a variant to the calling procedure.  Use the string to numeric
  559. '   conversion commands of VB to convert the variant into the numeric
  560. '   data type that you would like to process.
  561.  
  562.     On Local Error Resume Next
  563.     Dim x As Integer
  564.     Dim WorkString As String
  565.     
  566.     If Len(Trim(StringVal)) = 0 Then Exit Function
  567.     For x = 1 To Len(StringVal)
  568.     Select Case Mid(StringVal, x, 1)
  569.         Case "0" To "9", "."
  570.         WorkString = WorkString + Mid(StringVal, x, 1)
  571.     End Select
  572.     Next x
  573.     PurgeNumericInput = WorkString
  574. End Function
  575.  
  576. Function PurgeString (Partial As String, Whole As String) As String
  577. '   This routine will search the <Whole> text string for the first
  578. '   occurence of <Partial>.  If it is found, it will be removed from
  579. '   the string.
  580.  
  581.     On Error Resume Next
  582.     PurgeString = ""
  583.     If Len(Partial) < 1 Then Exit Function
  584.     If Len(Whole) < 1 Then Exit Function
  585.     Dim x As Integer
  586.     Dim WorkStr As String
  587.     WorkStr = Whole
  588.     Do
  589.     x = InStr(WorkStr, Partial)
  590.     If x = 0 Then Exit Do
  591.     WorkStr = Left(WorkStr, x - 1) + Mid(WorkStr, x + Len(Partial))
  592.     Loop
  593.     PurgeString = WorkStr
  594. End Function
  595.  
  596. Function ReadFileChunk (FileName As String, Action As Integer) As String
  597. '   This routine reads the contents of ANY file (ignoring delimiters)
  598. '   and returns the entire file in the function.  This
  599.     
  600. '   <Action=0> = From the beginning
  601. '   <Action=1> = Continue from last chunk read
  602.     Dim FBuffer As Integer
  603.     Static CyclesRead As Integer
  604.     Static FullLenString As String * 1000
  605.     Static PartialString As String
  606.     Dim Indicator As Long
  607.     Static Temp As String
  608.     
  609.     If Action = 0 Then CyclesRead = 0
  610.     
  611.     FBuffer = FreeFile
  612.     Open FileName For Random As #FBuffer Len = 1000
  613.     If CyclesRead * 1000 > LOF(FBuffer) Then
  614.     Close #FBuffer: Exit Function' You've read all there is
  615.     End If
  616.     CyclesRead = CyclesRead + 1
  617.     Get #FBuffer, CyclesRead, FullLenString
  618.     If LOF(FBuffer) - (CyclesRead * 1000) > -1 Then
  619.     'Not yet gone past end of file - return everything just read
  620.     ReadFileChunk = FullLenString
  621.     Else
  622.     'You just read past the end of file
  623.     'Obtain the portion you want and discard the rest
  624.     ReadFileChunk = Left(FullLenString, LOF(FBuffer) - (CyclesRead * 1000) + 1000)
  625.     End If
  626.     Close #FBuffer
  627. End Function
  628.  
  629. Function RebootSystem () As Integer
  630. '   Causes the computer to be rebooted
  631. '   If any programs refuse to terminate, then this function
  632. '   will return a ZERO
  633.     Dim i As Integer
  634.     Dim EW_REBOOTSYSTEM As Long
  635.     EW_REBOOTSYSTEM = &H43
  636.     i = ExitWindows(EW_REBOOTSYSTEM, 0)
  637. End Function
  638.  
  639. Sub ResourceMonitor ()
  640. '   Gets Free System Resources and displays them in a
  641. '   message box as percentages
  642.  
  643.     Dim FreeSystemResources As String
  644.     Dim FreeGDIResources    As String
  645.     Dim FreeUserResources   As String
  646.     Dim MsgBoxText          As String
  647.  
  648.     FreeSystemResources = CStr(GetFreeSystemResources(&H0)) & "%"
  649.     FreeGDIResources = CStr(GetFreeSystemResources(&H1)) & "%"
  650.     FreeUserResources = CStr(GetFreeSystemResources(&H2)) & "%"
  651.     
  652.     MsgBoxText = "Free System Resoruces: " + FreeSystemResources + Chr(13) + Chr(10)
  653.     MsgBoxText = MsgBoxText + "Free GDI Resoruces:    " + FreeGDIResources + Chr(13) + Chr(10)
  654.     MsgBoxText = MsgBoxText + "Free User Resoruces:   " + FreeUserResources + Chr(13) + Chr(10)
  655.     MsgBox MsgBoxText, 48, "Free System Resources"
  656.  
  657. End Sub
  658.  
  659. Function RestartWindows () As Integer
  660. '   Causes Windows to Restart
  661. '   If any programs refuse to terminate, then this function
  662. '   will return a ZERO
  663.     Dim i As Integer
  664.     Dim EW_RESTARTWINDOWS As Long
  665.     EW_RESTARTWINDOWS = &H42
  666.     i = ExitWindows(EW_RESTARTWINDOWS, 0)
  667. End Function
  668.  
  669. Sub SelectListItem (lst As Control, Idx As String)
  670. '   This routine will highlight a given line in a listbox based on the
  671. '   String being searched for.
  672.     Dim i As Integer
  673.     i = SendMessage(lst.hWnd, LB_SELECTSTRING, -1, ByVal Idx)
  674. End Sub
  675.  
  676. Function Upeach (msg As Variant)
  677.   On Error Resume Next
  678.   Dim length As Integer
  679.   Dim tmpmsg As String
  680.   Dim count As Integer
  681.  
  682.   length = Len(msg): If length < 1 Then Exit Function
  683.   tmpmsg = UCase$(Left$(msg, 1))
  684.   For count = 2 To length
  685.     If Mid$(msg, count - 1, 1) = " " Or Mid$(msg, count - 1, 1) = "-" Then
  686.       tmpmsg = tmpmsg + UCase$(Mid$(msg, count, 1))
  687.     ElseIf count = 3 Then
  688.       If UCase(Mid$(msg, count - 2, 2)) = "MC" Then
  689.     tmpmsg = tmpmsg + UCase$(Mid$(msg, count, 1))
  690.       Else
  691.     tmpmsg = tmpmsg + Mid$(msg, count, 1)
  692.       End If
  693.     Else
  694.       tmpmsg = tmpmsg + Mid$(msg, count, 1)
  695.     End If
  696.   Next count
  697.   Upeach = tmpmsg
  698. End Function
  699.  
  700. Function UpFirst (msg As Variant) As String
  701. 'Capitalize 1st Letter Of Argument Only
  702. 'Capitalizes the first letter of the passed argument (Assumes a string)
  703.     Dim length As Integer
  704.     length = Len(msg)
  705.     Select Case length
  706.     Case 0: UpFirst = ""
  707.     Case 1: UpFirst = UCase(msg)
  708.     Case Else: UpFirst = UCase(Left(msg, 1)) + Right(msg, length - 1)
  709.     End Select
  710. End Function
  711.  
  712.