Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

MessageBox (user32)
 
.
Summary
Summary
The MessageBox API(CHS:系统消息框API)

C# Signature:

[DllImport("user32.dll", SetLastError=true)]
static extern int MessageBoxTimeout(IntPtr hwnd, String text, String title, uint type, Int16 wLanguageId, Int32 milliseconds);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, int options);

uint range:0~6

C# Signature2:

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.U4)]
private static extern uint MessageBoxTimeout(IntPtr hwnd,
    [MarshalAs(UnmanagedType.LPTStr)]  String text,
    [MarshalAs(UnmanagedType.LPTStr)] String title,
    [MarshalAs(UnmanagedType.U4)] uint type,
    Int16 wLanguageId,
    Int32 milliseconds);

VB.NET Signature:

<DllImport("user32.dll", EntryPoint:="MessageBoxW", SetLastError:=True, Charset:=Charset.Unicode)> _
Public Function MessageBox(hwnd As IntPtr, _
      <MarshalAs(UnmanagedType.LPTSTR)> lpText As String, _
      <MarshalAs(UnmanagedType.LPTSTR)>lpCaption As String, _
      <MarshalAs(UnmanagedType.U4)>uType As MessageBoxOptions) As <MarshalAs(UnmanagedType.U4)>MessageBoxResult
End Function    

VB Signature:

Public Declare Function MessageBox Lib "user32.dll" Alias "MessageBoxA" _
         (ByVal prmlngWindowHandle As Long, _
          ByVal prmstrMessage As String, _
          ByVal prmstrCaption As String, _
          ByVal prmlngType As MessageBoxOptions) As MessageBoxResult

VB.Net Signature:

C++ Signature:

[DllImport("user32.DLL", EntryPoint="MessageBox", SetLastError=true, CharSet=CharSet::Auto, CallingConvention=CallingConvention::StdCall)]
int MessageBox(IntPtr hWnd, String^ Text, String^ Caption, MessageBoxOptions Options);

<DllImport("user32.dll", EntryPoint:="MessageBoxTimeoutA", setLastError:=True, CharSet:= CharSet.Unicode)> _

Private Shared Function MessageBoxTimeOut( _

    Byval hWnd as IntPtr, _
    <MarshalAs(UnmanagedType.LPStr)> ByVal lpText as StringBuilder, _
    <MarshalAs(UnmanagedType.LPStr)> ByVal lpCaption as StringBuilder, _
    <MarshalAs(UnmanagedType.U4)> ByVal uType as UInteger, _
    <MarshalAs(UnmanagedType.U2)> ByVal wLanguage as Int16, _
    <MarshalAs(UnmanagedType.U4)> ByVal dwMilliseconds as Int32, _
    ) as <MarshalAs(UnmanagedType.U4)> UInteger

End Function

VBA Signature:

Private Declare Function MessageBoxTimeOut _
     Lib "user32.dll" Alias "MessageBoxTimeoutA" _
     ( _
     Byval prmlngWindowHandle as Long, _
     Byval prmstrMessage as String, _
     Byval prmstrCaption as string, _
     Byval prmlngType as long, _
     Byval prmwLanguage as Integer, _
     Byval prmdwMiliseconds as Long _
     ) as Integer

User-Defined Types:

use MessageBoxButtons.* enumeration for type argument, cast as integer.

MessageBoxOptions, MessageBoxResult

Use enumeration for type aruments, cast as uint with Flags

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

Returns 32000 if the messagebox timed out.

None.

Tips & Tricks:

Input something

wLanguageID

The language for the text displayed in the message box button(s). Specifying a value of zero (0) indicates to display the button text in the default system language. If this parameter is MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), the current language associated with the calling thread is used.

To specify a language other than the current language, use the MAKELANGID macro to create this parameter. For more information,see http://msdn.microsoft.com/en-gb/library/windows/desktop/dd373908(v=vs.85).aspx

