zoukankan      html  css  js  c++  java
  • WPF 窗体使用 Show() 单个显示并设置弹窗相对于主窗体位置

    代码示例:

    WndNavigation _wnd = null;   // WPF窗体全局变量
    
    /// <summary>
    /// 适用Show仅打开一个窗体,并显示在相对于主窗体的位置
    /// </summary>
    private void OpenWnd()
    {
        if (_wnd == null || !_wnd.IsLoaded)
        {
            _wnd = new WndNavigation();
            _wnd.Closing += (s, e) => { _wnd = null; };
            _wnd.WindowStartupLocation = WindowStartupLocation.Manual;
            Window owner = System.Windows.Application.Current.MainWindow;
            _wnd.Owner = owner;
            if (owner != null)
            {
                #region 设置本窗体位置
                System.Windows.Point pLeft = owner.PointToScreen(new System.Windows.Point(0, 0));
                IntPtr hwnd = new WindowInteropHelper(owner).Handle;
                HwndSource hwndSource = HwndSource.FromHwnd(hwnd);
                if (hwndSource != null)
                    pLeft = hwndSource.CompositionTarget.TransformFromDevice.Transform(pLeft);
    
                // 在主窗体右上角
                //Point rightTop = new Point(pLeft.X + owner.ActualWidth, pLeft.Y);
                //_wnd.Left = rightTop.X - _wnd.Width - 80;
                //_wnd.Top = rightTop.Y + 25;
    
                System.Windows.Point rightBottom = new System.Windows.Point(pLeft.X + owner.ActualWidth, pLeft.Y + owner.ActualHeight);
    
                // 在主窗体右下角
                _wnd.Left = rightBottom.X - _wnd.Width - 110;
                _wnd.Top = rightBottom.Y - _wnd.Height - 70;
                #endregion
            }
            else
            {
                _wnd.Left = SystemParameters.WorkArea.Width - 280;
                _wnd.Top = SystemParameters.WorkArea.Height - 100;
            }
        }
    
        _wnd.Topmost = true;
    
        if (!_wnd.IsLoaded)
            _wnd.Show();
        else
            _wnd.Activate();
    }

    End~

  • 相关阅读:
    [USACO11DEC]牧草种植Grass Planting
    [LNOI2014]LCA
    PTA的Python练习题(九)
    竞争条件攻击
    burpsuite使用--暴力破解
    PTA的Python练习题(八)
    PTA的Python练习题(七)
    PTA的Python练习题(六)
    攻防世界Web进阶-Upload1
    BugkuCTF解题Web基础(一)
  • 原文地址:https://www.cnblogs.com/seanyan/p/15683094.html
Copyright © 2011-2022 走看看