Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Runtime.InteropServices;
- // DISCLAIMER: All comments and summaries in this code were added by ChatGPT, the AI assistant.
- // I made this from Roy de Jong's script https://gist.github.com/roydejong/130a91e1835154a3acaeda78c9dfbbd7 (with help from ChatGPT)
- /// <summary>
- /// A class to display Windows MessageBox dialogs using the native Windows API.
- /// For more details on MessageBox usage, see:
- /// <see>https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messagebox</see>
- /// </summary>
- public class WinMsgBox
- {
- // Import the MessageBox function from the user32.dll library,
- // which is used to display a message box on Windows.
- [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
- static extern int MessageBox(IntPtr hwnd, string lpText, string lpCaption, uint uType);
- /// <summary>
- /// Enum to specify the icon to be displayed in the message box.
- /// </summary>
- public enum WinMsgIcon
- {
- None = 0x00000000, // No icon
- Error = 0x00000010, // Displays an error icon (MB_ICONERROR)
- Warning = 0x00000030, // Displays a warning icon (MB_ICONEXCLAMATION)
- Information = 0x00000040, // Displays an information icon (MB_ICONASTERISK)
- Question = 0x00000020 // Displays a question icon (MB_ICONQUESTION)
- }
- /// <summary>
- /// Enum to specify the buttons and behavior of the message box prompt.
- /// </summary>
- public enum WinMsgPrompt : long
- {
- CancelRetryContinue = 0x00000006L, // Displays Cancel, Retry, and Continue buttons
- Help = 0x00004000L, // Displays a Help button
- Ok = 0x00000000L, // Displays only an Ok button
- OkCancel = 0x00000001L, // Displays Ok and Cancel buttons
- RetryCancel = 0x00000005L, // Displays Retry and Cancel buttons
- YesNo = 0x00000004L, // Displays Yes and No buttons
- YesNoCancel = 0x00000003L // Displays Yes, No, and Cancel buttons
- }
- /// <summary>
- /// Displays a message box with the specified message, caption, icon, and prompt buttons.
- ///
- /// Note: The 'prompt' parameter is provided for completeness, but since this method does
- /// not return any button pressed information, it would be most appropriate to use 'WinMsgPrompt.Ok'
- /// as the button type. Other button configurations won't affect the result in this case.
- /// </summary>
- /// <param name="message">The text to display in the message box.</param>
- /// <param name="caption">The title of the message box.</param>
- /// <param name="icon">The icon to be displayed (e.g., Information, Error, etc.).</param>
- /// <param name="prompt">The prompt buttons to be displayed (e.g., Ok, Yes/No, etc.).</param>
- public static void WinAlert(string message = "", string caption = "", WinMsgIcon icon = WinMsgIcon.None, WinMsgPrompt prompt = WinMsgPrompt.Ok)
- {
- // Combine the icon and prompt values into a single uint for the MessageBox function
- uint uType = (uint)icon | (uint)prompt;
- // Call the MessageBox function to display the message box with the provided parameters
- MessageBox(IntPtr.Zero, message, caption, uType);
- }
- /// <summary>
- /// Displays a message box with the specified message, caption, icon, and prompt buttons.
- /// This method returns the string name of the button pressed by the user.
- /// The possible button names are:
- /// 'Ok', 'Cancel', 'Abort', 'Retry', 'Ignore', 'Yes', 'No', 'Try Again', 'Continue'.
- ///
- /// Note: If the user clicks the "X" (close button) to close the message box, it will be counted as
- /// pressing the **Cancel** button, even if the actual Cancel button is not present in the message box.
- /// If the MessageBox function fails (returns 0), the method will return "Error".
- /// </summary>
- /// <param name="message">The text to display in the message box.</param>
- /// <param name="caption">The title of the message box.</param>
- /// <param name="icon">The icon to be displayed (e.g., Information, Error, etc.).</param>
- /// <param name="prompt">The prompt buttons to be displayed (e.g., Ok, Yes/No, etc.).</param>
- /// <returns>A string representing the button pressed by the user, or an error message if MessageBox fails.</returns>
- public static string WinAlertOutput(string message = "", string caption = "", WinMsgIcon icon = WinMsgIcon.None, WinMsgPrompt prompt = WinMsgPrompt.Ok)
- {
- // Combine the icon and prompt values into a single uint for the MessageBox function
- uint uType = (uint)icon | (uint)prompt;
- // Call the MessageBox function to display the message box and capture the button pressed
- int result = MessageBox(IntPtr.Zero, message, caption, uType);
- // Check if the MessageBox call failed (returns 0)
- if (result == 0)
- {
- return "Error: MessageBox failed to display.";
- }
- // Convert the result (integer) into a string representation of the button pressed
- return GetButtonName(result);
- }
- /// <summary>
- /// Converts the integer return value from MessageBox into a string representing the button pressed.
- /// </summary>
- /// <param name="result">The integer result returned from MessageBox.</param>
- /// <returns>The string name of the button pressed.</returns>
- private static string GetButtonName(int result)
- {
- switch (result)
- {
- case 1: return "Ok";
- case 2: return "Cancel";
- case 3: return "Abort";
- case 4: return "Retry";
- case 5: return "Ignore";
- case 6: return "Yes";
- case 7: return "No";
- case 10: return "Try Again";
- case 11: return "Continue";
- default: return "Unknown"; // In case the return value is unexpected
- }
- }
- }
Add Comment
Please, Sign In to add comment