It is often useful in Visual Basic programs to be able to disable the Ctrl-Alt-Delete key sequence. It is easily achieved by persuading Windows that a screen saver is running. This code also disables Ctrl-Esc, that is used to activate the Start Menu.
Copy this code into the declarations section of a module.
If your program requires the user to type in a directory path, why not harness to the power of the API to display a delightful dialog box to let your users select a directory from the drive without typing?
You can implement this dialog bow into your applications very easily by using the following API calls.
SHBrowseForFolder
SHGetPathFromIDList
lstrcat
Demo Project
Start a new Visual Basic standard-EXE project and add a command button.
Declarations
Copy this code into the declarations section of a module.
Private Const BIF_RETURNONLYFSDIRS = 1
Private Const BIF_DONTGOBELOWDOMAIN = 2
Private Const MAX_PATH = 260
Private Declare Function SHBrowseForFolder Lib _
"shell32" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib _
"shell32" (ByVal pidList As Long, ByVal lpBuffer _
As String) As Long
Private Declare Function lstrcat Lib "kernel32" _
Alias "lstrcatA" (ByVal lpString1 As String, ByVal _
lpString2 As String) As Long
Private Type BrowseInfo
hWndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Command Button Code
Add this code into the click event of you command button:
'Opens a Browse Folders Dialog Box that displays the
'directories in your computer
Dim lpIDList As Long ' Declare Varibles
Dim sBuffer As String
Dim szTitle As String
Dim tBrowseInfo As BrowseInfo
szTitle = "Hello World. Click on a directory and " & _
"it's path will be displayed in a message box"
' Text to appear in the the gray area under the
' title bar telling you what to do
With tBrowseInfo
.hWndOwner = Me.hWnd ' Owner Form
.lpszTitle = lstrcat(szTitle, "")
.ulFlags = BIF_RETURNONLYFSDIRS + _
BIF_DONTGOBELOWDOMAIN
End With
lpIDList = SHBrowseForFolder(tBrowseInfo)
If (lpIDList) Then
sBuffer = Space(MAX_PATH)
SHGetPathFromIDList lpIDList, sBuffer
sBuffer = Left(sBuffer, InStr(sBuffer, _
vbNullChar) - 1)
MsgBox sBuffer
End If
Run the program, and click on the command button. The browse for folder dialog will be displayed. Click on a directory and click on OK and the path you have selected will be shown in a message box.
Showing the Properties dialog box
This tip demonstrates how to use the Win32 API to bring up the Explorer properties dialog box for a specified file. This API function has the same effect as right clicking on a file in Windows 95 and selecting properties.
Declarations
Copy this code into the declarations section of a module.
Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type
Public Const SEE_MASK_INVOKEIDLIST = &HC
Public Const SEE_MASK_NOCLOSEPROCESS = &H40
Public Const SEE_MASK_FLAG_NO_UI = &H400
Declare Function ShellExecuteEX Lib "shell32.dll" Alias _
"ShellExecuteEx" (SEI As SHELLEXECUTEINFO) As Long
Code
Public Sub ShowProps(FileName As String, OwnerhWnd _
As Long)
Dim SEI As SHELLEXECUTEINFO
Dim r As Long
With SEI
.cbSize = Len(SEI)
.fMask = SEE_MASK_NOCLOSEPROCESS Or _
SEE_MASK_INVOKEIDLIST Or SEE_MASK_FLAG_NO_UI
.hwnd = OwnerhWnd
.lpVerb = "properties"
.lpFile = FileName
.lpParameters = vbNullChar
.lpDirectory = vbNullChar
.nShow = 0
.hInstApp = 0
.lpIDList = 0
End With
r = ShellExecuteEX(SEI)
End Sub
Use
To show the properties dialog box of "c:\autoexec.bat", use the following code:
Call ShowProps("c:\autoexec.bat", Me.hwnd)
Creating a HotKey for your application
Here's an example of how to create a hotkey for an app!
Declarations
Copy this code into the declarations section of a module.
Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Long) As Long
Declare Function DefWindowProc Lib "user32" _
Alias "DefWindowProcA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
Public Const WM_SETHOTKEY = &H32
Public Const WM_SHOWWINDOW = &H18
Public Const HK_SHIFTA = &H141 'Shift + A
Public Const HK_SHIFTB = &H142 'Shift * B
Public Const HK_CONTROLA = &H241 'Control + A
Public Const HK_ALTZ = &H45A
'The value of the key-combination has to
'declared in lowbyte/highbyte-format
'That means as a hex-number: the last two
'characters specify the lowbyte (e.g.: 41 = a),
'the first the highbyte (e.g.: 01 = 1 = Shift)
Form Code
Add the following code into the Form Load event
Me.WindowState = vbMinimized
'Let windows know what hotkey you want for
'your app, setting of lParam has no effect
erg& = SendMessage(Me.hwnd, WM_SETHOTKEY, _
HK_ALTZ, 0)
'Check if succesfull
If erg& <> 1 Then
MsgBox "You need another hotkey", vbOKOnly, _
"Error"
End If
'Tell windows what it should do, when the hotkey
'is pressed -> show the window!
'The setting of wParam and lParam has no effect
erg& = DefWindowProc(Me.hwnd, WM_SHOWWINDOW, _
0, 0)
When the user presses ALT+Z Form1 is shown.
Shutting down Windows
With one call the the API, you can persuade Windows to shutdown or restart. This code is perfect for installation routines that change system files, and so require a complete reboot to be correctly changed.
Declarations
Copy this code into the declarations section of a module.
Public Const EWX_LOGOFF = 0
Public Const EWX_SHUTDOWN = 1
Public Const EWX_REBOOT = 2
Public Const EWX_FORCE = 4
Declare Function ExitWindowsEx Lib "user32" Alias _
"ExitWindowsEx" (ByVal uFlags As Long, ByVal dwReserved _
As Long) As Long
If you want to reboot the computer use the following code:
t& = ExitWindowsEx(EWX_REBOOT, 0)
Conclusion
There are thousands of features that you can add to your application by using the API. For more information, check out the selection of advanced and intermediate tutorials.
Previous Articles...
More From Your Guide
|
Forums: Post your Visual Basic questions in our Q and A message board for free
Free Newsletter: Receive a weekly email newsletter from your Visual Basic Guide
NetLinks: Surf the library of over 500 links to the best Visual Basic resources
More Articles: Read more articles from your Visual Basic guide
Join this site: Become a member of the About.com Visual Basic site
Live Chat: Come on in and chat with other VB developers live!
|
In The Spotlight
|
Email Enable your App: Make your own Email Client
Learn Visual Basic: Resources for the newbie coder
Loud Coding: Add sound effects into your apps
Surf's Up: Create your own web browser
Make Quake: Create fantastic games with DirectX and VB
Y2K and VB: Prevent millennial meltdown in your programs
Write your own Word: Program your own text editor
|