System.Windows.MessageBox vs System.Windows.Forms.MessageBox

你说的曾经没有我的故事 提交于 2019-11-30 21:48:52

问题


I am having trouble finding out what the key differences are between the two message boxes. What is the difference between System.Windows.MessageBox and System.Windows.Forms.MessageBox?


回答1:


System.Windows.MessageBox was added with WPF, and exists within the WPF assemblies (PresentationFramework.dll).

System.Windows.Forms.MessageBox was added with Windows Forms, and exists within the Windows Forms assemblies.

If your program is a Windows Forms program, I would use the latter (System.Windows.Forms.MessageBox), as it won't pull in a dependency on WPF. On the other hand, if you are developing for WPF, I'd use System.Windows.MessageBox.




回答2:


One additional point should be noted:

If you want to display a message box in an application that is neither a windows forms application or a forms application (such as a .NET console application), you should not drag in assembly references for either as seems to be the common mantra all over the internet.

Instead, you should use pinvoke and call into User32 as follows:

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern MessageBoxResult MessageBox(IntPtr hWnd, String text, String caption, int options);

/// <summary>
/// Flags that define appearance and behaviour of a standard message box displayed by a call to the MessageBox function.
/// </summary>
[Flags]
public enum MessageBoxOptions : uint
{
    Ok = 0x000000,
    OkCancel = 0x000001,
    AbortRetryIgnore = 0x000002,
    YesNoCancel = 0x000003,
    YesNo = 0x000004,
    RetryCancel = 0x000005,
    CancelTryContinue = 0x000006,

    IconHand = 0x000010,
    IconQuestion = 0x000020,
    IconExclamation = 0x000030,
    IconAsterisk = 0x000040,
    UserIcon = 0x000080,
    IconWarning = IconExclamation,
    IconError = IconHand,
    IconInformation = IconAsterisk,
    IconStop = IconHand,
    DefButton1 = 0x000000,
    DefButton2 = 0x000100,
    DefButton3 = 0x000200,
    DefButton4 = 0x000300,

    ApplicationModal = 0x000000,
    SystemModal = 0x001000,
    TaskModal = 0x002000,

    Help = 0x004000, //Help Button
    NoFocus = 0x008000,

    SetForeground = 0x010000,
    DefaultDesktopOnly = 0x020000,
    Topmost = 0x040000,
    Right = 0x080000,
    RTLReading = 0x100000,
}

/// <summary>
/// Represents possible values returned by the MessageBox function.
/// </summary>
public enum MessageBoxResult : uint
{
    Ok = 1,
    Cancel,
    Abort,
    Retry,
    Ignore,
    Yes,
    No,
    Close,
    Help,
    TryAgain,
    Continue,
    Timeout = 32000
}

var result = User32.MessageBox(IntPtr.Zero, "Debugging Break", "Your Console Application", (int)User32.MessageBoxOptions.Ok);



回答3:


Both eventually call the same low level windows API as far as I know...




回答4:


The both basically do the same thing, except system.windows.messagebox is WPF and system.windows.forms.messagebox is Windows Forms. If you're using WPF use the former, for WinForms use the latter.



来源:https://stackoverflow.com/questions/4660587/system-windows-messagebox-vs-system-windows-forms-messagebox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!