Ormalawayo

NativeWinAlert

Mar 13th, 2025 (edited)
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Runtime.InteropServices;
  3.  
  4. // DISCLAIMER: All comments and summaries in this code were added by ChatGPT, the AI assistant.
  5. // I made this from Roy de Jong's script https://gist.github.com/roydejong/130a91e1835154a3acaeda78c9dfbbd7 (with help from ChatGPT)
  6.  
  7. /// <summary>
  8. /// A class to display Windows MessageBox dialogs using the native Windows API.
  9. /// For more details on MessageBox usage, see:
  10. /// <see>https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messagebox</see>
  11. /// </summary>
  12. public class WinMsgBox
  13. {
  14.     // Import the MessageBox function from the user32.dll library,
  15.     // which is used to display a message box on Windows.
  16.     [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  17.     static extern int MessageBox(IntPtr hwnd, string lpText, string lpCaption, uint uType);
  18.  
  19.     /// <summary>
  20.     /// Enum to specify the icon to be displayed in the message box.
  21.     /// </summary>
  22.     [Flags]
  23.     public enum WinMsgIcon
  24.     {
  25.         None = 0x00000000,        // No icon
  26.         Error = 0x00000010,       // Displays an error icon (MB_ICONERROR)
  27.         Warning = 0x00000030,     // Displays a warning icon (MB_ICONEXCLAMATION)
  28.         Information = 0x00000040, // Displays an information icon (MB_ICONASTERISK)
  29.         Question = 0x00000020     // Displays a question icon (MB_ICONQUESTION)
  30.     }
  31.  
  32.     /// <summary>
  33.     /// Enum to specify the buttons and behavior of the message box prompt.
  34.     /// </summary>
  35.     [Flags]
  36.     public enum WinMsgPrompt : long
  37.     {
  38.         CancelRetryContinue = 0x00000006L,  // Displays Cancel, Retry, and Continue buttons
  39.         Help = 0x00004000L,                 // Displays a Help button
  40.         Ok = 0x00000000L,                   // Displays only an Ok button
  41.         OkCancel = 0x00000001L,             // Displays Ok and Cancel buttons
  42.         RetryCancel = 0x00000005L,          // Displays Retry and Cancel buttons
  43.         YesNo = 0x00000004L,                // Displays Yes and No buttons
  44.         YesNoCancel = 0x00000003L           // Displays Yes, No, and Cancel buttons
  45.     }
  46.  
  47.     /// <summary>
  48.     /// Displays a message box with the specified message, caption, icon, and prompt buttons.
  49.     ///
  50.     /// Note: The 'prompt' parameter is provided for completeness, but since this method does
  51.     /// not return any button pressed information, it would be most appropriate to use 'WinMsgPrompt.Ok'
  52.     /// as the button type. Other button configurations won't affect the result in this case.
  53.     /// </summary>
  54.     /// <param name="message">The text to display in the message box.</param>
  55.     /// <param name="caption">The title of the message box.</param>
  56.     /// <param name="icon">The icon to be displayed (e.g., Information, Error, etc.).</param>
  57.     /// <param name="prompt">The prompt buttons to be displayed (e.g., Ok, Yes/No, etc.).</param>
  58.     public static void WinAlert(string message = "", string caption = "", WinMsgIcon icon = WinMsgIcon.None, WinMsgPrompt prompt = WinMsgPrompt.Ok)
  59.     {
  60.         // Combine the icon and prompt values into a single uint for the MessageBox function
  61.         uint uType = (uint)icon | (uint)prompt;
  62.  
  63.         // Call the MessageBox function to display the message box with the provided parameters
  64.         MessageBox(IntPtr.Zero, message, caption, uType);
  65.     }
  66.  
  67.     /// <summary>
  68.     /// Displays a message box with the specified message, caption, icon, and prompt buttons.
  69.     /// This method returns the string name of the button pressed by the user.
  70.     /// The possible button names are:
  71.     /// 'Ok', 'Cancel', 'Abort', 'Retry', 'Ignore', 'Yes', 'No', 'Try Again', 'Continue'.
  72.     ///
  73.     /// Note: If the user clicks the "X" (close button) to close the message box, it will be counted as
  74.     /// pressing the **Cancel** button, even if the actual Cancel button is not present in the message box.
  75.     /// If the MessageBox function fails (returns 0), the method will return "Error".
  76.     /// </summary>
  77.     /// <param name="message">The text to display in the message box.</param>
  78.     /// <param name="caption">The title of the message box.</param>
  79.     /// <param name="icon">The icon to be displayed (e.g., Information, Error, etc.).</param>
  80.     /// <param name="prompt">The prompt buttons to be displayed (e.g., Ok, Yes/No, etc.).</param>
  81.     /// <returns>A string representing the button pressed by the user, or an error message if MessageBox fails.</returns>
  82.     public static string WinAlertOutput(string message = "", string caption = "", WinMsgIcon icon = WinMsgIcon.None, WinMsgPrompt prompt = WinMsgPrompt.Ok)
  83.     {
  84.         // Combine the icon and prompt values into a single uint for the MessageBox function
  85.         uint uType = (uint)icon | (uint)prompt;
  86.  
  87.         // Call the MessageBox function to display the message box and capture the button pressed
  88.         int result = MessageBox(IntPtr.Zero, message, caption, uType);
  89.  
  90.         // Check if the MessageBox call failed (returns 0)
  91.         if (result == 0)
  92.         {
  93.             return "Error: MessageBox failed to display.";
  94.         }
  95.  
  96.         // Convert the result (integer) into a string representation of the button pressed
  97.         return GetButtonName(result);
  98.     }
  99.  
  100.     /// <summary>
  101.     /// Converts the integer return value from MessageBox into a string representing the button pressed.
  102.     /// </summary>
  103.     /// <param name="result">The integer result returned from MessageBox.</param>
  104.     /// <returns>The string name of the button pressed.</returns>
  105.     private static string GetButtonName(int result)
  106.     {
  107.         switch (result)
  108.         {
  109.             case 1: return "Ok";
  110.             case 2: return "Cancel";
  111.             case 3: return "Abort";
  112.             case 4: return "Retry";
  113.             case 5: return "Ignore";
  114.             case 6: return "Yes";
  115.             case 7: return "No";
  116.             case 10: return "Try Again";
  117.             case 11: return "Continue";
  118.             default: return "Unknown"; // In case the return value is unexpected
  119.         }
  120.     }
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment