View difference between Paste ID: F0jhRCiq and tkBwZD6G
SHOW: | | - or go back to the newest paste.
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
	public enum WinMsgIcon
23
	{
24
		None = 0x00000000,        // No icon
25
		Error = 0x00000010,       // Displays an error icon (MB_ICONERROR)
26
		Warning = 0x00000030,     // Displays a warning icon (MB_ICONEXCLAMATION)
27
		Information = 0x00000040, // Displays an information icon (MB_ICONASTERISK)
28
		Question = 0x00000020     // Displays a question icon (MB_ICONQUESTION)
29
	}
30
31
	/// <summary>
32
	/// Enum to specify the buttons and behavior of the message box prompt.
33
	/// </summary>
34
	public enum WinMsgPrompt : long
35
	{
36
		CancelRetryContinue = 0x00000006L,  // Displays Cancel, Retry, and Continue buttons
37
		Help = 0x00004000L,                 // Displays a Help button
38
		Ok = 0x00000000L,                   // Displays only an Ok button
39
		OkCancel = 0x00000001L,             // Displays Ok and Cancel buttons
40
		RetryCancel = 0x00000005L,          // Displays Retry and Cancel buttons
41
		YesNo = 0x00000004L,                // Displays Yes and No buttons
42
		YesNoCancel = 0x00000003L           // Displays Yes, No, and Cancel buttons
43
	}
44
45
	/// <summary>
46
	/// Displays a message box with the specified message, caption, icon, and prompt buttons.
47
	/// 
48
	/// Note: The 'prompt' parameter is provided for completeness, but since this method does
49
	/// not return any button pressed information, it would be most appropriate to use 'WinMsgPrompt.Ok' 
50
	/// as the button type. Other button configurations won't affect the result in this case.
51
	/// </summary>
52
	/// <param name="message">The text to display in the message box.</param>
53
	/// <param name="caption">The title of the message box.</param>
54
	/// <param name="icon">The icon to be displayed (e.g., Information, Error, etc.).</param>
55
	/// <param name="prompt">The prompt buttons to be displayed (e.g., Ok, Yes/No, etc.).</param>
56
	public static void WinAlert(string message = "", string caption = "", WinMsgIcon icon = WinMsgIcon.None, WinMsgPrompt prompt = WinMsgPrompt.Ok)
57
	{
58
		// Combine the icon and prompt values into a single uint for the MessageBox function
59
		uint uType = (uint)icon | (uint)prompt;
60
61
		// Call the MessageBox function to display the message box with the provided parameters
62
		MessageBox(IntPtr.Zero, message, caption, uType);
63
	}
64
65
	/// <summary>
66
	/// Displays a message box with the specified message, caption, icon, and prompt buttons.
67
	/// This method returns the string name of the button pressed by the user.
68
	/// The possible button names are:
69
	/// 'Ok', 'Cancel', 'Abort', 'Retry', 'Ignore', 'Yes', 'No', 'Try Again', 'Continue'.
70
	/// 
71
	/// Note: If the user clicks the "X" (close button) to close the message box, it will be counted as
72
	/// pressing the **Cancel** button, even if the actual Cancel button is not present in the message box.
73
	/// If the MessageBox function fails (returns 0), the method will return "Error".
74
	/// </summary>
75
	/// <param name="message">The text to display in the message box.</param>
76
	/// <param name="caption">The title of the message box.</param>
77
	/// <param name="icon">The icon to be displayed (e.g., Information, Error, etc.).</param>
78
	/// <param name="prompt">The prompt buttons to be displayed (e.g., Ok, Yes/No, etc.).</param>
79
	/// <returns>A string representing the button pressed by the user, or an error message if MessageBox fails.</returns>
80
	public static string WinAlertOutput(string message = "", string caption = "", WinMsgIcon icon = WinMsgIcon.None, WinMsgPrompt prompt = WinMsgPrompt.Ok)
81
	{
82
		// Combine the icon and prompt values into a single uint for the MessageBox function
83
		uint uType = (uint)icon | (uint)prompt;
84
85
		// Call the MessageBox function to display the message box and capture the button pressed
86
		int result = MessageBox(IntPtr.Zero, message, caption, uType);
87
88
		// Check if the MessageBox call failed (returns 0)
89
		if (result == 0)
90
		{
91
			return "Error: MessageBox failed to display.";
92
		}
93
94
		// Convert the result (integer) into a string representation of the button pressed
95
		return GetButtonName(result);
96
	}
97
98
	/// <summary>
99
	/// Converts the integer return value from MessageBox into a string representing the button pressed.
100
	/// </summary>
101
	/// <param name="result">The integer result returned from MessageBox.</param>
102
	/// <returns>The string name of the button pressed.</returns>
103
	private static string GetButtonName(int result)
104
	{
105
		switch (result)
106
		{
107
			case 1: return "Ok";
108
			case 2: return "Cancel";
109
			case 3: return "Abort";
110
			case 4: return "Retry";
111
			case 5: return "Ignore";
112
			case 6: return "Yes";
113
			case 7: return "No";
114
			case 10: return "Try Again";
115
			case 11: return "Continue";
116
			default: return "Unknown"; // In case the return value is unexpected
117
		}
118
	}
119
}
120