AceInfinity
Emeritus, Contributor
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:
Usage:
You'll have to provide:
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.
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: