1+ // Licensed to the .NET Foundation under one or more agreements.
2+ // The .NET Foundation licenses this file to you under the MIT license.
3+ // See the LICENSE file in the project root for more information.
4+ // https://github.com/dotnet/wpf/blob/v6.0.6/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/MessageBox.cs
5+
6+ #if ! NETFRAMEWORK && WINDOWS
7+
8+ using MS . Win32 ;
9+ using System . ComponentModel ;
10+ using System . Runtime . InteropServices ;
11+ using SR_ = MS . Internal . PresentationCore . SR ;
12+ using SRID = MS . Internal . PresentationCore . SRID ;
13+
14+ namespace System . Windows ;
15+
16+ public static class MessageBox
17+ {
18+ const int IDOK = 1 ;
19+ const int IDCANCEL = 2 ;
20+ const int IDYES = 6 ;
21+ const int IDNO = 7 ;
22+ const int DEFAULT_BUTTON1 = 0x00000000 ;
23+ const int DEFAULT_BUTTON2 = 0x00000100 ;
24+ const int DEFAULT_BUTTON3 = 0x00000200 ;
25+
26+ static MessageBoxResult Win32ToMessageBoxResult ( int value ) => value switch
27+ {
28+ IDOK => MessageBoxResult . OK ,
29+ IDCANCEL => MessageBoxResult . Cancel ,
30+ IDYES => MessageBoxResult . Yes ,
31+ IDNO => MessageBoxResult . No ,
32+ _ => MessageBoxResult . No ,
33+ } ;
34+
35+ public static MessageBoxResult Show (
36+ string messageBoxText ,
37+ string caption ,
38+ MessageBoxButton button ,
39+ MessageBoxImage icon ,
40+ MessageBoxResult defaultResult ,
41+ MessageBoxOptions options ) => ShowCore ( IntPtr . Zero , messageBoxText , caption , button , icon , defaultResult , options ) ;
42+
43+ public static MessageBoxResult Show (
44+ string messageBoxText ,
45+ string caption ,
46+ MessageBoxButton button ,
47+ MessageBoxImage icon ,
48+ MessageBoxResult defaultResult ) => ShowCore ( IntPtr . Zero , messageBoxText , caption , button , icon , defaultResult , 0 ) ;
49+
50+ public static MessageBoxResult Show (
51+ string messageBoxText ,
52+ string caption ,
53+ MessageBoxButton button ,
54+ MessageBoxImage icon ) => ShowCore ( IntPtr . Zero , messageBoxText , caption , button , icon , 0 , 0 ) ;
55+
56+ public static MessageBoxResult Show (
57+ string messageBoxText ,
58+ string caption ,
59+ MessageBoxButton button ) => ShowCore ( IntPtr . Zero , messageBoxText , caption , button , MessageBoxImage . None , 0 , 0 ) ;
60+
61+ public static MessageBoxResult Show ( string messageBoxText , string caption ) => ShowCore ( IntPtr . Zero , messageBoxText , caption , MessageBoxButton . OK , MessageBoxImage . None , 0 , 0 ) ;
62+
63+ public static MessageBoxResult Show ( string messageBoxText ) => ShowCore ( IntPtr . Zero , messageBoxText , string . Empty , MessageBoxButton . OK , MessageBoxImage . None , 0 , 0 ) ;
64+
65+ static int DefaultResultToButtonNumber ( MessageBoxResult result , MessageBoxButton button )
66+ {
67+ if ( result == 0 ) return DEFAULT_BUTTON1 ;
68+
69+ switch ( button )
70+ {
71+ case MessageBoxButton . OK :
72+ return DEFAULT_BUTTON1 ;
73+ case MessageBoxButton . OKCancel :
74+ if ( result == MessageBoxResult . Cancel ) return DEFAULT_BUTTON2 ;
75+ return DEFAULT_BUTTON1 ;
76+ case MessageBoxButton . YesNo :
77+ if ( result == MessageBoxResult . No ) return DEFAULT_BUTTON2 ;
78+ return DEFAULT_BUTTON1 ;
79+ case MessageBoxButton . YesNoCancel :
80+ if ( result == MessageBoxResult . No ) return DEFAULT_BUTTON2 ;
81+ if ( result == MessageBoxResult . Cancel ) return DEFAULT_BUTTON3 ;
82+ return DEFAULT_BUTTON1 ;
83+ default :
84+ return DEFAULT_BUTTON1 ;
85+ }
86+ }
87+
88+ internal static MessageBoxResult ShowCore (
89+ IntPtr owner ,
90+ string messageBoxText ,
91+ string caption ,
92+ MessageBoxButton button ,
93+ MessageBoxImage icon ,
94+ MessageBoxResult defaultResult ,
95+ MessageBoxOptions options )
96+ {
97+ if ( ! IsValidMessageBoxButton ( button ) )
98+ {
99+ throw new InvalidEnumArgumentException ( "button" , ( int ) button , typeof ( MessageBoxButton ) ) ;
100+ }
101+ if ( ! IsValidMessageBoxImage ( icon ) )
102+ {
103+ throw new InvalidEnumArgumentException ( "icon" , ( int ) icon , typeof ( MessageBoxImage ) ) ;
104+ }
105+ if ( ! IsValidMessageBoxResult ( defaultResult ) )
106+ {
107+ throw new InvalidEnumArgumentException ( "defaultResult" , ( int ) defaultResult , typeof ( MessageBoxResult ) ) ;
108+ }
109+ if ( ! IsValidMessageBoxOptions ( options ) )
110+ {
111+ throw new InvalidEnumArgumentException ( "options" , ( int ) options , typeof ( MessageBoxOptions ) ) ;
112+ }
113+
114+ // UserInteractive??
115+ //
116+ /*if (!SystemInformation.UserInteractive && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0) {
117+ throw new InvalidOperationException("UNDONE: SR.GetString(SR.CantShowModalOnNonInteractive)");
118+ }*/
119+
120+ if ( ( options & ( MessageBoxOptions . ServiceNotification | MessageBoxOptions . DefaultDesktopOnly ) ) != 0 )
121+ {
122+ if ( owner != IntPtr . Zero )
123+ {
124+ throw new ArgumentException ( SR_ . Get ( SRID . CantShowMBServiceWithOwner ) ) ;
125+ }
126+ }
127+ else
128+ {
129+ if ( owner == IntPtr . Zero )
130+ {
131+ owner = UnsafeNativeMethods . GetActiveWindow ( ) ;
132+ }
133+ }
134+
135+ int style = ( int ) button | ( int ) icon | ( int ) DefaultResultToButtonNumber ( defaultResult , button ) | ( int ) options ;
136+
137+ // modal dialog notification?
138+ //
139+ //Application.BeginModalMessageLoop();
140+ //MessageBoxResult result = Win32ToMessageBoxResult(SafeNativeMethods.MessageBox(new HandleRef(owner, handle), messageBoxText, caption, style));
141+ MessageBoxResult result = Win32ToMessageBoxResult ( UnsafeNativeMethods . MessageBox ( new HandleRef ( null , owner ) , messageBoxText , caption , style ) ) ;
142+ // modal dialog notification?
143+ //
144+ //Application.EndModalMessageLoop();
145+
146+ return result ;
147+ }
148+
149+ static bool IsValidMessageBoxButton ( MessageBoxButton value ) => value == MessageBoxButton . OK
150+ || value == MessageBoxButton . OKCancel
151+ || value == MessageBoxButton . YesNo
152+ || value == MessageBoxButton . YesNoCancel ;
153+
154+ static bool IsValidMessageBoxImage ( MessageBoxImage value ) => value == MessageBoxImage . Asterisk
155+ || value == MessageBoxImage . Error
156+ || value == MessageBoxImage . Exclamation
157+ || value == MessageBoxImage . Hand
158+ || value == MessageBoxImage . Information
159+ || value == MessageBoxImage . None
160+ || value == MessageBoxImage . Question
161+ || value == MessageBoxImage . Stop
162+ || value == MessageBoxImage . Warning ;
163+
164+ static bool IsValidMessageBoxResult ( MessageBoxResult value ) => value == MessageBoxResult . Cancel
165+ || value == MessageBoxResult . No
166+ || value == MessageBoxResult . None
167+ || value == MessageBoxResult . OK
168+ || value == MessageBoxResult . Yes ;
169+
170+ static bool IsValidMessageBoxOptions ( MessageBoxOptions value )
171+ {
172+ int mask = ~ ( ( int ) MessageBoxOptions . ServiceNotification |
173+ ( int ) MessageBoxOptions . DefaultDesktopOnly |
174+ ( int ) MessageBoxOptions . RightAlign |
175+ ( int ) MessageBoxOptions . RtlReading ) ;
176+
177+ if ( ( ( int ) value & mask ) == 0 )
178+ return true ;
179+ return false ;
180+ }
181+ }
182+
183+ public enum MessageBoxResult
184+ {
185+ None = 0 ,
186+
187+ OK = 1 ,
188+
189+ Cancel = 2 ,
190+
191+ Yes = 6 ,
192+
193+ No = 7 ,
194+
195+ // NOTE: if you add or remove any values in this enum, be sure to update MessageBox.IsValidMessageBoxResult()
196+ }
197+
198+ [ Flags ]
199+ public enum MessageBoxOptions
200+ {
201+ None = 0x00000000 ,
202+
203+ ServiceNotification = 0x00200000 ,
204+
205+ DefaultDesktopOnly = 0x00020000 ,
206+
207+ RightAlign = 0x00080000 ,
208+
209+ RtlReading = 0x00100000 ,
210+ }
211+
212+ public enum MessageBoxImage
213+ {
214+ None = 0 ,
215+
216+ Hand = 0x00000010 ,
217+
218+ Question = 0x00000020 ,
219+
220+ Exclamation = 0x00000030 ,
221+
222+ Asterisk = 0x00000040 ,
223+
224+ Stop = Hand ,
225+
226+ Error = Hand ,
227+
228+ Warning = Exclamation ,
229+
230+ Information = Asterisk ,
231+
232+ // NOTE: if you add or remove any values in this enum, be sure to update MessageBox.IsValidMessageBoxIcon()
233+ }
234+
235+ public enum MessageBoxButton
236+ {
237+ OK = 0x00000000 ,
238+
239+ OKCancel = 0x00000001 ,
240+
241+ YesNoCancel = 0x00000003 ,
242+
243+ YesNo = 0x00000004 ,
244+
245+ // NOTE: if you add or remove any values in this enum, be sure to update MessageBox.IsValidMessageBoxButton()
246+ }
247+
248+ #endif
0 commit comments