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
The MessageBox API(CHS:系统消息框API)

C# Signature:

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern MessageBoxResult MessageBox(IntPtr hWnd, String text, String caption, int options);

uint range:0~6

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.NET 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

C++ Signature:

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

User-Defined Types:

/// <summary>
/// Flags that define appearance and behaviour of a standard message box displayed by a call to the MessageBox function.
/// </summary>
[Flags]
public enum MessageBoxOptions : uint
{
    Ok         = 0x000000,
    OkCancel       = 0x000001,
    AbortRetryIgnore   = 0x000002,
    YesNoCancel    = 0x000003,
    YesNo          = 0x000004,
    RetryCancel    = 0x000005,
    CancelTryContinue  = 0x000006,

    IconHand       = 0x000010,
    IconQuestion       = 0x000020,
    IconExclamation    = 0x000030,
    IconAsterisk       = 0x000040,
    UserIcon       = 0x000080,

    IconWarning    = IconExclamation,
    IconError      = IconHand,
    IconInformation    = IconAsterisk,
    IconStop       = IconHand,

    DefButton1     = 0x000000,
    DefButton2     = 0x000100,
    DefButton3     = 0x000200,
    DefButton4     = 0x000300,

    ApplicationModal   = 0x000000,
    SystemModal    = 0x001000,
    TaskModal      = 0x002000,

    Help           = 0x004000, //Help Button
    NoFocus        = 0x008000,

    SetForeground      = 0x010000,
    DefaultDesktopOnly = 0x020000,
    Topmost        = 0x040000,
    Right          = 0x080000,
    RTLReading     = 0x100000,
}

/// <summary>
/// Represents possible values returned by the MessageBox function.
/// </summary>
public enum MessageBoxResult : uint
{
    Ok = 1,
    Cancel,
    Abort,
    Retry,
    Ignore,
    Yes,
    No,
    Close,
    Help,
    TryAgain,
    Continue,
    Timeout = 32000
}

Notes:

None.

Tips & Tricks:

Input something

Sample Code C#:

using System;
using System.Runtime.InteropServices;    

class Class1
{
    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    static extern int MessageBox(IntPtr hWnd, String text, String caption, int options);

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

Sample Code VB:

MessageBox(IntPtr.Zero, "Text", "Caption", MessageBoxOptions.Ok)

Sample Code C++:

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

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
Find References
Show Printable Version
Revisions