Silverlight / WPF - Create custom message box in SL5

Asked By gopal krish on 22-May-12 01:46 AM
hi..

how to Create custom message box in SL5..

need ur suggestions with examples..


regards
gopal.s
Somesh Yadav replied to gopal krish on 22-May-12 02:15 AM

One of the new features in SL5 is the ability to use PInvoke. This post shows how to use PInvoke to show MesageBox with lot more options.

Start by creating a SL5 OOB application with elevated trust and add the following code

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern MessageBoxResult MessageBox(IntPtr hWnd, String text, String caption, int options);
public enum MessageBoxOptions
{
Ok = 0×000000,
OkCancel = 0×000001,
AbortRetryIgnore = 0×000002,
YesNoCancel = 0×000003,
YesNo = 0×000004,
RetryCancel = 0×000005,
CancelTryContinue = 0×000006,

IconHand = 0×000010,
IconQuestion = 0×000020,
IconExclamation = 0×000030,
IconAsterisk = 0×000040,
UserIcon = 0×000080,

IconWarning = IconExclamation,
IconError = IconHand,
IconInformation = IconAsterisk,
IconStop = IconHand,

DefButton1 = 0×000000,
DefButton2 = 0×000100,
DefButton3 = 0×000200,
DefButton4 = 0×000300,

ApplicationModal = 0×000000,
SystemModal = 0×001000,
TaskModal = 0×002000,

Help = 0×004000, //Help Button
NoFocus = 0×008000,

SetForeground = 0×010000,
DefaultDesktopOnly = 0×020000,
Topmost = 0×040000,
Right = 0×080000,
RTLReading = 0×100000,
}

public enum MessageBoxResult : uint
{
Ok = 1,
Cancel,
Abort,
Retry,
Ignore,
Yes,
No,
Close,
Help,
TryAgain,
Continue,
Timeout = 32000
}

To actually see the messagebox, we could do something like

MessageBoxOptions options = MessageBoxOptions.CancelTryContinue | MessageBoxOptions.DefButton1 | MessageBoxOptions.IconQuestion;
MessageBox(IntPtr.Zero, “This is a messagebox with 3 buttons, one of them being default and a question icon”, “Caption”, (int)options);

Here is the result

http://leeontech.files.wordpress.com/2011/09/capture.png