Visual Basic Code Snippets #8
In this next update I'll continue to re-post my Visual Basic code snippets (which are older than five years by the way) that pertain to System Functions.
Ensure Application Can't Load Multiple Times
Compatibility: Win. 98-XP
This isn't a system function but is definitely essential to any application.
Add the following code to the Form_Load() event...
If App.PrevInstance Then
MsgBox ("The application is already open."), vbExclamation, "The requested " & "application is already open"
Unload Me
End If
Display Internet Properties Dialog
Compatibility: Win. 98-XP
Add the following code into a module within your project...
Public Function ShowInetProperties() As Boolean
Dim lRet As Long
lRet = Shell("rundll32.exe shell32.dll,Control_RunDLL inetcpl.cpl,,0", vbNormalFocus)
ShowInetProperties = lRet > 0
End Function
Add the following code where you want the code to execute...
Call ShowInetProperties()
Toggle CapsLock Key On/Off
Compatibility: Win. 98-XP
Add the following code into a module within your project...
Public Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
Public Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Public Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
Public Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long
Public Const VER_PLATFORM_WIN32_NT = 2
Public Const VER_PLATFORM_WIN32_WINDOWS = 1
Public Const VK_CAPITAL = &H14
Public Const KEYEVENTF_EXTENDEDKEY = &H1
Public Const KEYEVENTF_KEYUP = &H2
Public Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
Public Sub ToggleCapsLock(TurnOn As Boolean)
'To turn capslock on, set turnon to true
'To turn capslock off, set turnon to false
Dim bytKeys(255) As Byte
Dim bCapsLockOn As Boolean
'Get status of the 256 virtual keys
GetKeyboardState bytKeys(0)
bCapsLockOn = bytKeys(VK_CAPITAL)
Dim typOS As OSVERSIONINFO
If bCapsLockOn <> TurnOn Then
If typOS.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then
bytKeys(VK_CAPITAL) = 1
SetKeyboardState bytKeys(0)
Else
'Simulate Key Press
keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
'Simulate Key Release
keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
End If
End If
End Sub
Add the following code where you want the code to execute...
'To turn capslock on...
Call ToggleCapsLock(True)
'To turn capslock off...
Call ToggleCapsLock(False)