Code:
/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / Framework / System / Windows / MessageBox.cs / 1 / MessageBox.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // // // History: // [....] 08/07/03 Moved to wcp tree //----------------------------------------------------------------------------- using System; using System.Runtime.InteropServices; using System.Security.Permissions; using System.Security; using System.ComponentModel; using System.Windows; using System.Windows.Interop; using MS.Utility; using MS.Win32; namespace System.Windows { ////// public sealed class MessageBox { #if NEVER class WindowWin32Window : IWin32Window { Window window; internal WindowWin32Window(Window window) { this.window = window; } IntPtr IWin32Window.Handle { get { return window.SourceWindow.Handle; } } IntPtr IWin32Window.Parent { get { return ((IWin32Window)window).Parent; } set { ((IWin32Window)window).Parent = value; } } } #endif private const int IDOK = 1; private const int IDCANCEL = 2; private const int IDABORT = 3; private const int IDRETRY = 4; private const int IDIGNORE = 5; private const int IDYES = 6; private const int IDNO = 7; private const int DEFAULT_BUTTON1 = 0x00000000; private const int DEFAULT_BUTTON2 = 0x00000100; private const int DEFAULT_BUTTON3 = 0x00000200; ////// Displays a /// message box that can contain text, button, and symbols that /// inform and instruct the /// user. /// ////// This constructor is private so people aren't tempted to try and create /// instances of these -- they should just use the static show /// methods. /// private MessageBox() { } private static MessageBoxResult Win32ToMessageBoxResult(int value) { switch (value) { case IDOK: return MessageBoxResult.OK; case IDCANCEL: return MessageBoxResult.Cancel; case IDYES: return MessageBoxResult.Yes; case IDNO: return MessageBoxResult.No; default: return MessageBoxResult.No; } } #region No Owner Methods ////// ////// Displays a message box with specified text, caption, and style. /// ////// Critical: This code calls ShowCore which is critical /// PublicOK: Showcore filters for valid input and in this case the IntPtr is valid /// [SecurityCritical] public static MessageBoxResult Show( string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, MessageBoxOptions options) { return ShowCore(IntPtr.Zero, messageBoxText, caption, button, icon, defaultResult, options); } ////// ////// Displays a message box with specified text, caption, and style. /// ////// Critical: This code calls ShowCore which is critical /// PublicOK: Showcore filters for valid input and in this case the IntPtr is valid /// [SecurityCritical] public static MessageBoxResult Show( string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult) { return ShowCore(IntPtr.Zero, messageBoxText, caption, button, icon, defaultResult, 0); } ////// ////// Displays a message box with specified text, caption, and style. /// ////// Critical: This code calls ShowCore which is critical /// PublicOK: Showcore filters for valid input and in this case the IntPtr is valid /// [SecurityCritical] public static MessageBoxResult Show( string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon) { return ShowCore(IntPtr.Zero, messageBoxText, caption, button, icon, 0, 0); } ////// ////// Displays a message box with specified text, caption, and style. /// ////// Critical: This code calls ShowCore which is critical /// PublicOK: Showcore filters for valid input and in this case the IntPtr is valid /// [SecurityCritical] public static MessageBoxResult Show( string messageBoxText, string caption, MessageBoxButton button) { return ShowCore(IntPtr.Zero, messageBoxText, caption, button, MessageBoxImage.None, 0, 0); } ////// ////// Displays a message box with specified text and caption. /// ////// Critical: This code calls ShowCore which is critical /// PublicOK: Showcore filters for valid input and in this case the IntPtr is valid /// [SecurityCritical] public static MessageBoxResult Show(string messageBoxText, string caption) { return ShowCore(IntPtr.Zero, messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.None, 0, 0); } ////// ////// Displays a message box with specified text. /// ////// Critical: This code calls ShowCore which is critical /// PublicOK: Showcore filters for valid input and in this case the IntPtr is valid /// [SecurityCritical] public static MessageBoxResult Show(string messageBoxText) { return ShowCore(IntPtr.Zero, messageBoxText, String.Empty, MessageBoxButton.OK, MessageBoxImage.None, 0, 0); } #endregion #if WIN32_OWNER_WINDOW #region IWin32Window Methods ////// ////// Displays a message box with specified text, caption, and style. /// ////// /// Critical: This code calls ShowCore which is critical /// PublicOK: Showcore filters for valid input and in this case the IntPtr is valid /// since we do not let you create a new window in PT /// [SecurityCritical] public static MessageBoxResult Show(IWin32Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, MessageBoxOptions options) { return ShowCore(owner, messageBoxText, caption, button, icon, defaultResult, options); } ////// ////// Displays a message box with specified text, caption, and style. /// ////// /// Critical: This code calls ShowCore which is critical /// PublicOK: Showcore filters for valid input and in this case the IntPtr is valid /// since we do not let you create a new window in PT /// [SecurityCritical] public static MessageBoxResult Show(IWin32Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult) { return ShowCore(owner, messageBoxText, caption, button, icon, defaultResult, 0); } ////// ////// Displays a message box with specified text, caption, and style. /// ////// /// Critical: This code calls ShowCore which is critical /// PublicOK: Showcore filters for valid input and in this case the IntPtr is valid /// since we do not let you create a new window in PT /// [SecurityCritical] public static MessageBoxResult Show(IWin32Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon) { return ShowCore(owner, messageBoxText, caption, button, icon, 0, 0); } ////// ////// Displays a message box with specified text, caption, and style. /// ////// /// Critical: This code calls ShowCore which is critical /// PublicOK: Showcore filters for valid input and in this case the IntPtr is valid /// since we do not let you create a new window in PT /// [SecurityCritical] public static MessageBoxResult Show(IWin32Window owner, string messageBoxText, string caption, MessageBoxButton button) { return ShowCore(owner, messageBoxText, caption, button, MessageBoxImage.None, 0, 0); } ////// ////// Displays a message box with specified text and caption. /// ////// /// Critical: This code calls ShowCore which is critical /// PublicOK: Showcore filters for valid input and in this case the IntPtr is valid /// since we do not let you create a new window in PT /// [SecurityCritical] public static MessageBoxResult Show(IWin32Window owner, string messageBoxText, string caption) { return ShowCore(owner, messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.None, 0, 0); } ////// ////// Displays a message box with specified text. /// ////// /// Critical: This code calls ShowCore which is critical /// PublicOK: Showcore filters for valid input and in this case the IntPtr is valid /// since we do not let you create a new window in PT /// [SecurityCritical] public static MessageBoxResult Show(IWin32Window owner, string messageBoxText) { return ShowCore(owner, messageBoxText, String.Empty, MessageBoxButton.OK, MessageBoxImage.None, 0, 0); } #endregion #endif #region Window Methods ////// ////// Displays a message box with specified text, caption, and style. /// ////// Critical: This code accesses critical handle /// PublicOK: It passes the handle to ShowCore which sends it to MessageBox /// [SecurityCritical] public static MessageBoxResult Show( Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, MessageBoxOptions options) { return ShowCore((new WindowInteropHelper(owner)).CriticalHandle, messageBoxText, caption, button, icon, defaultResult, options); } ////// ////// Displays a message box with specified text, caption, and style. /// ////// Critical: This code accesses critical handle /// PublicOK: It passes the handle to ShowCore which sends it to MessageBox /// [SecurityCritical] public static MessageBoxResult Show( Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult) { return ShowCore((new WindowInteropHelper (owner)).CriticalHandle, messageBoxText, caption, button, icon, defaultResult, 0); } ////// ////// Displays a message box with specified text, caption, and style. /// ////// Critical: This code accesses critical handle /// PublicOK: It passes the handle to ShowCore which sends it to MessageBox /// [SecurityCritical] public static MessageBoxResult Show( Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon) { return ShowCore((new WindowInteropHelper (owner)).CriticalHandle, messageBoxText, caption, button, icon, 0, 0); } ////// ////// Displays a message box with specified text, caption, and style. /// ////// Critical: This code accesses critical handle /// PublicOK: It passes the handle to ShowCore which sends it to MessageBox /// [SecurityCritical] public static MessageBoxResult Show( Window owner, string messageBoxText, string caption, MessageBoxButton button) { return ShowCore((new WindowInteropHelper (owner)).CriticalHandle, messageBoxText, caption, button, MessageBoxImage.None, 0, 0); } ////// ////// Displays a message box with specified text and caption. /// ////// Critical: This code accesses critical handle /// PublicOK: It passes the handle to ShowCore which sends it to MessageBox /// [SecurityCritical] public static MessageBoxResult Show(Window owner, string messageBoxText, string caption) { return ShowCore((new WindowInteropHelper (owner)).CriticalHandle, messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.None, 0, 0); } ////// ////// Displays a message box with specified text. /// ////// Critical: This code accesses critical handle /// PublicOK: It passes the handle to ShowCore which sends it to MessageBox /// [SecurityCritical] public static MessageBoxResult Show(Window owner, string messageBoxText) { return ShowCore((new WindowInteropHelper (owner)).CriticalHandle, messageBoxText, String.Empty, MessageBoxButton.OK, MessageBoxImage.None, 0, 0); } #endregion private static int DefaultResultToButtonNumber(MessageBoxResult result, MessageBoxButton button) { if (result == 0) return DEFAULT_BUTTON1; switch (button) { case MessageBoxButton.OK: return DEFAULT_BUTTON1; case MessageBoxButton.OKCancel: if (result == MessageBoxResult.Cancel) return DEFAULT_BUTTON2; return DEFAULT_BUTTON1; case MessageBoxButton.YesNo: if (result == MessageBoxResult.No) return DEFAULT_BUTTON2; return DEFAULT_BUTTON1; case MessageBoxButton.YesNoCancel: if (result == MessageBoxResult.No) return DEFAULT_BUTTON2; if (result == MessageBoxResult.Cancel) return DEFAULT_BUTTON3; return DEFAULT_BUTTON1; default: return DEFAULT_BUTTON1; } } ////// Critical: This code calls into UnsafeNativeMethods.MessageBox and takes an IntPtr /// [SecurityCritical] private static MessageBoxResult ShowCore( IntPtr owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, MessageBoxOptions options) { if (!IsValidMessageBoxButton(button)) { throw new InvalidEnumArgumentException ("button", (int)button, typeof(MessageBoxButton)); } if (!IsValidMessageBoxImage(icon)) { throw new InvalidEnumArgumentException ("icon", (int)icon, typeof(MessageBoxImage)); } if (!IsValidMessageBoxResult(defaultResult)) { throw new InvalidEnumArgumentException ("defaultResult", (int)defaultResult, typeof(MessageBoxResult)); } if (!IsValidMessageBoxOptions(options)) { throw new InvalidEnumArgumentException("options", (int)options, typeof(MessageBoxOptions)); } // options intentionally not verified because we don't expose all the options Win32 supports. // /*if (!SystemInformation.UserInteractive && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0) { throw new InvalidOperationException(" */ if (owner != IntPtr.Zero && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0) { throw new ArgumentException (SR.Get(SRID.CantShowMBServiceWithOwner)); } //CASRemoval:new UIPermission(UIPermissionWindow.SafeSubWindows).Demand(); int style = (int) button | (int) icon | (int) DefaultResultToButtonNumber(defaultResult, button) | (int) options; IntPtr handle = IntPtr.Zero; if ((options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0) { if (owner == IntPtr.Zero) { handle = UnsafeNativeMethods.GetActiveWindow(); } else { handle = owner; } } // MessageBoxResult result = Win32ToMessageBoxResult (UnsafeNativeMethods.MessageBox (new HandleRef (null, handle), messageBoxText, caption, style)); // return result; } private static bool IsValidMessageBoxButton(MessageBoxButton value) { return value == MessageBoxButton.OK || value == MessageBoxButton.OKCancel || value == MessageBoxButton.YesNo || value == MessageBoxButton.YesNoCancel; } private static bool IsValidMessageBoxImage(MessageBoxImage value) { return value == MessageBoxImage.Asterisk || value == MessageBoxImage.Error || value == MessageBoxImage.Exclamation || value == MessageBoxImage.Hand || value == MessageBoxImage.Information || value == MessageBoxImage.None || value == MessageBoxImage.Question || value == MessageBoxImage.Stop || value == MessageBoxImage.Warning; } private static bool IsValidMessageBoxResult(MessageBoxResult value) { return value == MessageBoxResult.Cancel || value == MessageBoxResult.No || value == MessageBoxResult.None || value == MessageBoxResult.OK || value == MessageBoxResult.Yes; } ////// This code exists to prevent a denial of service attack on massagebox. The logic here is to /// create a mask where all the valid bits are zero and invalid bits are one. Using that "And /// operation we are able to decide whether the bits passed in were all valid (result is zero) /// or whether any invalid bits were set (Result is non zero) /// private static bool IsValidMessageBoxOptions(MessageBoxOptions value) { int mask = ~((int)MessageBoxOptions.ServiceNotification | (int)MessageBoxOptions.DefaultDesktopOnly | (int)MessageBoxOptions.RightAlign | (int)MessageBoxOptions.RtlReading); if (((int)value & mask) == 0) return true; return false; } } ////// ////// Specifies identifiers to /// indicate the return value of a dialog box. /// ///public enum MessageBoxResult { /// /// ////// /// Nothing is returned from the dialog box. This /// means that the modal dialog continues running. /// /// ///None = 0, /// /// ////// The /// dialog box return value is /// OK (usually sent from a button labeled OK). /// /// ///OK = 1, /// /// ////// The /// dialog box return value is Cancel (usually sent /// from a button labeled Cancel). /// /// ///Cancel = 2, /// /// ////// The dialog box return value is /// Yes (usually sent from a button labeled Yes). /// /// ///Yes = 6, /// /// ////// The dialog box return value is /// No (usually sent from a button labeled No). /// /// ///No = 7, // NOTE: if you add or remove any values in this enum, be sure to update MessageBox.IsValidMessageBoxResult() } /// /// [Flags] public enum MessageBoxOptions { ///[To be supplied.] ////// ////// Specifies that all default options should be used. /// ///None = 0x00000000, /// /// ////// Specifies that the message box is displayed on the active desktop. /// ///ServiceNotification = 0x00200000, /// /// ////// Specifies that the message box is displayed on the active desktop. /// ///DefaultDesktopOnly = 0x00020000, /// /// ////// Specifies that the message box text is right-aligned. /// ///RightAlign = 0x00080000, /// /// ////// Specifies that the message box text is displayed with Rtl reading order. /// ///RtlReading = 0x00100000, } /// /// public enum MessageBoxImage { ///[To be supplied.] ////// ////// Specifies that the /// message box contain no symbols. /// ///None = 0, /// /// ////// Specifies that the /// message box contains a /// hand symbol. /// ///Hand = 0x00000010, /// /// ////// Specifies /// that the message /// box contains a question /// mark symbol. /// ///Question = 0x00000020, /// /// ////// Specifies that the /// message box contains an /// exclamation symbol. /// ///Exclamation = 0x00000030, /// /// ////// Specifies that the /// message box contains an /// asterisk symbol. /// ///Asterisk = 0x00000040, /// /// ////// Specifies that the message box contains a hand icon. This field is /// constant. /// ///Stop = Hand, /// /// ////// Specifies that the /// message box contains a /// hand icon. /// ///Error = Hand, /// /// ////// Specifies that the message box contains an exclamation icon. /// ///Warning = Exclamation, /// /// ////// Specifies that the /// message box contains an /// asterisk icon. /// ///Information = Asterisk, // NOTE: if you add or remove any values in this enum, be sure to update MessageBox.IsValidMessageBoxIcon() } /// /// public enum MessageBoxButton { ///[To be supplied.] ////// OK = 0x00000000, ////// Specifies that the /// message box contains an OK button. This field is /// constant. /// ////// OKCancel = 0x00000001, ////// Specifies that the /// message box contains OK and Cancel button. This field /// is /// constant. /// ////// YesNoCancel = 0x00000003, ////// Specifies that the /// message box contains Yes, No, and Cancel button. This /// field is /// constant. /// ////// YesNo = 0x00000004, // NOTE: if you add or remove any values in this enum, be sure to update MessageBox.IsValidMessageBoxButton() } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved./// Specifies that the /// message box contains Yes and No button. This field is /// constant. /// ///
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- KeyInfo.cs
- PropertyInformationCollection.cs
- XmlQualifiedName.cs
- precedingquery.cs
- ToolboxCategoryItems.cs
- MetabaseServerConfig.cs
- ClientConfigurationHost.cs
- AssemblyNameUtility.cs
- SectionInput.cs
- WebHostedComPlusServiceHost.cs
- SamlAuthenticationClaimResource.cs
- DesignerVerbCollection.cs
- AsyncSerializedWorker.cs
- SelectionItemProviderWrapper.cs
- SqlUDTStorage.cs
- PasswordBoxAutomationPeer.cs
- SynchronizationContext.cs
- SecUtil.cs
- EdmSchemaError.cs
- LogArchiveSnapshot.cs
- DbSource.cs
- SafeMemoryMappedViewHandle.cs
- XmlAttributes.cs
- MessageQueue.cs
- _CacheStreams.cs
- SessionEndingCancelEventArgs.cs
- PrinterResolution.cs
- DoubleCollection.cs
- StatusBarDrawItemEvent.cs
- ValidateNames.cs
- DataGridViewLinkColumn.cs
- ProcessManager.cs
- HandleExceptionArgs.cs
- DmlSqlGenerator.cs
- SmtpReplyReader.cs
- RectangleHotSpot.cs
- UniqueConstraint.cs
- filewebrequest.cs
- NativeCompoundFileAPIs.cs
- SettingsAttributeDictionary.cs
- XmlSerializationWriter.cs
- CodeCompiler.cs
- FixedTextView.cs
- ConfigurationException.cs
- InvalidCommandTreeException.cs
- StandardOleMarshalObject.cs
- SchemaImporter.cs
- NotifyIcon.cs
- ClientConfigurationSystem.cs
- AddingNewEventArgs.cs
- XmlBaseReader.cs
- TextParaLineResult.cs
- IndependentAnimationStorage.cs
- BindingManagerDataErrorEventArgs.cs
- ResourceDescriptionAttribute.cs
- WindowsFormsSynchronizationContext.cs
- TabControlCancelEvent.cs
- SkipQueryOptionExpression.cs
- Instrumentation.cs
- VectorAnimationBase.cs
- SystemResourceHost.cs
- HwndSourceKeyboardInputSite.cs
- UriTemplateCompoundPathSegment.cs
- ResolveNameEventArgs.cs
- Listen.cs
- ComponentChangingEvent.cs
- ToolStripProfessionalLowResolutionRenderer.cs
- NotificationContext.cs
- coordinatorscratchpad.cs
- NewArray.cs
- TextPenaltyModule.cs
- CoreSwitches.cs
- SystemColors.cs
- FixedDocumentPaginator.cs
- FunctionParameter.cs
- BinaryVersion.cs
- ColorMap.cs
- InvalidChannelBindingException.cs
- PrimitiveOperationFormatter.cs
- FlagsAttribute.cs
- ObservableCollection.cs
- CollectionDataContractAttribute.cs
- SQLDoubleStorage.cs
- TextEffect.cs
- DataGridViewImageColumn.cs
- FacetChecker.cs
- SerializationStore.cs
- DataRowView.cs
- SafeProcessHandle.cs
- ThemeableAttribute.cs
- ChannelTraceRecord.cs
- WebEventTraceProvider.cs
- UnionExpr.cs
- Identifier.cs
- ZoneButton.cs
- ChineseLunisolarCalendar.cs
- PrintDocument.cs
- ToolStripTemplateNode.cs
- MediaElement.cs
- SignedInfo.cs