コード例 #1
0
        private bool? ShowInCenterParent(WinMessageBox win)
        {
            bool? res = false;
            this.Dispatcher.Invoke(new Action(() =>
            {
                //ActiveWindowHandle = GetActiveWindow();  //获取父窗体句柄  
                int count = Application.Current.MainWindow.OwnedWindows.Count;
                Window parentWin = this;
                if (_openedWidnowsList.Count > 0)
                {
                    parentWin = _openedWidnowsList.Pop();
                }
                _openedWidnowsList.Push(parentWin);
                _openedWidnowsList.Push(win);
                win.Owner = parentWin;

                ActiveWindowHandle = new WindowInteropHelper(parentWin).Handle;

                WindowInteropHelper helper = new WindowInteropHelper(win);
                //helper.Owner = ActiveWindowHandle;

                RECT rect;
                GetWindowRect(ActiveWindowHandle, out rect);

                //win.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;

                //win.Left = rect.left + ((rect.right - rect.left) - win.Width) / 2;
                //win.Top = rect.top + ((rect.bottom - rect.top) - win.Height) / 2;
                //win.ShowInTaskbar = false;
                //win.ShowDialog();

                win.SourceInitialized += delegate
                {
                    // Get WPF size and location for non-WPF owner window
                    int nonWPFOwnerLeft = rect.left; // Get non-WPF owner’s Left
                    int nonWPFOwnerWidth = rect.right - rect.left; // Get non-WPF owner’s Width
                    int nonWPFOwnerTop = rect.top; // Get non-WPF owner’s Top
                    int nonWPFOwnerHeight = rect.bottom - rect.top; // Get non-WPF owner’s Height 

                    // Get transform matrix to transform non-WPF owner window
                    // size and location units into device-independent WPF 
                    // size and location units

                    HwndSource source = HwndSource.FromHwnd(helper.Handle);
                    if (source == null) return;
                    Matrix matrix = source.CompositionTarget.TransformFromDevice;
                    Point ownerWPFSize = matrix.Transform(
                      new Point(nonWPFOwnerWidth, nonWPFOwnerHeight));
                    Point ownerWPFPosition = matrix.Transform(
                      new Point(nonWPFOwnerLeft, nonWPFOwnerTop));

                    // Center WPF window
                    win.WindowStartupLocation = WindowStartupLocation.Manual;
                    win.Left = ownerWPFPosition.X + (ownerWPFSize.X - win.Width) / 2;
                    win.Top = ownerWPFPosition.Y + (ownerWPFSize.Y - win.Height) / 2;

                };
                res = win.ShowDialog();
            }));

            _openedWidnowsList.Pop();
            return res;
        }
コード例 #2
0
 private void button1_Click(object sender, RoutedEventArgs e)
 {
     //WinBrightAdjust bright = new WinBrightAdjust();
     //bright.ShowDialog();
     WinMessageBox msg = new WinMessageBox();
     msg.ShowDialog();
 }
コード例 #3
0
 private void OnShowGlobalMsgBox(DialogMessage msg)
 {
     this.Dispatcher.Invoke(new Action(() =>
     {
         WinMessageBox msgBox = new WinMessageBox() { MsgContent = msg.Content, Caption = msg.Caption, MsgButton = msg.Button, MsgImage = msg.Icon };
         msgBox.Owner = Application.Current.MainWindow;
         msgBox.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
         msgBox.ShowDialog();
     }));
 }