★ wanayoo — archive 1999 http://visualbasic.about.com/compute/visualbasic/library/weekly/aa101499.htmNouvelle recherche | Portail wanayoo
   You are here:    About.com > Computing/Technology > Visual Basic > Articles
spacer
shadow
shadow
spacer Home
Tue, Feb  29,  2000
spacer
graphic
Join Free
Your Guide James Limm - your About.com Guide to:
Visual Basic

Content: Welcome | Netlinks | Articles | Guide Bio | Search | Related
TalkAbout: Forums | Chat | Events | Newsletter | Share This Site | Join
Shopping: ShopNow | Books | Videos | Jobs | ShoppingAbout

5 Top API Tricks

Dateline: 10/14/99

Introduction

Visual Basic is great, and it allows really quite good programs to be constructed in less time than it takes you to say "Hello World". However, although Visual Basic is perfect for quickly creating simple applications, it lacks the built in power of other programming languages such as Visual C++. It is impossible, for example, to take advantage of many of the built in Windows dialog boxes, such as the file copy and move dialog boxes that appears whenever you manipulate large files in Explorer. This article will show you how to utilise the power of the Windows API to add special features to your application. Note: Before you set sail on the sea of API development, I recommend you peruse the excellent newbie API tutorial from "Special Edition Using Visual Basic 6".

Contents

Disabling Ctrl-Alt-Delete and Ctrl-Esc

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.

Declarations

Copy this code into the declarations section of a module.

Private Declare Function SystemParametersInfo Lib _
"user32" Alias "SystemParametersInfoA" (ByVal uAction _
As Long, ByVal uParam As Long, ByVal lpvParam As Any, _
ByVal fuWinIni As Long) As Long
Code
Sub DisableCtrlAltDelete(bDisabled As Boolean)
    Dim X As Long
    X = SystemParametersInfo(97, bDisabled, CStr(1), 0)
End Sub

Use

To disable Ctrl-Alt-Delete:

Call DisableCtrlAltDelete(True)

To enable Ctrl-Alt-Delete:

Call DisableCtrlAltDelete(False)

Using the Browse Folder Dialog Box

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.

Subscribe to The Visual Basic Newsletter
Name
Email

Previous Articles...

About.com 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!

  • About.com 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
  • About.com Related About.com GuideSites
  • C/C++
  • Computer Science
  • Delphi Programming
  • Focus on Java
  • Focus on JavaScript
  •  
  • Focus on Web3D
  • HTML
  • Pascal Programming
  • Perl
  • XML
  •  
    About.com Elsewhere on About.com
    What's New, What's Hot
  • Breaking News
  • Spay Day 2000
  • California Skiing Guide
  •   JustAbout
  • Quit Smoking
  • Healthy Living
  • Twisted Travel

  • search Search
      
    This Site About.com
    Share This Site With a Friend (enter their email address below):

       

    Top of Page

    spacer
    In Partnership With TechWeb
    TechWeb News
     TechWeb News
    Cyber Guru Favors Regulated Open Access

    Windows 2000 Aces Transaction Processing Test

    IBM Will Ship PCs With 256-Bit Encryption
    Top Downloads
     Free Downloads
     


    Marketplace

    ShoppingAbout

    Find a Friend BookStore Build a Web Page Finance Channel Send a card! Sweepstakes
    shadow
    shadow

    shadow
    shadow

    © 2000 About.com, Inc. All rights reserved.
    ©   2000 About.com, Inc. "MiningCo.com" and "About.com" are trademarks of About.com, Inc.,
    a publicly traded company listed on NASDAQ under the symbol "BOUT."
    For more information on About.com, visit Our Story, Be a Guide, Be an Advertiser, or Investor Relations.
    For rules of use, read our User Agreement; for privacy concerns, read our Privacy Policy.
    Having a Problem? Report it here.