Sample Code C#:

using System;
using System.Runtime.InteropServices;    

dwMilliseconds

Parameter is in milliseconds so 1000 will be 1 second etc

Messagebox wont time out if dwMilliseconds is 0

Please add some!

Sample Code:

C# Example

private static uint SetupnCallMessageBoxTimeOut(IntPtr hWnd, string itsMessage, string itsCaption, uint itsMessageBoxOptions,Int32 itsTimeOutMilliSeconds)
class Class1
{
     return MessageBoxTimeout(hWnd , itsMessage, itsCaption,itsMessageBoxOptions,0,itsTimeOutMilliSeconds);
}
    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    static extern int MessageBox(IntPtr hWnd, String text, String caption, int options);

VB.Net Example

    [STAThread]
    static void Main(string[] args)
    {
        MessageBox(IntPtr.Zero, "Text", "Caption", 0);
    }
}

Sample Code VB.NET:

Imports System;
Imports System.Runtime.InteropServices;    

   private Shared Function DisplayMessageBox( _
       ByVal hWnd as IntPtr, _
       ByVal Message as string, _
       ByVal Caption as string, _
       ByVal MessageOptions as UInteger, _
       Byval TimeOutMilliSeconds as Int32 _
   ) as Uinteger
Class Class1
     <DllImport("user32.dll", EntryPoint:="MessageBoxW", SetLastError:=True, Charset:=Charset.Unicode)> _
     Public Function MessageBox(hwnd As IntPtr, _
           <MarshalAs(UnmanagedType.LPTSTR)> lpText As String, _
           <MarshalAs(UnmanagedType.LPTSTR)>lpCaption As String, _
           <MarshalAs(UnmanagedType.U4)>uType As MessageBoxOptions) As <MarshalAs(UnmanagedType.U4)>MessageBoxResult
     End Function    

   return MessageBoxTimeOut(hWnd, New StringBuilder(Message), New StringBuilder(Caption), MessageBoxOptions,0, TimeOutMilliSeconds)
    <STAThread> _
    Public Sub Main()
        MessageBox(IntPtr.Zero, "Text", "Caption", MessageBoxOptions.Ok or MessageBoxOptions.IconWarning)
    End Sub
End Class

Sample Code VB:

Public Declare Function MessageBox Lib "user32.dll" Alias "MessageBoxA" _
         (ByVal prmlngWindowHandle As Long, _
          ByVal prmstrMessage As String, _
          ByVal prmstrCaption As String, _
          ByVal prmlngType As MessageBoxOptions) As MessageBoxResult

   End Function      
MessageBox(0, "Text", "Caption", MessageBoxOptions.Ok or MessageBoxOptions.IconWarning)

Sample Code C++:

MessageBox(IntPtr::Zero, "Text", "Caption", MessageBoxOptions::OkCancel | MessageBoxOptions::IconWarning);

Sample code for tiny c compler ( http://bellard.org/tcc/ ):

// compile with: tcc trial.c -luser32
// reference: https://msdn.microsoft.com/en-us/library/windows/desktop/ms645505%28v=vs.85%29.aspx

VBA Example

save as file trial.c:

     Public Function DisplayMessageBox( _
    ByVal hWnd As Long, _
    ByVal Message As String, _
    ByVal Caption As String, _
    ByVal MessageBoxOptions As Long, _
    ByVal TimeOutMilliseconds As Long _
     ) As Integer

    DisplayMessageBox = MessageBoxTimeOut(hWnd, Message, Caption, MessageBoxOptions, 0, TimeOutMilliseconds)

     End Function
#include <windows.h>
int main() {
    MessageBox(NULL, "Text", "Caption", 0);
    return 0;
}

binary is 1536 bytes!

Please add some!

Contibuted by John Refling

Documentation

Alternative Managed API:

System.Windows.Forms.MessageBox.Show

Documentation
MessageBox on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions