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 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:

MessageBoxOptions, MessageBoxResult

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

Imports System;
Imports System.Runtime.InteropServices;    

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    

    <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

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

save as file trial.c:

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

contibuted by John Refling

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