'****************************************************
'* COMMON01.BAS Version 1.4 Date: 12/01/94 *
'* VB Tips & Tricks *
'* 8430-D Summerdale Road San Diego CA 92126-5415 *
'* Compuserve: 74227,1557 *
'* America On-Line: DPMCS *
'****************************************************
Declare Function WritePrivateProfileString Lib "Kernel" (ByVal AppName As String, ByVal KeyName As String, ByVal NewString As String, ByVal filename As String) As Integer
Declare Function GetPrivateProfilestring Lib "Kernel" (ByVal AppName As String, ByVal KeyName As String, ByVal default As String, ByVal ReturnedString As String, ByVal MAXSIZE As Integer, ByVal filename As String) As Integer
Declare Function GetKeyState Lib "User" (ByVal NVirtKey%) As Integer
Declare Function FindWindow Lib "User" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Integer
Declare Sub ShowWindow Lib "User" (ByVal hWnd As Integer, ByVal nCmdShow As Integer)
Declare Sub SFocus Lib "User" Alias "SetFocus" (ByVal hWnd As Integer)
Declare Sub SetWindowPos Lib "User" (ByVal hWnd As Integer, ByVal hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer)
Global hWnd As Integer
Const SWP_NOMOVE = 2
Const SWP_NOSIZE = 1
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Declare Function GetModuleUsage Lib "Kernel" (ByVal hModule As Integer) As Integer
Declare Function WinExec Lib "Kernel" (ByVal lpCmdLine As String, ByVal nCmdShow As Integer) As Integer
'CTRL3D DECLARE
Declare Function GetModuleHandle Lib "Kernel" (ByVal ModuleName As String) As Integer
Declare Function Ctl3dAutoSubclass Lib "Ctl3D.DLL" (ByVal hInst As Integer) As Integer
Declare Function Ctl3dRegister Lib "Ctl3D.DLL" (ByVal hInst As Integer) As Integer
Declare Function Ctl3dUnregister Lib "Ctl3D.DLL" (ByVal hInst As Integer) As Integer
'*******************************************************
'* Procedure Name: AppRunning *
'*-----------------------------------------------------*
'* Created: 2/8/94 By: MSDN *
'* Modified: By: *
'*=====================================================*
'*Checks to see if the current application is already *
'*running. To use just call the sub. If the application*
'*is already running, it will end the current *
'*application. *
'* *
'*******************************************************
Sub AppRunning()
Dim sMsg As String
If App.PrevInstance Then
sMsg = App.EXEName & " already running! "
MsgBox sMsg, 4112
End
End If
End Sub
'*******************************************************
'* Procedure Name: CenterForm *
'*-----------------------------------------------------*
'* Created: 2/10/94 By: VB Programmers Journal *
'* Modified: 4/24/94 By: David McCarter *
'*=====================================================*
'*This code will center a form in the center of the *
'*screen. To use it, just call the sub and pass it the *
'*form name [Call CenterForm main] *
'* *
'* *
'*******************************************************
Sub CenterForm(frmIN As Form)
Dim iTop As Integer, iLeft As Integer
If frmIN.WindowState <> 0 Then Exit Sub
iTop = (Screen.Height - frmIN.Height) \ 2
iLeft = (Screen.Width - frmIN.Width) \ 2
'If iTop And iLeft Then
frmIN.Move iLeft, iTop
'End If
End Sub
'*******************************************************
'* Procedure Name: CenterMDIChild *
'*-----------------------------------------------------*
'* Created: 2/10/94 By: VB Programmers Journal *
'* Modified: 3/24/94 By: D. McCarter *
'*=====================================================*
'* Centers a child form within a parent MDI form. To *
'* use, call the sub and pass it the parent form name *
'* and the child form name [CenterMDIChild form1 form2]*
'* *
'* *
'*******************************************************
Sub CenterMDIChild(frmParent As Form, frmChild As Form)
Dim iTop As Integer, iLeft As Integer
If frmParent.WindowState <> 0 Or frmChild.WindowState <> 0 Then Exit Sub
iTop = (frmParent.ScaleHeight - frmChild.Height) \ 2
iLeft = (frmParent.ScaleWidth - frmChild.Width) \ 2
If iTop And iLeft Then
frmChild.Move iLeft, iTop
End If
End Sub
'*******************************************************
'* Procedure Name: CheckUnique *
'*-----------------------------------------------------*
'* Created: 4/18/94 By: Terry Brooking *
'* Modified: By: *
'*=====================================================*
'*Checks for previous instance of application, If found*
'*ensures it's restored from Icon and Focused on! It is*
'*up to main routine to end application if required. *
'* *
'* *
'*******************************************************
Function CheckUnique(F As Form) As Integer
'Check for previous instance of application. If found, ensures
'is restored from Icon and Focused on! It is up to Main routine
'to end application if required.
Dim X As Integer
Dim Title As String
CheckUnique = True
If App.PrevInstance Then
'MsgBox "Prev Instance found!"
Title = F.Caption
F.Caption = Title & " - New" 'This is necessary as you may find yourself!
hWnd = FindWindow(0&, Title)
F.Caption = Title 'Restore caption
DoEvents
If hWnd Then
'MsgBox "Handle found!"
ShowWindow hWnd, 1 'Restores from Minimsed if necessary
DoEvents
SFocus hWnd 'Sets focus
DoEvents
CheckUnique = False
End If
End If
End Function
'*******************************************************
'* Procedure Name: CutCopyPaste *
'*-----------------------------------------------------*
'* Created: By: VB Help File *
'* Modified: By: *
'*=====================================================*
'*This procedure puts all the cut,copy paste commands *
'*in one place. To use, just call the sub and pass it *
'*your choice- 0=Cut, 1=Copy, 2=Paste, 3=Delete, *
'*[Call CutCopyPaste 2] *
'* *
'*******************************************************
Sub CutCopyPaste(iChoice As Integer)
' ActiveForm refers to the active form in the MDI form.
If TypeOf Screen.ActiveControl Is TextBox Then
Select Case iChoice
Case 0 ' Cut.
' Copy selected text to Clipboard.
Clipboard.SetText Screen.ActiveControl.SelText
' Delete selected text.
Screen.ActiveControl.SelText = ""
Case 1 ' Copy.
' Copy selected text to Clipboard.
Clipboard.SetText Screen.ActiveControl.SelText
Case 2 ' Paste.
' Put Clipboard text in text box.
Screen.ActiveControl.SelText = Clipboard.GetText()
Case 3 ' Delete.
' Delete selected text.
Screen.ActiveControl.SelText = ""
End Select
End If
End Sub
'*******************************************************
'* Procedure Name: DOSSHELL *
'*-----------------------------------------------------*
'* Created: 8/13/94 By: David McCarter *
'* Modified: By: *
'*=====================================================*
'*Executes a Shell function and does not return control*
'*to your program until the shell is finished. The *
'*Shell string should contain all info for the shell *
'*including path and file name. WinType should be one *
'*of these values: *
'*Normal Window=1 *
'*Maximized Window=3 *
'*Minimized Window=6 *
'*Hidden Window=0 *
'*******************************************************
Sub DOSShell(ShellString As String, WinType As Integer)
Dim InstanceHandle As Integer, X As Integer
InstanceHandle = Shell(ShellString, WinType)
Do While GetModuleUsage(InstanceHandle) > 0
X = DoEvents()
Loop
End Sub
'*******************************************************
'* Procedure Name: FileExists *
'*-----------------------------------------------------*
'* Created: 8/29/94 By: David McCarter *
'* Modified: By: *
'*=====================================================*
'*This function will check to make sure that a file *
'*exists.It will return True if the file was found and *
'*False if it was not found. *
'*Example: If Not FileExists("autoexec.bat") Then... *
'*******************************************************
Function FileExists(filename As String) As Integer
Dim i As Integer
On Error Resume Next
i = Len(Dir$(filename))
If Err Or i = 0 Then
FileExists = False
Else
FileExists = True
End If
End Function
'*******************************************************
'* Procedure Name: FixAmper *
'*-----------------------------------------------------*
'* Created: By: Bart Larsen *
'* Modified: By: *
'*=====================================================*
'*This function fixes strings that are to be used by a *
'*Label control so the "&" (Chr(38)) does not *
'*underscore the following character. *
'*[Label1.Caption=FixAmper("bf&ftr.zip")] *
'* *
'*******************************************************
Function FixAmper(Strng As String) As String
Dim sTemp As String, n As Integer
While InStr(Strng, "&")
n = InStr(Strng, "&")
sTemp = sTemp + Left$(Strng, n) + "&"
Strng = Mid$(Strng, n + 1)
Wend
sTemp = sTemp + Strng
FixAmper = sTemp
End Function
'*******************************************************
'* Procedure Name: GetAppPath *
'*-----------------------------------------------------*
'* Created: 3/24/94 By: David McCarter *
'* Modified: By: *
'*=====================================================*
'*Returns the application path with a trailing \. *
'*To use, call the function [SomeString=GetAppPath()] *
'* *
'* *
'* *
'*******************************************************
Function GetAPPPath() As String
Dim sTemp As String
sTemp = App.Path
If Right$(sTemp, 1) <> "\" Then sTemp = sTemp + "\"
GetAPPPath = sTemp
End Function
'*******************************************************
'* Procedure Name: CheckUnique *
'*-----------------------------------------------------*
'* Created: 4/18/94 By: KeepOnTop *
'* Modified: By: *
'*=====================================================*
'*Keep form on top. Note that this is switched off if *
'*form is minimised, so place in resize event as well. *
'* *
'* *
'* *
'*******************************************************
Sub KeepOnTop(F As Form)
'Keep form on top. Note that this is switched off if form is
'minimised, so place in resize event as well.
Const wFlags = SWP_NOMOVE Or SWP_NOSIZE
SetWindowPos F.hWnd, HWND_TOPMOST, 0, 0, 0, 0, wFlags 'Window will stay on top
'To undo call again with HWND_NOTOPMOST
DoEvents
End Sub
'*******************************************************
'* Procedure Name: LongDirFix *
'*-----------------------------------------------------*
'* Created: 6/30/94 By: David McCarter *
'* Modified: By: *
'*=====================================================*
'*This function will shorten a directory name to the *
'*length passed to it. *
'*Usage: Label1.Caption=LongDirFix(sDirName, 32) *
'*The second paramater the the max length of the *
'*returned string. *
'*******************************************************
Function LongDirFix(Incomming As String, Max As Integer) As String
Dim i As Integer, LblLen As Integer, StringLen As Integer
Dim TempString As String
TempString = Incomming
LblLen = Max
If Len(TempString) <= LblLen Then
LongDirFix = TempString
Exit Function
End If
LblLen = LblLen - 6
For i = Len(TempString) - LblLen To Len(TempString)
If Mid$(TempString, i, 1) = "\" Then Exit For
Next
LongDirFix = Left$(TempString, 3) + "..." + Right$(TempString, Len(TempString) - (i - 1))
End Function
'*******************************************************
'* Procedure Name: MakeDir *
'*-----------------------------------------------------*
'* Created: 8/29/94 By: David McCarter *
'* Modified: By: *
'*=====================================================*
'*This function will create a directory even if the *
'*underlying directories do not exist. *
'*Usage: MakeDir "c:\temp\demo" *
'*This procedue also uses the ValDir to find if the *
'*directory already exists. *
'*******************************************************
Sub MakeDir(sDirName As String)
Dim iMouseState As Integer
Dim iNewLen As Integer
Dim iDirLen As Integer
'Get Mouse State
iMouseState = Screen.MousePointer
'Change Mouse To Hour Glass
Screen.MousePointer = 11
'Set Start Length To Search For [\]
iNewLen = 4
'Add [\] To Directory Name If Not There
If Right$(sDirName, 1) <> "\" Then sDirName = sDirName + "\"
'Create Nested Directory
While Not ValDir(sDirName)
iDirLen = InStr(iNewLen, sDirName, "\")
Debug.Print Left$(sDirName, iDirLen)
If Not ValDir(Left$(sDirName, iDirLen)) Then
MkDir Left$(sDirName, iDirLen - 1)
End If
iNewLen = iDirLen + 1
Wend
'Leave The Mouse The Way You Found It
Screen.MousePointer = iMouseState
End Sub
'*******************************************************
'* Procedure Name: ReadINI *
'*-----------------------------------------------------*
'* Created: By: Daniel Bowen *
'* Modified: 3/24/94 By: David McCarter *
'*=====================================================*
'*Returns a string from an INI file. To use, call the *
'*functions and pass it the AppName, KeyName and INI *
'*File Name, [sReg=ReadINI(App1,Key1,INIFile)]. If you *
'*need the returned value to be a integer then use the *
'*val command. *
'*******************************************************
Function ReadINI(AppName, KeyName, filename As String) As String
Dim sRet As String
sRet = String(255, Chr(0))
ReadINI = Left(sRet, GetPrivateProfilestring(AppName, ByVal KeyName, "", sRet, Len(sRet), filename))
End Function
'*******************************************************
'* Procedure Name: SelectText *
'*-----------------------------------------------------*
'* Created: 2/14/94 By: David McCarter *
'* Modified: By: *
'*=====================================================*
'*Selects all the text in a text box. Call it when the *
'*text box get focus, [SelectText Text1.text] *
'* *
'* *
'* *
'*******************************************************
Sub SelectText(ctrIn As Control)
ctrIn.SelStart = 0
ctrIn.SelLength = Len(ctrIn.Text)
End Sub
'*******************************************************
'* Procedure Name: Turn3DOff *
'*-----------------------------------------------------*
'* Created: 11/12/94 By: David McCarter *
'* Modified: By: *
'*=====================================================*
'* This function turns off the 3D effect for VB *
'* message boxes turn on by the Turn3DOn function. You *
'* MUST makes sure this function is called before the *
'* program ends! *
'* *
'*******************************************************
Sub Turn3DOff()
Dim INST As Integer
Dim RET As Integer
INST = GetModuleHandle(App.EXEName)
RET = Ctl3dUnregister(INST)
End Sub
'*******************************************************
'* Procedure Name: Turn3DOn *
'*-----------------------------------------------------*
'* Created: 11/12/94 By: David McCarter *
'* Modified: By: *
'*=====================================================*
'* Use this function to turn on a 3D effect on all *
'* Windows message and dialog boxes your program uses. *
'* You must call the Turn3DOff function before the *
'* program ends or VBT (very bad things) will happen! *
'* *
'*******************************************************
Sub Turn3DOn()
Dim INST As Integer
Dim RET As Integer
INST = GetModuleHandle(App.EXEName)
RET = Ctl3dRegister(INST)
RET = Ctl3dAutoSubclass(INST)
End Sub
'*******************************************************
'* Procedure Name: ValDir *
'*-----------------------------------------------------*
'* Created: 8/29/94 By: David McCarter *
'* Modified: By: *
'*=====================================================*
'*This function is used by MakeDir to validate if a *
'*directory already exists. *
'*******************************************************
Function ValDir(sIncoming As String) As Integer
Dim iCheck As String, iErrResult As Integer
On Local Error GoTo ValDirError
'If Right$(sIncoming, 1) <> "\" Then sIncoming = sIncoming + "\"
iCheck = Dir$(sIncoming)
If iErrResult = 76 Then
ValDir = False
Else
ValDir = True
End If
Exit Function
ValDirError:
Select Case Err
Case Is = 76
iErrResult = Err
Resume Next
Case Else
End Select
End Function
'*******************************************************
'* Procedure Name: WriteINI *
'*-----------------------------------------------------*
'* Created: 2/10/94 By: David McCarter *
'* Modified: By: *
'*=====================================================*
'*Writes a string to an INI file. To use, call the *
'*function and pass it the AppName, KeyName, the New *
'*String and the INI File Name, *
'*[R=WriteINI(App1,Key1,sReg,INIFile)]. Returns a 1 if *
'*there were no errors and a 0 if there were errors. *
'*******************************************************
Function WriteINI(AppName, KeyName, NewString, filename As String) As Integer
WriteINI = WritePrivateProfileString(AppName, KeyName, NewString, filename)
End Function