home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 6_2008-2009.ISO / data / zips / Multiple_D2178123292010.psc / INIclass.cls < prev    next >
Text File  |  2007-08-26  |  9KB  |  277 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "INIclass"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15. ' =========================================================
  16. ' Class:    cIniFile
  17. ' Author:   Steve McMahon
  18. ' Date  :   21 Feb 1997
  19. '
  20. ' A nice class wrapper around the INIFile functions
  21. ' Allows searching,deletion,modification and addition
  22. ' of Keys or Values.
  23. '
  24. ' Updated 10 May 1998 for VB5.
  25. '   * Added EnumerateAllSections method
  26. '   * Added Load and Save form position methods
  27. ' =========================================================
  28.  
  29. Private m_sPath As String
  30. Private m_sKey As String
  31. Private m_sSection As String
  32. Private m_sDefault As String
  33. Private m_lLastReturnCode As Long
  34.  
  35. #If Win32 Then
  36.     ' Profile String functions:
  37.     Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
  38.     Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As Any, ByVal lpKeyName As Any, ByVal lpDefault As Any, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
  39. #Else
  40.     ' Profile String functions:
  41.     Private Declare Function WritePrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Integer
  42.     Private Declare Function GetPrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As Any, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
  43. #End If
  44.  
  45. Property Get LastReturnCode() As Long
  46.     LastReturnCode = m_lLastReturnCode
  47. End Property
  48. Property Get Success() As Boolean
  49.     Success = (m_lLastReturnCode <> 0)
  50. End Property
  51. Property Let Default(sDefault As String)
  52.     m_sDefault = sDefault
  53. End Property
  54. Property Get Default() As String
  55.     Default = m_sDefault
  56. End Property
  57. Property Let Path(sPath As String)
  58.     m_sPath = sPath
  59. End Property
  60. Property Get Path() As String
  61.     Path = m_sPath
  62. End Property
  63. Property Let Key(sKey As String)
  64.     m_sKey = sKey
  65. End Property
  66. Property Get Key() As String
  67.     Key = m_sKey
  68. End Property
  69. Property Let Section(sSection As String)
  70.     m_sSection = sSection
  71. End Property
  72. Property Get Section() As String
  73.     Section = m_sSection
  74. End Property
  75. Property Get Value() As String
  76. Dim sBuf As String
  77. Dim iSize As String
  78. Dim iRetCode As Integer
  79.  
  80.     sBuf = Space$(255)
  81.     iSize = Len(sBuf)
  82.     iRetCode = GetPrivateProfileString(m_sSection, m_sKey, m_sDefault, sBuf, iSize, m_sPath)
  83.     If (iSize > 0) Then
  84.         Value = Left$(sBuf, iRetCode)
  85.     Else
  86.         Value = ""
  87.     End If
  88.  
  89. End Property
  90. Property Let Value(sValue As String)
  91. Dim iPos As Integer
  92.     ' Strip chr$(0):
  93.     iPos = InStr(sValue, Chr$(0))
  94.     Do While iPos <> 0
  95.         sValue = Left$(sValue, (iPos - 1)) & Mid$(sValue, (iPos + 1))
  96.         iPos = InStr(sValue, Chr$(0))
  97.     Loop
  98.     m_lLastReturnCode = WritePrivateProfileString(m_sSection, m_sKey, sValue, m_sPath)
  99. End Property
  100. Public Sub DeleteKey()
  101.     m_lLastReturnCode = WritePrivateProfileString(m_sSection, m_sKey, 0&, m_sPath)
  102. End Sub
  103. Public Sub DeleteSection()
  104.     m_lLastReturnCode = WritePrivateProfileString(m_sSection, 0&, 0&, m_sPath)
  105. End Sub
  106. Property Get INISection() As String
  107. Dim sBuf As String
  108. Dim iSize As String
  109. Dim iRetCode As Integer
  110.  
  111.     sBuf = Space$(8192)
  112.     iSize = Len(sBuf)
  113.     iRetCode = GetPrivateProfileString(m_sSection, 0&, m_sDefault, sBuf, iSize, m_sPath)
  114.     If (iSize > 0) Then
  115.         INISection = Left$(sBuf, iRetCode)
  116.     Else
  117.         INISection = ""
  118.     End If
  119.  
  120. End Property
  121. Property Let INISection(sSection As String)
  122.     m_lLastReturnCode = WritePrivateProfileString(m_sSection, 0&, sSection, m_sPath)
  123. End Property
  124. Property Get Sections() As String
  125. Dim sBuf As String
  126. Dim iSize As String
  127. Dim iRetCode As Integer
  128.  
  129.     sBuf = Space$(8192)
  130.     iSize = Len(sBuf)
  131.     iRetCode = GetPrivateProfileString(0&, 0&, m_sDefault, sBuf, iSize, m_sPath)
  132.     If (iSize > 0) Then
  133.         Sections = Left$(sBuf, iRetCode)
  134.     Else
  135.         Sections = ""
  136.     End If
  137.  
  138. End Property
  139. Public Sub EnumerateCurrentSection(ByRef sKey() As String, ByRef iCount As Long)
  140. Dim sSection As String
  141. Dim iPos As Long
  142. Dim iNextPos As Long
  143. Dim sCur As String
  144.     
  145.     iCount = 0
  146.     Erase sKey
  147.     sSection = INISection
  148.     If (Len(sSection) > 0) Then
  149.         iPos = 1
  150.         iNextPos = InStr(iPos, sSection, Chr$(0))
  151.         Do While iNextPos <> 0
  152.             sCur = Mid$(sSection, iPos, (iNextPos - iPos))
  153.             If (sCur <> Chr$(0)) Then
  154.                 iCount = iCount + 1
  155.                 ReDim Preserve sKey(1 To iCount) As String
  156.                 sKey(iCount) = Mid$(sSection, iPos, (iNextPos - iPos))
  157.                 iPos = iNextPos + 1
  158.                 iNextPos = InStr(iPos, sSection, Chr$(0))
  159.             End If
  160.         Loop
  161.     End If
  162. End Sub
  163. Public Sub EnumerateAllSections(ByRef sSections() As String, ByRef iCount As Long)
  164. Dim sIniFile As String
  165. Dim iPos As Long
  166. Dim iNextPos As Long
  167. Dim sCur As String
  168.     
  169.     iCount = 0
  170.     Erase sSections
  171.     sIniFile = Sections
  172.     If (Len(sIniFile) > 0) Then
  173.         iPos = 1
  174.         iNextPos = InStr(iPos, sIniFile, Chr$(0))
  175.         Do While iNextPos <> 0
  176.             If (iNextPos <> iPos) Then
  177.                 sCur = Mid$(sIniFile, iPos, (iNextPos - iPos))
  178.                 iCount = iCount + 1
  179.                 ReDim Preserve sSections(1 To iCount) As String
  180.                 sSections(iCount) = sCur
  181.             End If
  182.             iPos = iNextPos + 1
  183.             iNextPos = InStr(iPos, sIniFile, Chr$(0))
  184.         Loop
  185.     End If
  186.  
  187. End Sub
  188. Public Sub SaveFormPosition(ByRef frmThis As Object)
  189. Dim sSaveKey As String
  190. Dim sSaveDefault As String
  191. On Error GoTo SaveError
  192.     sSaveKey = Key
  193.     If Not (frmThis.WindowState = vbMinimized) Then
  194.         Key = "Maximised"
  195.         Value = (frmThis.WindowState = vbMaximized) * -1
  196.         If (frmThis.WindowState <> vbMaximized) Then
  197.             Key = "Left"
  198.             Value = frmThis.Left
  199.             Key = "Top"
  200.             Value = frmThis.Top
  201.             Key = "Width"
  202.             Value = frmThis.Width
  203.             Key = "Height"
  204.             Value = frmThis.Height
  205.         End If
  206.     End If
  207.     Key = sSaveKey
  208.     Exit Sub
  209. SaveError:
  210.     Key = sSaveKey
  211.     m_lLastReturnCode = 0
  212.     Exit Sub
  213. End Sub
  214. Public Sub LoadFormPosition(ByRef frmThis As Object, Optional ByRef lMinWidth = 3000, Optional ByRef lMinHeight = 3000)
  215. Dim sSaveKey As String
  216. Dim sSaveDefault As String
  217. Dim lLeft As Long
  218. Dim lTOp As Long
  219. Dim lWidth As Long
  220. Dim lHeight As Long
  221. On Error GoTo LoadError
  222.     sSaveKey = Key
  223.     sSaveDefault = Default
  224.     Default = "FAIL"
  225.     Key = "Left"
  226.     lLeft = CLngDefault(Value, frmThis.Left)
  227.     Key = "Top"
  228.     lTOp = CLngDefault(Value, frmThis.Top)
  229.     Key = "Width"
  230.     lWidth = CLngDefault(Value, frmThis.Width)
  231.     If (lWidth < lMinWidth) Then lWidth = lMinWidth
  232.     Key = "Height"
  233.     lHeight = CLngDefault(Value, frmThis.Height)
  234.     If (lHeight < lMinHeight) Then lHeight = lMinHeight
  235.     If (lLeft < 4 * Screen.TwipsPerPixelX) Then lLeft = 4 * Screen.TwipsPerPixelX
  236.     If (lTOp < 4 * Screen.TwipsPerPixelY) Then lTOp = 4 * Screen.TwipsPerPixelY
  237.     If (lLeft + lWidth > Screen.Width - 4 * Screen.TwipsPerPixelX) Then
  238.         lLeft = Screen.Width - 4 * Screen.TwipsPerPixelX - lWidth
  239.         If (lLeft < 4 * Screen.TwipsPerPixelX) Then lLeft = 4 * Screen.TwipsPerPixelX
  240.         If (lLeft + lWidth > Screen.Width - 4 * Screen.TwipsPerPixelX) Then
  241.             lWidth = Screen.Width - lLeft - 4 * Screen.TwipsPerPixelX
  242.         End If
  243.     End If
  244.     If (lTOp + lHeight > Screen.Height - 4 * Screen.TwipsPerPixelY) Then
  245.         lTOp = Screen.Height - 4 * Screen.TwipsPerPixelY - lHeight
  246.         If (lTOp < 4 * Screen.TwipsPerPixelY) Then lTOp = 4 * Screen.TwipsPerPixelY
  247.         If (lTOp + lHeight > Screen.Height - 4 * Screen.TwipsPerPixelY) Then
  248.             lHeight = Screen.Height - lTOp - 4 * Screen.TwipsPerPixelY
  249.         End If
  250.     End If
  251.     If (lWidth >= lMinWidth) And (lHeight >= lMinHeight) Then
  252.         frmThis.Move lLeft, lTOp, lWidth, lHeight
  253.     End If
  254.     Key = "Maximised"
  255.     If (CLngDefault(Value, 0) <> 0) Then
  256.         frmThis.WindowState = vbMaximized
  257.     End If
  258.     Key = sSaveKey
  259.     Default = sSaveDefault
  260.     Exit Sub
  261. LoadError:
  262.     Key = sSaveKey
  263.     Default = sSaveDefault
  264.     m_lLastReturnCode = 0
  265.     Exit Sub
  266. End Sub
  267. Public Function CLngDefault(ByVal sString As String, Optional ByVal lDefault As Long = 0) As Long
  268. Dim lR As Long
  269. On Error Resume Next
  270.     lR = CLng(sString)
  271.     If (Err.Number <> 0) Then
  272.         CLngDefault = lDefault
  273.     Else
  274.         CLngDefault = lR
  275.     End If
  276. End Function
  277.