第一个窗口始终在屏幕中打开鼠标所在的位置。如何设置它始终在主屏幕中打开

时间:2014-11-19 02:36:08

标签: c# wpf winforms multiscreen

我有一个多屏应用程序。然后我在App.xaml的代码隐藏中覆盖OnStartup方法,如下所示:

protected override void OnStartup(StartupEventArgs e)
{
   base.OnStartup(e);

   MainWindow mainWindow = new MainWindow ();

   Screen s1 = Screen.AllScreens[0];
   Rectangle r1 = s1.WorkingArea;
   mainWindow.Top = r1.Top;
   mainWindow.Left = r1.Left;
   mainWindow.Show();
}

但是当鼠标悬停时,窗口始终显示在屏幕上。如何将窗口设置为始终显示在主屏幕中?

2 个答案:

答案 0 :(得分:0)

我想出了这个属性WindowStartUpLoacation =“CenterScreen”就是原因。 然后我将其设置为“手动”,问题不再存在。

答案 1 :(得分:0)

它可以帮助你......


ScreensHandler.cs

    ...
    public class ScreensHandler
    {
        #region Attributes
        /// <summary>
        /// All information about all screen on a single system
        /// </summary        
        private ScreensInformations _screensInformation;
        /// <summary>
        /// Property all information about all screen on a single system
        /// </summary>        
        public ScreensInformations ScreensInformation
        {
            get
            {
                if (this._screensInformation == null)
                {
                    this._screensInformation = new ScreensInformations();
                }
                return this._screensInformation;
            }
        }

        /// <summary>
        /// Store the log when we call the method this.place_onscreen
        /// </summary>
        public string Log_place_onscreen { get; set; }
        #endregion

        #region Constructor
        public ScreensHandler()
        {

        }
        #endregion

        #region Place window on screen
        /// <summary>
        /// Place the window WindowHandle on the screen number index_screen
        /// </summary>
        /// <param name="WindowHandle">The window handle to place</param>
        /// <param name="index_screen"></param>
        /// <returns></returns>
        public bool place_onscreen(IntPtr WindowHandle, int index_screen)
        {
            bool isSuccess = false;
            if (index_screen < this.ScreensInformation.NumberScreen)
            {
                if(this.ScreensInformation.Screens[index_screen] != null) 
                {
                    Screen current_screen = this.ScreensInformation.Screens[index_screen];
                    Rectangle current_screen_WorkingArea = current_screen.WorkingArea;

                    isSuccess = SetWindowPos(
                        WindowHandle,
                        HWND_TOP, // HWND_TOP = Places the window at the top of the Z order.
                        current_screen_WorkingArea.Left,
                        current_screen_WorkingArea.Top,
                        current_screen_WorkingArea.Width,
                        current_screen_WorkingArea.Height
                        ,0);
                    // SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE

                    this.Log_place_onscreen =
                          "   ---{SUCESS}-----------------------------------------\n"
                        + "   | Place WindowHandle(#" + WindowHandle + ") on screen(#" + index_screen + ") \n" 
                        + "   | Device name : " + current_screen.DeviceName.ToString() + "\n"
                        + "   | Working area : " + current_screen.WorkingArea.ToString() + "\n"
                        + "   | Primary ? : " + current_screen.Primary.ToString() + "\n"
                        + "   ----------------------------------------------------\n";
                } 
            }
            else 
            {
                this.Log_place_onscreen =
                      "   ---{FAILED}-----------------------------------------\n"
                    + "   | The screen with index (#" + index_screen + ") doesn't exist \n"
                    + "   ----------------------------------------------------\n";
            }
            return isSuccess;
        }

        /// <summary>
        /// Use to go in full screen mode
        /// </summary>
        /// <param name="hWnd"></param>
        public void chrome_on_fullscreen(IntPtr hWnd)
        {
            SetForegroundWindow(hWnd);
            SendKeys.SendWait("{F11}");
        }

        #region Methods DllImport (user32.dll)
        /// <summary>
        /// Changes the size, position, and Z order of a child, pop-up, or top-level window. These windows are ordered according to their appearance on the screen. The topmost window receives the highest rank and is the first window in the Z order.
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="hWndInsertAfter"></param>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        /// <param name="cx"></param>
        /// <param name="cy"></param>
        /// <param name="wFlagslong"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        static extern bool SetWindowPos(IntPtr hwnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int wFlagslong);
        const short SWP_NOSIZE = 0x0001;
        const short SWP_NOMOVE = 0x0002;
        const int SWP_NOZORDER = 0x0004;
        const int SWP_SHOWWINDOW = 0x0040;
        const int SWP_NOACTIVATE = 0x0010;
        const int HWND_TOP = 0;

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetForegroundWindow(IntPtr hWnd);
        #endregion

        #endregion
    }

ScreensInformations.cs

    ....
    /// <summary>
    /// Give all information about all screen on a single system
    /// </summary>
    public class ScreensInformations
    {
        #region Attributs
        /// <summary>
        /// Use to represents a display device or multiple display devices on a single system (Namespace System.Windows.Forms)
        /// </summary>              
        public Screen[] Screens { get; set; }

        /// <summary>
        /// Get the number of screen
        /// </summary>
        public int NumberScreen
        {
            get
            {
                return this.Screens.Length;
            }
        }
        #endregion

        #region Constructor
        public ScreensInformations()
        {
            this.Screens = Screen.AllScreens;
        }
        #endregion

        #region Methods
        public override string ToString()
        {
            StringBuilder temp = new StringBuilder();
            int index = 1;
            temp.Append(" Number screens detected = "+this.NumberScreen+"\n");
            foreach (Screen screen in this.Screens)
            {
                // For each screen, add the screen properties to a list box.
                temp.Append("\n");
                temp.Append(" > SCREEN " + index + "\n");
                temp.Append("   ---------------------------------------------------------\n");
                temp.Append("   |    Device Name : " + screen.DeviceName + "\n");
                temp.Append("   |         Bounds : " + screen.Bounds.ToString() + "\n");
                temp.Append("   |           Type : " + screen.GetType().ToString() + "\n");
                temp.Append("   |   Working Area : " + screen.WorkingArea.ToString() + "\n");
                temp.Append("   | Primary Screen : " + screen.Primary.ToString() + "\n");
                temp.Append("   ---------------------------------------------------------\n");
                index++;
            }
            return temp.ToString();
        }
        #endregion
    }

这两个类ScreensHandler和ScreenInformation来自一个类似的应用程序(仅限C#中的CLI应用程序),能够通过解析给定的JSON文件在多个屏幕上启动Chrome。在这里(https://github.com/AOT-DEP-PADI/ChromeLauncher),您可以找到有关它的更多信息。

ChromeLauncher