MessageBoxIndirect Function

Declare Function MessageBoxIndirect Lib "user32.dll" Alias "MessageBoxIndirectA" (lpMsgBoxParams As MSGBOXPARAMS) As Long

Platforms

Description & Usage

MessageBoxIndirect opens and operates a small message box on the screen. Message boxes are typically used either to communicate important information (such as an error message) or to prompt the user. This function offers more options for the creation of the message box than MessageBox and MessageBoxEx do because this function stores those options in a structure passed to the function. The message box only closes when the user presses one of the buttons presented.

Return Value

If an error occured, the function returns 0 (use GetLastError to get the error code). If successful, the function returns one of the following flags specifying the button the user clicked on:

IDABORT
The Abort button was clicked.
IDCANCEL
The Cancel button was clicked (or the user dismissed the message box using the Esc key).
IDCONTINUE
Windows 2000: The Continue button was clicked.
IDIGNORE
The Ignore button was clicked.
IDNO
The No button was clicked.
IDOK
The OK button was clicked.
IDRETRY
The Retry button was clicked.
IDTRYAGAIN
Windows 2000: The Try Again button was clicked.
IDYES
The Yes button was clicked.

Visual Basic-Specific Issues

None.

Parameters

lpMsgBoxParams
The settings used to create and operate the message box.

Constant Definitions

Const IDABORT = 3
Const IDCANCEL = 2
Const IDCONTINUE = 5
Const IDIGNORE = 5
Const IDNO = 7
Const IDOK = 1
Const IDRETRY = 4
Const IDTRYAGAIN = 4
Const IDYES = 6

Example

' This code is licensed according to the terms and conditions listed here.

' Create a Yes/No dialog for the user.  Also offer a Help
' button which opens the corresponding topic in a WinHelp file.
' The message box is owned by window Form1.  Pay attention
' to where various parts of the example code must be placed.

' *** Place the following code in a module. ***
' This callback function handles the message box's Help button.
Public Sub MsgBoxCallback (lpHelpInfo As HELPINFO)
  Dim retval As Long  ' return value

  ' Open the proper topic in the help file "C:\MyProg\proghelp.hlp".
  ' The desired Context ID is found inside the structure.
  retval = WinHelp(Form1.hWnd, "C:\MyProg\proghelp.hlp", HELP_CONTEXT, lpHelpInfo.dwContextId)
End Sub

' This is a dummy function, merely returning what is passed to it.  This
' is needed because the AddressOf operator is only valid inside
' a function call, whereas we need to use it to set a data member.
Public Function DummyFunc (ByVal param As Long) As Long
  DummyFunc = param
End Function

' *** Place the following code where you want to create the message box. ***
Dim mbp As MSGBOXPARAMS  ' message box's parameters
Dim mbresult As Long  ' result of message box

' Fill in the parameter structure for the message box.
With mbp
  .cbSize = Len(mbp)  ' size of structure
  .hwndOwner = Form1.hWnd  ' window which owns the message box
  .hInstance = App.hInstance  ' handle to instance of this program
  .lpszText = "Are you sure you want to do that?"  ' message box text
  .lpszCaption = "User Prompt"  ' message box's title bar text
  .dwStyle = MB_YESNO Or MB_HELP Or MB_ICONQUESTION  ' flags for display
  .lpszIcon = 0  ' not needed
  .dwContextHelpId = 2300  ' Context ID for the proper topic in the Help file
  .lpfnMsgBoxCallback = DummyFunc(AddressOf MsgBoxCallback)  ' pointer to calback function
  .dwLanguageId = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)  ' use American English
End With

' Display the message box.
mbresult = MessageBoxIndirect(mbp)
' Determine what the user pressed.
If mbresult = IDYES Then
  Debug.Print "OK, go ahead and do that."
Elseif mbresult = IDNO Then
  Debug.Print "Operation aborted."
End If

See Also

MessageBox, MessageBoxEx

Category

Dialog Boxes

Back to the Function list.
Back to the Reference section.


Last Modified: February 11, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/m/messageboxindirect.html