home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Programmer'…arterly (Limited Edition) / Visual_Basic_Programmers_Journal_VB-CD_Quarterly_Limited_Edition_1995.iso / code / ch26code / app.cls < prev    next >
Text File  |  1995-08-01  |  18KB  |  362 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "Application"
  6. Attribute VB_Creatable = True
  7. Attribute VB_Exposed = True
  8. '**************************************************************
  9. ' APP.CLS - This is the application class which is exposed
  10. '           to other OLE Automation clients. It provides some
  11. '           handy routines that aren't included in VB, and it
  12. '           is a good demonstration on how to write a OLE server
  13. '           that can be used with other Office apps.
  14. '**************************************************************
  15. Option Explicit
  16. '**************************************************************
  17. ' Hidden API Functions for private use only
  18. '**************************************************************
  19. #If Win32 Then
  20. Private Declare Function GetPrivateProfileInt Lib "kernel32" _
  21.     Alias "GetPrivateProfileIntA" (ByVal lpApplicationName$, _
  22.     ByVal lpKeyName As String, ByVal nDefault As Long, ByVal _
  23.     lpFileName As String) As Long
  24.  
  25. Private Declare Function GetPrivateProfileString Lib "kernel32" _
  26.     Alias "GetPrivateProfileStringA" (ByVal lpApplicationName$, _
  27.     lpKeyName As Any, ByVal lpDefault As String, ByVal _
  28.     lpReturnedString As String, ByVal nSize As Long, ByVal _
  29.     lpFileName As String) As Long
  30.  
  31. Private Declare Function WritePrivateProfileString Lib _
  32.     "kernel32" Alias "WritePrivateProfileStringA" (ByVal _
  33.     lpApplicationName As String, lpKeyName As Any, lpString _
  34.     As Any, ByVal lplFileName As String) As Long
  35.  
  36. Private Declare Function GetShortPathName Lib "kernel32" Alias _
  37.     "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal _
  38.     lpszShortPath As String, ByVal cchBuffer As Long) As Long
  39. #Else
  40. Private Declare Function GetPrivateProfileInt Lib "Kernel" _
  41.         (ByVal lpApplicationName As String, ByVal lpKeyName _
  42.         As String, ByVal nDefault As Integer, ByVal lpFileName _
  43.         As String) As Integer
  44.  
  45. Private Declare Function GetPrivateProfileString Lib "Kernel" _
  46.         (ByVal lpAppName As Any, ByVal lpKeyName As Any, _
  47.         ByVal lpDefault As String, ByVal lpReturnedString _
  48.         As String, ByVal nSize As Integer, ByVal lpFileName _
  49.         As String) As Integer
  50.  
  51. Private Declare Function WritePrivateProfileString% Lib "Kernel" _
  52.         (ByVal lpAppName As Any, ByVal lpKeyName As Any, ByVal _
  53.         lpString As Any, ByVal lpFileName As String)
  54. #End If
  55. '**************************************************************
  56. ' Hidden variable for this class
  57. '**************************************************************
  58. Private thisAbout As New About
  59. '**************************************************************
  60. ' Description: This proceedure displays an about box
  61. '
  62. ' Arguments:
  63. ' AsSplash  (Boolean)- Display as splash screen?
  64. ' App       (String) - The name of your application
  65. ' AppCompany(String) - The name of your company
  66. ' VerNum    (String) - The version number of your app
  67. ' User      (String) - The name of the registered user
  68. ' Company   (String) - The User's company name
  69. ' RegNum    (String) - The User's registration number
  70. ' AboutMsg  (String) - Your about box message that goes
  71. '                      between the 2 black lines
  72. ' IconProg  (String) - The filename (without a extension)
  73. '                      of the running app that contains
  74. '                      the icon you would like to use.
  75. '                      The default is Progman
  76. '                      (for Program Manager)
  77. '
  78. ' IconIdx   (Long)   - The 1 based index of the icon
  79. '                      stored in IconProg. The default
  80. '                      is 1
  81. '**************************************************************
  82. Public Sub ShowAboutBox(AsSplash As Boolean, _
  83.                         Optional App, _
  84.                         Optional AppCompany, _
  85.                         Optional VerNum, _
  86.                         Optional User, _
  87.                         Optional Company, _
  88.                         Optional RegNum, _
  89.                         Optional AboutMsg)
  90.  
  91.     '**********************************************************
  92.     ' You should only set the properties if the argument was
  93.     ' provided. Otherwise, just let the default values appear.
  94.     '**********************************************************
  95.     If Not IsMissing(App) Then thisAbout.AppName = App
  96.     If Not IsMissing(AppCompany) Then _
  97.                         thisAbout.AppCompanyName = AppCompany
  98.     If Not IsMissing(VerNum) Then thisAbout.VersionNumber = VerNum
  99.     If Not IsMissing(User) Then thisAbout.UserName = User
  100.     If Not IsMissing(Company) Then thisAbout.CompanyName = Company
  101.     If Not IsMissing(RegNum) Then thisAbout.Registration = RegNum
  102.     If Not IsMissing(AboutMsg) Then thisAbout.Message = AboutMsg
  103.     '**********************************************************
  104.     ' Show it using the About object
  105.     '**********************************************************
  106.     thisAbout.ShowAbout AsSplash
  107. End Sub
  108. '**************************************************************
  109. ' Returns a reference to an About object so that its properties
  110. ' may be accessed individually.
  111. '**************************************************************
  112. Public Property Get CreateAbout() As Object
  113. Attribute CreateAbout.VB_Description = "Returns an About object, so that you may access the class directly."
  114.     Set CreateAbout = thisAbout
  115. End Property
  116. '**************************************************************
  117. ' Unload via the About object
  118. '**************************************************************
  119. Public Sub UnloadSplash()
  120. Attribute UnloadSplash.VB_Description = "Unloads the splash screen"
  121.     thisAbout.HideSplash
  122. End Sub
  123. '**************************************************************
  124. ' This method is just a wrapper for the global function which
  125. ' the about object needs too. This demonstrates how you can
  126. ' expose non-class objects.
  127. '**************************************************************
  128. ' NOTE: You may be wondering why I didn't just put the code
  129. '       in here, and require other modules to just call this
  130. '       one. The reason is that this is a class. If another
  131. '       module wants to use a class method, then they must
  132. '       create an object which consumes a great deal of
  133. '       memory. This method exposes our object, but it also
  134. '       leaves it available to all forms by putting it into
  135. '       a module. This duplication is actually an optimization.
  136. '**************************************************************
  137. #If Win32 Then
  138. Public Sub AlwaysOnTop(ByVal hwnd&, ResetWindow As Boolean)
  139. #Else
  140. Public Sub AlwaysOnTop(ByVal hwnd%, ResetWindow As Boolean)
  141. Attribute AlwaysOnTop.VB_Description = "Sets or removes the topmost setting from a given window."
  142. #End If
  143.     Common.AlwaysOnTop hwnd, ResetWindow
  144. End Sub
  145. '**************************************************************
  146. ' This method is a wrapper for Common.FileExists.
  147. '**************************************************************
  148. Public Function FileExists(FileName$) As Boolean
  149. Attribute FileExists.VB_Description = "Checks to see if a file exists"
  150.     FileExists = Common.FileExists(FileName)
  151. End Function
  152. '**************************************************************
  153. ' This method is a wrapper for Common.GetWinDir.
  154. '**************************************************************
  155. Public Function GetWinDir(WithSlash As Boolean) As String
  156. Attribute GetWinDir.VB_Description = "Returns the path to the Windows directory."
  157.     GetWinDir = Common.GetWinDir(WithSlash)
  158. End Function
  159. #If Win32 Then
  160. '**************************************************************
  161. ' This function converts a long file name into a DOS compatible
  162. ' short file name.
  163. '**************************************************************
  164. Private Function GetShortName(LongFileName As String) As String
  165. Dim strFileName As String
  166.     strFileName = Space(2048)
  167.     GetShortName = Left(strFileName, GetShortPathName _
  168.         (LongFileName, strFileName, Len(strFileName)))
  169. End Function
  170. #End If
  171. '**************************************************************
  172. ' This method extracts the filename (with extension) from a
  173. ' fully qualified path. If path = "c:\autoexec.bat", then
  174. ' this method returns "autoexec.bat".
  175. '**************************************************************
  176. ' NOTE: This method is not used by any modules or forms in this
  177. ' project, so its code belongs here.
  178. '**************************************************************
  179. '***************************************************************
  180. ' WARNING: This function modifies Path, so ByVal is required.
  181. '***************************************************************
  182. Public Function ExtractFileName(ByVal Path As String) As String
  183. Attribute ExtractFileName.VB_Description = "Extracts the filename from fully qualified path."
  184. Dim res%
  185.     '***********************************************************
  186.     ' One of the few uses for GoTo is as an error handler,and
  187.     ' this is a great example of how to use them.
  188.     '***********************************************************
  189.     On Error GoTo ExtractFileName_Err
  190.     '***********************************************************
  191.     ' Since a filename (with extension) in DOS can only be
  192.     ' a maximum of 13 chars (8 + 1 + 3), get rid of the rest.
  193.     '***********************************************************
  194.     #If Win32 Then ' Convert LFN's to SFN's
  195.         Path = GetShortName(Path)
  196.     #End If
  197.     If Len(Path) > 13 Then Path = Right(Path, 13)
  198.     res = InStr(Path, "\")
  199.     '***********************************************************
  200.     ' Get rid of the rest of the garbage by looking for slashes.
  201.     '***********************************************************
  202.     Do While res <> 0
  203.         Path = Mid$(Path, res + 1, Len(Path))
  204.         res = InStr(Path, "\")
  205.     Loop
  206.     '***********************************************************
  207.     ' Return the result, and exit the function to prevent
  208.     ' executing the error handler.
  209.     '***********************************************************
  210.     ExtractFileName = Path
  211.     Exit Function
  212. '***************************************************************
  213. ' Our error handler calls an external module's generic error
  214. ' handler, and exits to prevent further damage.
  215. '***************************************************************
  216. ExtractFileName_Err:
  217.     ErrHandler Err, "ExtractFileName"
  218.     Exit Function
  219. End Function
  220. '**************************************************************
  221. ' Calls the API to read an INI file, and return the results.
  222. ' A large buffer is used so that this function can be used
  223. ' in any app without causing a GPF.
  224. '***************************************************************
  225. ' NOTE: ByVal is used, so you can pass control values such
  226. '       as Text1.Text without surrounding it in parenthesis.
  227. '**************************************************************
  228. Public Function GetINI(ByVal Section$, ByVal Key$, ByVal _
  229.                        Default$, ByVal FileName$) As String
  230. Dim res&, retVal$
  231.     retVal = Space$(32400)
  232.     res = GetPrivateProfileString(Section, Key, Default, _
  233.                             retVal, Len(retVal), FileName)
  234.     GetINI = Left$(retVal, res)
  235. End Function
  236. '**************************************************************
  237. ' Same as above, but it returns an integer.
  238. '**************************************************************
  239. Public Function GetINIInt(ByVal Section$, ByVal Key$, ByVal _
  240.                         Default%, ByVal FileName$) As Integer
  241.     GetINIInt = GetPrivateProfileInt(Section, Key, Default, FileName)
  242. End Function
  243. '**************************************************************
  244. ' This function is useful with SendMessage and GetVersion
  245. ' so you can get the low order word.
  246. '**************************************************************
  247. Public Function GetLoWord(ByVal DWORD&) As Long
  248.     GetLoWord = DWORD And &HFFFF&
  249. End Function
  250. '**************************************************************
  251. ' Same as above, but returns the high order word.
  252. '**************************************************************
  253. Public Function GetHiWord(ByVal DWORD As Long) As Long
  254. Attribute GetHiWord.VB_Description = "Returns the high order word from a DWORD."
  255.     GetHiWord = DWORD \ &H10000
  256. End Function
  257. #If Win16 Then
  258. '**************************************************************
  259. ' This function is EXTREMELY useful under Win16 for making
  260. ' a DWORD which is sometimes required by SendMessage's lParam
  261. ' argument.
  262. '**************************************************************
  263. Public Function MakelParam(LoWord%, HiWord%) As Long
  264. Attribute MakelParam.VB_Description = "Takes 2 integers and converts them to a DWORD."
  265.     MakelParam = CLng(HiWord) * &H1000& Or LoWord
  266. End Function
  267. #End If
  268. '**************************************************************
  269. ' This method returns the Windows version as a variant so you
  270. ' can use it as text, or as a number.
  271. '**************************************************************
  272. Public Function WindowsVersion() As Variant
  273. Attribute WindowsVersion.VB_Description = "Returns the version of Windows that is currently running."
  274. Dim WinVer As Long
  275.     WinVer = GetLoWord(GetVersion())
  276.     WindowsVersion = Format((WinVer Mod 256) + ((WinVer \ 256) _
  277.                             / 100), "Fixed")
  278. End Function
  279. '**************************************************************
  280. ' This methods accepts alphanumeric settings to write to an
  281. ' INI file. In addition, you can delete a section or key by,
  282. ' passing the special "_DELETE_" string.
  283. '**************************************************************
  284. Public Sub WriteINI(ByVal Section$, ByVal Key$, ByVal Setting _
  285.                     As Variant, ByVal FileName$)
  286. Attribute WriteINI.VB_Description = "Writes an entry to an INI file. Use _DELETE_ in Key or Setting to delete Sections or Keys."
  287.     '**********************************************************
  288.     ' If key is set to _DELETE_, then delete the section
  289.     '**********************************************************
  290.     If Key = "_DELETE_" Then
  291.         WritePrivateProfileString Section, 0&, 0&, FileName
  292.     '**********************************************************
  293.     ' If setting is set to _DELETE_, then delete the key
  294.     '**********************************************************
  295.     ElseIf Setting = "_DELETE_" Then
  296.         WritePrivateProfileString Section, Key, 0&, FileName
  297.     '**********************************************************
  298.     ' Otherwise, convert the setting to a string and write it
  299.     ' to the INI file.
  300.     '**********************************************************
  301.     Else
  302.         WritePrivateProfileString Section, Key, CStr(Setting), _
  303.                                   FileName
  304.     End If
  305. End Sub
  306. #If Win32 Then
  307. '**************************************************************
  308. ' This method demonstrates how you can expose API calls. Since
  309. ' you can't use As Any with functions, SendMessage requires
  310. ' type-safe versions.
  311. '**************************************************************
  312. Public Function SendMessageAsLong(hwnd As Long, wMsg As _
  313.             Integer, wParam As Long, lParam As Long) As Long
  314.     SendMessageAsLong = Common.SendMessage(hwnd, wMsg, wParam, _
  315.                                           lParam)
  316. End Function
  317. '**************************************************************
  318. ' See above.
  319. '**************************************************************
  320. Public Function SendMessageAsStr(hwnd As Long, wMsg As _
  321.           Integer, wParam As Long, lParam As String) As Long
  322.     SendMessageAsStr = Common.SendMessage(hwnd, wMsg, wParam, _
  323.     lParam)
  324. End Function
  325. '**************************************************************
  326. ' See above.
  327. '**************************************************************
  328. Public Function PostMessage(ByVal hwnd As Long, ByVal wMsg _
  329.    As Integer, ByVal wParam As Long, lParam As Long) As Long
  330.     PostMessage = Common.PostMessage(ByVal hwnd, wMsg, wParam, _
  331.                                     lParam)
  332. End Function
  333. #Else
  334. '**************************************************************
  335. ' This method demonstrates how you can expose API calls. Since
  336. ' you can't use As Any with functions, SendMessage requires
  337. ' type-safe versions.
  338. '**************************************************************
  339. Public Function SendMessageAsLong(hwnd As Integer, wMsg As _
  340.             Integer, wParam As Integer, lParam As Long) As Long
  341.     SendMessageAsLong = Common.SendMessage(hwnd, wMsg, wParam, _
  342.                                           lParam)
  343. End Function
  344. '**************************************************************
  345. ' See above.
  346. '**************************************************************
  347. Public Function SendMessageAsStr(hwnd As Integer, wMsg As _
  348.           Integer, wParam As Integer, lParam As String) As Long
  349. Attribute SendMessageAsStr.VB_Description = "An exposed wrapper for the SendMessage API call. Use this function as described in the SDK."
  350.     SendMessageAsStr = Common.SendMessage(hwnd, wMsg, wParam, _
  351.     lParam)
  352. End Function
  353. '**************************************************************
  354. ' See above.
  355. '**************************************************************
  356. Public Function PostMessage(ByVal hwnd As Integer, ByVal wMsg _
  357.    As Integer, ByVal wParam As Integer, lParam As Long) As Long
  358.     PostMessage = Common.PostMessage(ByVal hwnd, wMsg, wParam, _
  359.                                     lParam)
  360. End Function
  361. #End If
  362.