home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD13572192001.psc / Module1.bas < prev   
Encoding:
BASIC Source File  |  2001-01-09  |  7.1 KB  |  200 lines

  1. Attribute VB_Name = "Module1"
  2. Option Explicit
  3. 'Used for Undo/redo and loading the initial message from me
  4. Private Declare Function GetTempFilename Lib "Kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, _
  5.     ByVal lpPrefixString As String, _
  6.     ByVal wUnique As Long, _
  7.     ByVal lpTempFilename As String _
  8.     ) As Long
  9. Private Declare Function GetTempPath Lib "Kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
  10. 'Used to "Float" the Find/Replace window
  11. Global Const SWP_NOMOVE = 2
  12. Global Const SWP_NOSIZE = 1
  13. Global Const HWND_TOPMOST = -1
  14. Global Const HWND_NOTOPMOST = -2
  15. Global Const FLOAT = 1, SINK = 0
  16. Public Declare Sub 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)
  17. 'This just stops things jumping around and updating before we're ready
  18. Public Declare Function LockWindowUpdate Lib "user32" (ByVal hWnd As Long) As Long
  19. 'This stops the Find window unloading till Program close when we save settings
  20. Public FinalClose As Boolean
  21. 'Set a form to On Top
  22. Sub FloatWindow(x As Long, action As Integer)
  23. Dim wFlags As Integer, result As Integer
  24. wFlags = SWP_NOMOVE Or SWP_NOSIZE
  25. If action <> 0 Then
  26.     Call SetWindowPos(x, HWND_TOPMOST, 0, 0, 0, 0, wFlags)
  27. Else
  28.     Call SetWindowPos(x, HWND_NOTOPMOST, 0, 0, 0, 0, wFlags)
  29. End If
  30. End Sub
  31. 'The next 4 functions make handling files easier by parsing
  32. Public Function PathOnly(ByVal filepath As String) As String
  33. Dim temp As String
  34.     temp = Mid$(filepath, 1, InStrRev(filepath, "\"))
  35.     If Right(temp, 1) = "\" Then temp = Left(temp, Len(temp) - 1)
  36.     PathOnly = temp
  37. End Function
  38. Public Function FileOnly(ByVal filepath As String) As String
  39.     FileOnly = Mid$(filepath, InStrRev(filepath, "\") + 1)
  40. End Function
  41. Public Function ExtOnly(ByVal filepath As String, Optional dot As Boolean) As String
  42.     ExtOnly = Mid$(filepath, InStrRev(filepath, ".") + 1)
  43. If dot = True Then ExtOnly = "." + ExtOnly
  44. End Function
  45. Public Function ChangeExt(ByVal filepath As String, Optional newext As String) As String
  46. Dim temp As String
  47. temp = Mid$(filepath, 1, InStrRev(filepath, "."))
  48. temp = Left(temp, Len(temp) - 1)
  49. If newext <> "" Then newext = "." + newext
  50. ChangeExt = temp + newext
  51. End Function
  52. 'Save a plain text file without the RTF gobbledygook
  53. Public Sub FileSave(Text As String, filepath As String)
  54. On Error Resume Next
  55. Dim Directory As String
  56.               Directory$ = filepath
  57.               Open Directory$ For Output As #1
  58.            Print #1, Text
  59.        Close #1
  60. Exit Sub
  61. End Sub
  62. 'Stops us attempting to do something to a file that isn't there
  63. Function FileExists(ByVal FileName As String) As Integer
  64. Dim temp$, MB_OK
  65.     FileExists = True
  66. On Error Resume Next
  67.     temp$ = FileDateTime(FileName)
  68.     Select Case Err
  69.         Case 53, 76, 68
  70.             FileExists = False
  71.             Err = 0
  72.         Case Else
  73.             If Err <> 0 Then
  74.                 MsgBox "Error Number: " & Err & Chr$(10) & Chr$(13) & " " & Error, MB_OK, "Error"
  75.             End If
  76.     End Select
  77. End Function
  78. 'API often returns a string with a null character at the end
  79. Public Function StripTerminator(ByVal strString As String) As String
  80.     Dim intZeroPos As Integer
  81.     intZeroPos = InStr(strString, Chr$(0))
  82.     If intZeroPos > 0 Then
  83.         StripTerminator = Left$(strString, intZeroPos - 1)
  84.     Else
  85.         StripTerminator = strString
  86.     End If
  87. End Function
  88. 'Where's the temp folder ?
  89. Public Function temppath() As String
  90.     Dim sBuffer As String
  91.     Dim lRet As Long
  92.     sBuffer = String$(255, vbNullChar)
  93.     lRet = GetTempPath(255, sBuffer)
  94.     If lRet > 0 Then
  95.         sBuffer = Left$(sBuffer, lRet)
  96.     End If
  97.     temppath = sBuffer
  98.     If Right(temppath, 1) = "\" Then temppath = Left(temppath, Len(temppath) - 1)
  99. End Function
  100. 'Get a unique name for a temp file
  101. Public Function GetTempFile(lpTempFilename As String) As Boolean
  102.     lpTempFilename = String(255, vbNullChar)
  103.     GetTempFile = GetTempFilename(temppath, "bb", 0, lpTempFilename) > 0
  104.     lpTempFilename = StripTerminator(lpTempFilename)
  105. End Function
  106. 'Shrink a path to fit a label or text box
  107. Public Function LabelEdit(Path As String, Length As Integer) As String
  108. Dim temp As String, temp1 As String, temp2 As String
  109. If Len(Path) < Length Then
  110.     LabelEdit = Path
  111.     Exit Function
  112. End If
  113. temp = Mid$(Path, InStrRev(Path, "\") + 1)
  114. If Len(temp) + 7 < Length Then
  115.     temp1 = Mid$(Path, 1, InStrRev(Path, "\") - 1)
  116.     temp2 = Mid$(temp1, InStrRev(temp1, "\") + 1)
  117.     If Len(temp) + 7 + Len(temp2) < Length Then temp = temp2 + "\" + temp
  118. End If
  119. LabelEdit = Left(Path, 3) + "...\" + temp
  120. End Function
  121. 'Remove empty lines from a richtextbox
  122. Public Sub RemoveRTFblanks(RTF As RichTextBox)
  123. Dim textfound As Long
  124. Dim Position As Long
  125. Dim St As Long
  126. Dim Lng As Long
  127. Dim temp As String
  128. St = 0
  129. If RTF.Text = "" Then Exit Sub
  130. Screen.MousePointer = 11
  131. LockWindowUpdate RTF.hWnd
  132. Do Until Position >= Len(RTF.Text)
  133.     textfound = RTF.Find(vbCrLf, St)
  134.     If textfound = -1 Then GoTo fin
  135.     Position = RTF.SelStart + RTF.SelLength
  136.     RTF.SelStart = St
  137.     RTF.SelLength = Position - St - 1
  138.     If RTF.SelText <> String(Len(RTF.SelText), " ") Then temp = temp + RTF.SelText + vbCrLf
  139.     St = Position
  140. Loop
  141. fin:
  142. RTF.Text = temp
  143. LockWindowUpdate 0
  144. Screen.MousePointer = 0
  145.  
  146. End Sub
  147. 'Used to load my initial blurb
  148. Public Sub LoadRTFres(rtftext As RichTextBox, mynum As Integer)
  149. Dim sFileName As String
  150. If GetTempFile2("", "~rs", 0, sFileName) Then
  151.     If Not SaveResItemToDisk(mynum, "Custom", sFileName) Then
  152.         rtftext.LoadFile sFileName
  153.         Kill sFileName
  154.     Else
  155.         MsgBox "Unable to save resource item to disk!", vbCritical
  156.     End If
  157. Else
  158.     MsgBox "Unable to get temp file name!", vbCritical
  159. End If
  160.  
  161. End Sub
  162. 'Used to load my initial blurb
  163. Public Function GetTempFile2( _
  164.     ByVal strDestPath As String, _
  165.     ByVal lpPrefixString As String, _
  166.     ByVal wUnique As Integer, _
  167.     lpTempFilename As String _
  168.     ) As Boolean
  169.    If strDestPath = "" Then
  170.         strDestPath = String(255, vbNullChar)
  171.         If GetTempPath(255, strDestPath) = 0 Then
  172.             GetTempFile2 = False
  173.             Exit Function
  174.         End If
  175.     End If
  176.     lpTempFilename = String(255, vbNullChar)
  177.     GetTempFile2 = GetTempFilename(strDestPath, lpPrefixString, wUnique, lpTempFilename) > 0
  178.     lpTempFilename = StripTerminator(lpTempFilename)
  179. End Function
  180. 'Used to load my initial blurb
  181. Public Function SaveResItemToDisk( _
  182.             ByVal iResourceNum As Integer, _
  183.             ByVal sResourceType As String, _
  184.             ByVal sDestFileName As String _
  185.             ) As Long
  186.     Dim bytResourceData()   As Byte
  187.     Dim iFileNumOut         As Integer
  188.     On Error GoTo SaveResItemToDisk_err
  189.     bytResourceData = LoadResData(iResourceNum, sResourceType)
  190.     iFileNumOut = FreeFile
  191.     Open sDestFileName For Binary Access Write As #iFileNumOut
  192.         Put #iFileNumOut, , bytResourceData
  193.     Close #iFileNumOut
  194.     SaveResItemToDisk = 0
  195.     Exit Function
  196. SaveResItemToDisk_err:
  197.     SaveResItemToDisk = Err.Number
  198. End Function
  199.  
  200.