Using the Win32 API

In this exercise, you will create a message box using both the Win32 API and the inherent Visual Basic function, MsgBox .

* To create a message box using the Win32 API:

  1. Start Microsoft Visual Basic and create a new standard EXE project.
  2. From the Project menu, select Add Module .
  3. In General Declarations , type the following code:
     Declare Function MessageBox Lib "user32" Alias "MessageBoxA"(ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long 
  4. Add a new command button control to Form1 .
  5. In the Click event of Command1 , type the following code:
     MessageBox Me.hwnd, "API MessageBox function", "API", 0 
  6. Run the application.
  7. Click Command1 .

    The MessageBox Win32 API function has been called directly from Visual Basic code.

* To create a message box in Visual Basic:

  1. Start Visual Basic and select Standard EXE .
  2. Add a command button control to Form1 .
  3. In the Click event of Command2 , enter the following code:
     MsgBox "Visual Basic MsgBox function" 
  4. From the Run menu, click Start to run the program.
  5. Click Command12 .

    Notice that a message box has been displayed. Visual Basic internally utilizes the Win32 API to call the MessageBox function in the User32.dll library.



Microsoft Windows Architecture Training
Microsoft Windows Architecture for Developers Training Kit
ISBN: B00007FY9D
EAN: N/A
Year: 1998
Pages: 324

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net