[C#] MessageBox Notification In Console Application

AceInfinity

Emeritus, Contributor
Joined
Feb 21, 2012
Posts
1,728
Location
Canada
I find that sometimes it is a bit more 'visible' to use a MessageBox in a console application sometimes. However, including the System.Windows.Forms namespace as a reference can be a bit too bulky as it comes with lots of other things as well that we probably don't need.

So the way to do this would be to P/Invoke the Win32 API function, MessageBox.

Just thought I'd post my quick dummy setup for some MessageBox utility here:

MessageBoxUtils Namespace:
Code:
namespace MessageBoxUtils
{
	public abstract class Notification
	{
		[DllImport("User32.dll")]
		public static extern int MessageBox(IntPtr h, string m, string c, MessageBoxOptions type);

		// MessageBox Dialog Result Constants
		const int ID_ABORT = 3;
		const int ID_CANCEL = 2;
		const int ID_CONTINUE = 11;
		const int ID_IGNORE = 5;
		const int ID_NO = 7;
		const int ID_OK = 1;
		const int ID_RETRY = 4;
		const int ID_TRYAGAIN = 10;
		const int ID_YES = 6;
	}

	[Flags]
	public enum MessageBoxOptions : uint
	{
		// Buttons
		MB_ABORTRETRYIGNORE = 0x00000002,
		MB_CANCELTRYCONTINUE = 0x00000006,
		MB_HELP = 0x00004000,
		MB_OK = 0x00000000,
		MB_OKCANCEL = 0x00000001,
		MB_RETRYCANCEL = 0x00000005,
		MB_YESNO = 0x00000004,
		MB_YESNOCANCEL = 0x00000003,

		// Icons
		MB_ICONEXCLAMATION = 0x00000030,
		MB_ICONWARNING = 0x00000030,
		MB_ICONINFORMATION = 0x00000040,
		MB_ICONASTERISK = 0x00000040,
		MB_ICONQUESTION = 0x00000020,
		MB_ICONSTOP = 0x00000010,
		MB_ICONERROR = 0x00000010,
		MB_ICONHAND = 0x00000010,

		// Default Buttons
		MB_DEFBUTTON1 = 0x00000000,
		MB_DEFBUTTON2 = 0x00000100,
		MB_DEFBUTTON3 = 0x00000200,
		MB_DEFBUTTON4 = 0x00000300,

		// Modality
		MB_APPLMODAL = 0x00000000,
		MB_SYSTEMMODAL = 0x00001000,
		MB_TASKMODAL = 0x00002000,

		// Other Options
		MB_DEFAULT_DESKTOP_ONLY = 0x00020000,
		MB_RIGHT = 0x00080000,
		MB_RTLREADING = 0x00100000,
		MB_SETFOREGROUND = 0x00010000,
		MB_TOPMOST = 0x00040000,
		MB_SERVICE_NOTIFICATION = 0x00200000
	}
}

Usage:
Code:
static void MainMethod()
{
	MessageBoxOptions options = MessageBoxOptions.MB_OK | MessageBoxOptions.MB_ICONEXCLAMATION | MessageBoxOptions.MB_APPLMODAL;
	string[] allOptions = Enum.Format(typeof(MessageBoxOptions), options, "G").Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);

	Console.WriteLine("MessageBox Dialog Result: {0}", 
		Notification.MessageBox(
			Process.GetCurrentProcess().MainWindowHandle,
			string.Join("\n", allOptions),
			"MessageBox Test", 
			options
		));
}

You'll have to provide:
Code:
using MessageBoxUtils;

In a line of code if we're in a different namespace here, if you don't want to be explicit at defining the entire thing, every time you use something from this namespace.

This is as basic as I could make it, but it comes in handy.
 
Last edited:
A bit more refined... I actually shouldn't have been exposing that direct extern function for the user to use directly, so this time i've created an overloaded method as a wrapper for it.

Code:
namespace MessageBoxUtils
{
    internal sealed class NativeMethods
    {
        [DllImport("User32.dll")]
        public static extern int MessageBox(IntPtr h, string m, string c, uint type);
    }

