Create Custom Dialog like MessageBox

  • Thread starter Dennis C. Drumm
  • Start date
D

Dennis C. Drumm

This is a restatement of an earlier post that evidently wasn't clear. I am
building a custom MessageBox dialog that has, among other things, a few new
button options (yes to all, no to all, etc.) and would like to make the
dialog look and act like the standard MessageBox. Therefore, I would like to
put in the message section the same type of icons found in the MessageBox
dialog, such as MessageBoxIcon.Exclamation. In another post here I had asked
about firing of sounds and it was suggested that I look at the book .NET
Framework Solutions, In Search of the Lost Win32 API by John Paul Meuller
for more information on rolling your own message dialogs. I have ordered
that book and it will be here in about a week. In the meantime, I thought I
would ask you guys how to put one of the standard MessageBox icons, such as
the exclamation icon, into my custom MessageBox dialog. Can someone tell how
that can be done?

Thanks,

Dennis
 
W

William Ryan

Here is the crux of the class. You can change the implementation but it
should give you a good idea of how to handle it. Good call on getting
Mueller's book...it's a masterpiece.

Good Luck,

Bill

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;
}
}
}
 
J

Jeffrey Tan[MSFT]

Hi Dennis,

I have added a reply to another posts of you in this group,
Please check if my reply was useful.
Thanks

Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Reply-To: "Dennis C. Drumm" <[email protected]>
| From: "Dennis C. Drumm" <[email protected]>
| Subject: Create Custom Dialog like MessageBox
| Date: Sat, 6 Sep 2003 04:33:31 -0400
| Lines: 20
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#X#[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: ipn36372-d67497.net-resource.net 216.204.76.41
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:182818
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| This is a restatement of an earlier post that evidently wasn't clear. I am
| building a custom MessageBox dialog that has, among other things, a few
new
| button options (yes to all, no to all, etc.) and would like to make the
| dialog look and act like the standard MessageBox. Therefore, I would like
to
| put in the message section the same type of icons found in the MessageBox
| dialog, such as MessageBoxIcon.Exclamation. In another post here I had
asked
| about firing of sounds and it was suggested that I look at the book .NET
| Framework Solutions, In Search of the Lost Win32 API by John Paul Meuller
| for more information on rolling your own message dialogs. I have ordered
| that book and it will be here in about a week. In the meantime, I thought
I
| would ask you guys how to put one of the standard MessageBox icons, such
as
| the exclamation icon, into my custom MessageBox dialog. Can someone tell
how
| that can be done?
|
| Thanks,
|
| Dennis
|
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top