private static void CenterWindow(IntPtr hOwner, IntPtr hMessageBox) { try { // Size MessageBox // Размер MessageBox var rectChild = new UtilsWin32.Rect(); var widthChild = 0; var heightChild = 0; if (hMessageBox != IntPtr.Zero) { UtilsWin32.GetWindowRect(hMessageBox, ref rectChild); widthChild = rectChild.right - rectChild.left; heightChild = rectChild.bottom - rectChild.top; } // Get size and location MessageBox // Получить размер и позицию формы var rectParent = new UtilsWin32.Rect(); var widthParent = 0; var heightParent = 0; if (hOwner != IntPtr.Zero) { UtilsWin32.GetWindowRect(hOwner, ref rectParent); widthParent = rectParent.right - rectParent.left; heightParent = rectParent.bottom - rectParent.top; } // Center MessageBox on parent form // Центровка MessageBox на форме var left = rectParent.left + (widthParent - widthChild) / 2; var top = rectParent.top + (heightParent - heightChild) / 2; UtilsWin32.SetWindowPos(hMessageBox, UtilsWin32.SetWindowPosConst.HWND_TOPMOST, left, top, 0, 0, UtilsWin32.SetWindowPosConst.SWP_NOSIZE | UtilsWin32.SetWindowPosConst.SWP_NOOWNERZORDER | UtilsWin32.SetWindowPosConst.SWP_SHOWWINDOW); } catch (Exception) { // } }
public DialogResult Show(IWin32Window owner) { CheckIfDisposed(); var hOwner = IntPtr.Zero; if (owner != null) { hOwner = owner.Handle; } // Task for search MessageBox and timer change // Задача на отслеживание MessageBox и изменение таймера Task.Run(() => { try { IntPtr hWnd; // Wait MessageBox, work go down // Ждать окно сообщения. Работа перейдёт вниз по коду. while ((hWnd = UtilsWin32.FindWindow("#32770", Caption)) == IntPtr.Zero) { Thread.Sleep(25); // Exit by timer // Выйти по таймеру if (DateTime.Now - DtStart > TimeSpan.FromSeconds(Timeout)) { break; } } // Center window one time // Отцентрировать один раз if (hWnd != IntPtr.Zero) { CenterWindow(hOwner, hWnd); } // Resume thread // Возобновить поток while (hWnd != IntPtr.Zero) { // Change caption // Изменить заголовок UtilsWin32.SendMessage(hWnd, UtilsWin32.MsgConst.WM_SETTEXT, IntPtr.Zero, Caption + @" [" + (DtStart - DateTime.Now.AddSeconds(-Timeout)).Seconds + @"]"); Thread.Sleep(100); // Timer // Таймер if (DateTime.Now - DtStart > TimeSpan.FromSeconds(Timeout)) { // Change caption // Изменить заголовок UtilsWin32.SendMessage(hWnd, UtilsWin32.MsgConst.WM_SETTEXT, IntPtr.Zero, Caption); // Close MessageBox, if possible // Закрыть окно, если возможно UtilsWin32.SendMessage(hWnd, UtilsWin32.MsgConst.WM_CLOSE, IntPtr.Zero, IntPtr.Zero); // Wait MessageBox // Подождать Thread.Sleep(250); // Press Enter on MessageBox there haven't close button // Нажать Enter на окнах без кнопки закрытия UtilsWin32.PostMessage(hWnd, UtilsWin32.MsgConst.WM_KEYDOWN, 0x0D, 0x0); // Exit while // Завершить цикл break; } } } catch (Exception) { // } }); _result = owner == null ? MessageBox.Show(Text, Caption, Buttons, MessageBoxIcon, DefaultButton) : MessageBox.Show(owner, Text, Caption, Buttons, MessageBoxIcon, DefaultButton); return(_result); }