    public abstract class Notification
    {
        // Methods
        public static NotificationResult Show(string message)
        {
            return Show(message, string.Empty);
        }

        public static NotificationResult Show(string message, string caption)
        {
            return Show(message, caption, MessageBoxButtons.MB_OK);
        }

        public static NotificationResult Show(string message, string caption, MessageBoxButtons options)
        {
            return Show(message, caption, options, MessageBoxIcon.None);
        }

        public static NotificationResult Show(string message, string caption, MessageBoxButtons options, MessageBoxIcon icon)
        {
            return Show(message, caption, options, icon, MessageBoxDefaultButton.MB_DEFBUTTON1);
        }

        public static NotificationResult Show(string message, string caption, MessageBoxButtons options, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
        {
            return Show(message, caption, options, icon, defaultButton, MessageBoxModality.MB_APPLMODAL);
        }

        public static NotificationResult Show(string message, string caption, MessageBoxButtons options, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxModality modality)
        {
            return Show(message, caption, options, icon, defaultButton, modality, MessageBoxOptions.None);
        }

        public static NotificationResult Show(string message, string caption, MessageBoxButtons options, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxModality modality, MessageBoxOptions otherOptions)
        {
            return (NotificationResult)NativeMethods.MessageBox(Process.GetCurrentProcess().MainWindowHandle, message, caption, (uint)options + (uint)icon + (uint)defaultButton + (uint)modality + (uint)otherOptions);
        }
    }

    // Defined Types
    public enum NotificationResult
    {
        // MessageBox Notification Result Constants
        ID_ABORT =                0x3,
        ID_CANCEL =               0x2,
        ID_CONTINUE =             0xB,
        ID_IGNORE =               0x5,
        ID_NO =                   0x7,
        ID_OK =                   0x1,
        ID_RETRY =                0x4,
        ID_TRYAGAIN =             0xA,
        ID_YES =                  0x6
    }

    public enum MessageBoxButtons : uint
    {
        // Buttons
        MB_ABORTRETRYIGNORE =     0x2,
        MB_CANCELTRYCONTINUE =    0x6,
        MB_HELP =                 0x4000,
        MB_OK =                   0x0,
        MB_OKCANCEL =             0x1,
        MB_RETRYCANCEL =          0x5,
        MB_YESNO =                0x4,
        MB_YESNOCANCEL =          0x3,
    }

    public enum MessageBoxIcon : uint
    {
        // Icons
        None =                    0x0,
        MB_ICONEXCLAMATION =      0x30,
        MB_ICONWARNING =          0x30,
        MB_ICONINFORMATION =      0x40,
        MB_ICONASTERISK =         0x40,
        MB_ICONQUESTION =         0x20,
        MB_ICONSTOP =             0x10,
        MB_ICONERROR =            0x10,
        MB_ICONHAND =             0x10,
    }

    public enum MessageBoxDefaultButton : uint
    {
        // Default Buttons
        MB_DEFBUTTON1 =           0x0,
        MB_DEFBUTTON2 =           0x100,
        MB_DEFBUTTON3 =           0x200,
        MB_DEFBUTTON4 =           0x300,
    }

    public enum MessageBoxModality : uint
    {
        // Modality
        MB_APPLMODAL =            0x0,
        MB_SYSTEMMODAL =          0x1000,
        MB_TASKMODAL =            0x2000,
    }

    public enum MessageBoxOptions : uint
    {
        // Other Options
        None =                    0x0,
        MB_DEFAULT_DESKTOP_ONLY = 0x20000,
        MB_RIGHT =                0x80000,
        MB_RTLREADING =           0x100000,
        MB_SETFOREGROUND =        0x10000,
        MB_TOPMOST =              0x40000,
        MB_SERVICE_NOTIFICATION = 0x200000
    }
}

Example Usage:
Code:
static void MainMethod()
{
	NotificationResult result = Notification.Show("Message", "Caption", MessageBoxButtons.MB_YESNOCANCEL);
	Console.WriteLine("Result: {0}", result);
	if (result == NotificationResult.ID_YES)
	{
		Console.WriteLine("You Pressed YES");
	}
}
 
Last edited:

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top