Custom Dialog as MessageBox
I found from net a nice code to creat a custom dialog as a message box code goes like as follows.
Any idea please give comments
using System;
using System.Collections;
using
System.ComponentModel;
using System.Data;
using
System.Drawing;
using
System.Runtime.InteropServices;
using
System.Windows.Forms;
namespace ShowMessage
{
/// <summary>
/// Summary description for
Form1.
/// </summary>
public class
frmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnQuit;
private System.Windows.Forms.Button btnTest;
/// <summary>
/// Required designer
variable.
/// </summary>
private System.ComponentModel.Container components =
null;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}
private void btnQuit_Click(object sender, System.EventArgs
e)
{
// Exit the application.
Close();
}
// MessageBoxEx()
provides features, including a language identifier,
// not found in the .NET Framework version. This function
also enables
// you to add special buttons and
other features to the message box.
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int MessageBoxEx(
IntPtr hWnd,
[MarshalAs(UnmanagedType.LPTStr)]String Message,
[MarshalAs(UnmanagedType.LPTStr)]String Header,
UInt32 Type,
UInt16 LanguageID);
// Create a list of buttons.
public class
MBButton
{
public const UInt32
MB_OK = 0x00000000;
public
const UInt32 MB_OKCANCEL = 0x00000001;
public const UInt32 MB_ABORTRETRYIGNORE = 0x00000002;
public const UInt32 MB_YESNOCANCEL = 0x00000003;
public const UInt32 MB_YESNO = 0x00000004;
public const UInt32 MB_RETRYCANCEL = 0x00000005;
public const UInt32 MB_CANCELTRYCONTINUE = 0x00000006;
public const UInt32 MB_HELP = 0x00004000;
}
// Create a list of icon types.
public class MBIcon
{
public
const UInt32 MB_ICONHAND = 0x00000010;
public const UInt32 MB_ICONQUESTION = 0x00000020;
public const UInt32 MB_ICONEXCLAMATION = 0x00000030;
public const UInt32 MB_ICONASTERISK = 0x00000040;
public const UInt32 MB_USERICON = 0x00000080;
public const UInt32 MB_ICONWARNING =
MB_ICONEXCLAMATION;
public const UInt32
MB_ICONERROR = MB_ICONHAND;
public
const UInt32 MB_ICONINFORMATION = MB_ICONASTERISK;
public const UInt32 MB_ICONSTOP = MB_ICONHAND;
}
// Create a list of default buttons.
public class MBDefButton
{
public
const UInt32 MB_DEFBUTTON1 = 0x00000000;
public const UInt32 MB_DEFBUTTON2 = 0x00000100;
public const UInt32 MB_DEFBUTTON3 = 0x00000200;
public const UInt32 MB_DEFBUTTON4 = 0x00000300;
}
// Create a list of message box
modalities.
public class MBModal
{
public const UInt32 MB_APPLMODAL =
0x00000000;
public const UInt32
MB_SYSTEMMODAL = 0x00001000;
public const
UInt32 MB_TASKMODAL = 0x00002000;
}
// Create a list of special message box attributes.
public class MBSpecial
{
public
const UInt32 MB_SETFOREGROUND =
0x00010000;
public const UInt32
MB_DEFAULT_DESKTOP_ONLY = 0x00020000;
public const UInt32 MB_SERVICE_NOTIFICATION_NT3X =
0x00040000;
public const UInt32 MB_TOPMOST
= 0x00040000;
public
const UInt32 MB_RIGHT =
0x00080000;
public const UInt32
MB_RTLREADING = 0x00100000;
public const UInt32 MB_SERVICE_NOTIFICATION =
0x00200000;
}
// Return values
can use an enum in place of a class.
public
enum MBReturn
{
IDOK =
1,
IDCANCEL = 2,
IDABORT
= 3,
IDRETRY = 4,
IDIGNORE = 5,
IDYES = 6,
IDNO = 7,
IDCLOSE = 8,
IDHELP = 9,
IDTRYAGAIN = 10,
IDCONTINUE = 11,
IDTIMEOUT = 32000
}
private void btnTest_Click(object
sender, System.EventArgs e)
{
MBReturn Result; // Result of user input.
// Display a message box.
Result =
(MBReturn)MessageBoxEx(this.Handle,
"Your
message Here
"MessageBox Title Here",
MBButton.MB_CANCELTRYCONTINUE | MBButton.MB_HELP |
MBIcon.MB_ICONEXCLAMATION |
MBModal.MB_SYSTEMMODAL |
MBDefButton.MB_DEFBUTTON4 |
MBSpecial.MB_TOPMOST,
0);
// Determine a result.
switch (Result)
{
case MBReturn.IDCANCEL:
MessageBox.Show("Cancel");
break;
case MBReturn.IDTRYAGAIN:
MessageBox.Show("Try Again");
break;
case MBReturn.IDCONTINUE:
MessageBox.Show("Continue");
break;
default:
MessageBox.Show("?");
break;
}
}
private void frmMain_HelpRequested(object sender,
System.Windows.Forms.HelpEventArgs
hlpevent)
{
// Display
information about the help request.
MessageBox.Show("Here is your help message:\r\n" +
"\r\nSender: " + sender.ToString() +
"\r\nMouse Position: " + hlpevent.MousePos,
"Title
MessageBoxButtons.OK,
MessageBoxIcon.Information);
// Tell
Windows that the help request was handled.
hlpevent.Handled = true;
}
}
}
Thanks and Regards,
Suresh[Microsoft MVP.Net,India | MCAD.NET(CM) | MCSD.NET(EA)]