Exemplo n.º 1
0
        public static void CenterWindowOnMasterWindowScreen(Window window, IMasterDisplayWindow masterWindow)
        {
            log.InfoFormat("Center window on master window screen, window: widht={0}, height={1}", window.Width, window.Height);
            if (null == masterWindow)
            {
                log.Info("Master window is null and can not center window");
                return;
            }

            Screen screen = DpiUtil.GetScreenByHandle(masterWindow.GetHandle());

            double currentRatio      = 1;
            uint   currentScreenDpiX = 96;
            uint   currentScreenDpiY = 96;

            try
            {
                DpiUtil.GetDpiByScreen(screen, out currentScreenDpiX, out currentScreenDpiY);
                currentRatio = (double)currentScreenDpiX / 96;
            }
            catch (DllNotFoundException e)
            {
                log.ErrorFormat("Can not load windows dll: {0}", e);
            }

            //if (masterWindow.GetWindow().WindowState == WindowState.Minimized)
            //{
            //    masterWindow.GetWindow().WindowState = WindowState.Normal;
            //}

            double tempWidth  = window.Width;
            double tempHeight = window.Height;
            double left       = 0;
            double top        = 0;

            if (screen.DeviceName.Equals(System.Windows.Forms.Screen.PrimaryScreen.DeviceName))
            {
                log.InfoFormat("Set window on primary screen, screen: left={0}, top: {1}, width: {2}, height: {3}, ratio: {4}"
                               , screen.Bounds.Left, screen.Bounds.Top, screen.Bounds.Width, screen.Bounds.Height, currentRatio
                               );
                left = (screen.Bounds.Left / currentRatio + (screen.Bounds.Width / currentRatio - tempWidth) / 2);
                top  = (screen.Bounds.Top / currentRatio + (screen.Bounds.Height / currentRatio - tempHeight) / 2);
            }
            else
            {
                log.InfoFormat("Set window on extend screen, screen: left={0}, top: {1}, width: {2}, height: {3}, ratio: {4}"
                               , screen.Bounds.Left, screen.Bounds.Top, screen.Bounds.Width, screen.Bounds.Height, currentRatio
                               );
                left = (screen.Bounds.Left * currentRatio + (screen.Bounds.Width * currentRatio - tempWidth) / 2);
                top  = (screen.Bounds.Top * currentRatio + (screen.Bounds.Height * currentRatio - tempHeight) / 2);
            }

            window.Left = left;
            window.Top  = top;
            log.InfoFormat("window is centered, left: {0}, top: {1}, set value: {2} - {3}", window.Left, window.Top, left, top);
        }
Exemplo n.º 2
0
 private static Screen GetSuitableScreen(IMasterDisplayWindow masterWindow)
 {
     if (System.Windows.Forms.Screen.AllScreens.Length == 1)
     {
         return(System.Windows.Forms.Screen.PrimaryScreen);
     }
     else
     {
         // there are more than one screens.
         Screen   rejectiveWinScreen = DpiUtil.GetScreenByHandle(masterWindow.GetHandle());
         Screen[] screens            = System.Windows.Forms.Screen.AllScreens;
         foreach (System.Windows.Forms.Screen s in screens)
         {
             if (!s.DeviceName.Equals(rejectiveWinScreen.DeviceName))
             {
                 return(s);
             }
         }
     }
     return(null);
 }
Exemplo n.º 3
0
        public static System.Windows.Forms.Screen SetWndOnSuitableScreen(Window window, IMasterDisplayWindow masterWindow)
        {
            log.InfoFormat("set screen for window: {0}", window.Title);
            Screen screen = GetSuitableScreen(masterWindow);

            if (screen == null)
            {
                return(null);
            }
            log.InfoFormat("target screen is {0}", screen.DeviceName);
            //IntPtr handle = new WindowInteropHelper(window).Handle;

            Screen currentScreen = DpiUtil.GetScreenByPoint((int)window.Left, (int)window.Top);

            if (screen.Equals(currentScreen))
            {
                // already in the suitable screen, do nothing.
                screen = currentScreen;
            }
            log.DebugFormat("current screen is {0}", currentScreen.DeviceName);

            System.Windows.Forms.Screen masterWindowScreen = DpiUtil.GetScreenByHandle(masterWindow.GetHandle());
            double currentRatio      = 1;
            double primaryRatio      = 1;
            uint   currentScreenDpiX = 96;
            uint   currentScreenDpiY = 96;
            uint   primaryDpiX       = 96;
            uint   primaryDpiY       = 96;

            try
            {
                DpiUtil.GetDpiByScreen(screen, out currentScreenDpiX, out currentScreenDpiY);
                currentRatio = (double)currentScreenDpiX / 96;

                DpiUtil.GetDpiByScreen(System.Windows.Forms.Screen.PrimaryScreen, out primaryDpiX, out primaryDpiY);
                primaryRatio = (double)primaryDpiX / 96;
            }
            catch (DllNotFoundException e)
            {
                log.ErrorFormat("Can not load windows dll: {0}", e);
            }


            log.DebugFormat("```````1```{0}, {1}, {2}, {3}", window.Left, window.Top, window.Width, window.Height);


            if (masterWindow.GetWindow().WindowState == WindowState.Minimized)
            {
                masterWindow.GetWindow().WindowState = WindowState.Normal;
            }

            double tempWidth  = window.Width;
            double tempHeight = window.Height;

            if (currentScreen.DeviceName.Equals(System.Windows.Forms.Screen.PrimaryScreen.DeviceName))
            {
                log.DebugFormat("currentScreen in Primary");
                window.Left = (screen.Bounds.Left / primaryRatio + (screen.Bounds.Width / primaryRatio - tempWidth) / 2);
                window.Top  = (screen.Bounds.Top / primaryRatio + (screen.Bounds.Height / primaryRatio - tempHeight) / 2);
            }
            else
            {
                log.DebugFormat("currentScreen in extend");
                window.Left = (screen.Bounds.Left * currentRatio + (screen.Bounds.Width * currentRatio - tempWidth) / 2);
                window.Top  = (screen.Bounds.Top * currentRatio + (screen.Bounds.Height * currentRatio - tempHeight) / 2);
            }

            log.Info("Set window on suitable screen end.");
            return(screen);
        }