1
- using System . Diagnostics . CodeAnalysis ;
1
+ using System ;
2
+ using System . Diagnostics . CodeAnalysis ;
2
3
using System . Linq ;
4
+ using System . Runtime . InteropServices ;
3
5
using System . Windows ;
4
6
using System . Windows . Interop ;
5
7
using System . Windows . Threading ;
6
8
using Wpf . Ui . Violeta . Resources . Localization ;
9
+ using Wpf . Ui . Violeta . Threading ;
7
10
using Wpf . Ui . Violeta . Win32 ;
8
11
9
12
namespace Wpf . Ui . Violeta . Controls ;
@@ -45,7 +48,6 @@ private static IPendingHandler ShowCore(Window? owner, string? message, string?
45
48
CloseOnCanceled = closeOnCanceled ,
46
49
} ;
47
50
48
- dialog . Owner = owner ;
49
51
dialog . Closing += ( _ , e ) =>
50
52
{
51
53
if ( ! e . Cancel )
@@ -70,4 +72,146 @@ private static IPendingHandler ShowCore(Window? owner, string? message, string?
70
72
return pending ;
71
73
} ) ;
72
74
}
75
+
76
+ [ Obsolete ( "Under development" ) ]
77
+ public static STAThread < IPendingHandler > ShowAsync ( string ? message = null , string ? title = null , bool isShowCancel = false , bool closeOnCanceled = true )
78
+ {
79
+ return ShowAsync ( null , message , title , isShowCancel , closeOnCanceled ) ;
80
+ }
81
+
82
+ [ Obsolete ( "Under development" ) ]
83
+ public static STAThread < IPendingHandler > ShowAsync ( Window ? owner , string ? message , string ? title = null , bool isShowCancel = false , bool closeOnCanceled = true )
84
+ {
85
+ owner
86
+ ??= Application . Current . Windows . OfType < Window > ( ) . FirstOrDefault ( window => window . IsActive )
87
+ ?? Application . Current . MainWindow ;
88
+
89
+ STAThread < IPendingHandler > sta = new ( sta =>
90
+ {
91
+ sta . Result = ShowCoreAsync ( owner , message , title , isShowCancel , closeOnCanceled ) ;
92
+ sta . Result . Show ( ) ;
93
+ } ) ;
94
+ sta . Start ( ) ;
95
+ return sta ;
96
+ }
97
+
98
+ [ SuppressMessage ( "Performance" , "CA1859:Use concrete types when possible for improved performance" ) ]
99
+ [ SuppressMessage ( "Style" , "IDE0060:Remove unused parameter" ) ]
100
+ [ SuppressMessage ( "CodeQuality" , "IDE0079:Remove unnecessary suppression" ) ]
101
+ private static IPendingHandler ShowCoreAsync ( Window ? owner , string ? message , string ? title , bool isShowCancel , bool closeOnCanceled = true )
102
+ {
103
+ Point ? center = null ;
104
+
105
+ try
106
+ {
107
+ owner ? . Dispatcher . Invoke ( ( ) =>
108
+ {
109
+ RECT rect = new ( ) ;
110
+ _ = User32 . GetWindowRect ( new WindowInteropHelper ( owner ) . Handle , ref rect ) ;
111
+ POINT p = new ( rect . Left + ( int ) ( ( rect . Right - rect . Left ) / 2d ) , rect . Top + ( int ) ( ( rect . Bottom - rect . Top ) / 2d ) ) ;
112
+ center = new ( p . X , p . Y ) ;
113
+ } ) ;
114
+ }
115
+ catch
116
+ {
117
+ ///
118
+ }
119
+
120
+ PendingBoxDialog dialog = new ( )
121
+ {
122
+ Title = title ?? string . Empty ,
123
+ Message = message ?? ( SH . Loading + "..." ) ,
124
+ IsShowCancel = isShowCancel ,
125
+ WindowStartupLocation = WindowStartupLocation . CenterScreen ,
126
+ } ;
127
+ PendingHandlerAsync pending = new ( dialog ) ;
128
+
129
+ dialog . Closing += ( _ , e ) =>
130
+ {
131
+ if ( ! e . Cancel )
132
+ {
133
+ _ = User32 . SetForegroundWindow ( new WindowInteropHelper ( owner ) . Handle ) ;
134
+ }
135
+ } ;
136
+ dialog . Closed += ( s , e ) =>
137
+ {
138
+ pending . RaiseClosedEvent ( s , e ) ;
139
+ pending . Close ( ) ;
140
+ } ;
141
+ dialog . Canceled += ( s , e ) =>
142
+ {
143
+ pending . RaiseCanceledEvent ( s , e ) ;
144
+ pending . Close ( ) ;
145
+ } ;
146
+
147
+ if ( center != null )
148
+ {
149
+ dialog . Loaded += ( _ , _ ) =>
150
+ {
151
+ try
152
+ {
153
+ LayoutHelper . MoveWindowCenter ( dialog , ( Point ) center ) ;
154
+ }
155
+ catch
156
+ {
157
+ ///
158
+ }
159
+ } ;
160
+ }
161
+ return pending ;
162
+ }
163
+ }
164
+
165
+ file static class LayoutHelper
166
+ {
167
+ public static void MoveWindowCenter ( Window window , Point center )
168
+ {
169
+ if ( window == null )
170
+ {
171
+ return ;
172
+ }
173
+
174
+ nint hwnd = new WindowInteropHelper ( window ) . Handle ;
175
+
176
+ if ( hwnd == IntPtr . Zero )
177
+ {
178
+ return ;
179
+ }
180
+
181
+ RECT rect = default ;
182
+ _ = User32 . GetWindowRect ( hwnd , ref rect ) ;
183
+
184
+ nint monitorHandle = User32 . MonitorFromWindow ( hwnd , User32 . MonitorDefaultTo . Nearest ) ;
185
+ User32 . MONITORINFO monitorInfo = new ( )
186
+ {
187
+ cbSize = Marshal . SizeOf < User32 . MONITORINFO > ( )
188
+ } ;
189
+ _ = User32 . GetMonitorInfo ( monitorHandle , ref monitorInfo ) ;
190
+ RECT screen = monitorInfo . rcMonitor ;
191
+
192
+ ( int x , int y ) = ( ( int ) center . X , ( int ) center . Y ) ;
193
+ ( int w , int h ) = ( rect . Right - rect . Left , rect . Bottom - rect . Top ) ;
194
+
195
+ ( x , y ) = ( ( int ) ( x - w / 2d ) , ( int ) ( y - h / 2d ) ) ;
196
+
197
+ if ( x + w > screen . Right )
198
+ {
199
+ x = screen . Right - w ;
200
+ }
201
+ else if ( x - w < screen . Left )
202
+ {
203
+ x = screen . Left + w ;
204
+ }
205
+
206
+ if ( y + h > screen . Bottom )
207
+ {
208
+ y = screen . Bottom - h ;
209
+ }
210
+ else if ( y - h < screen . Top )
211
+ {
212
+ y = screen . Top + h ;
213
+ }
214
+
215
+ _ = User32 . MoveWindow ( hwnd , x , y , w , h , false ) ;
216
+ }
73
217
}
0 commit comments