Determine the screen on which the Window is located and the Window screen
Recently, a function is required to maximize the Window. If multiple monitors are used, the function is required to maximize the current display. Because the Window size is set based on the screen length and width, WindowState is not used. maximized, window. left does not know the value of this setting. Later, when debugging, we found that the X coordinates of the non-primary monitor were based on the primary screen. For example, if 2nd are the primary screen, the first screen X is-1600.
Foreach (System. windows. forms. screen screen in System. windows. forms. screen. allScreens) {if (winLeft> = screen. bounds. left & winLeft <= screen. bounds. left + screen. bounds. width) {return screen;} return System. windows. forms. screen. allScreens [0];
Later I found a more concise answer.
IntPtr handle = new WindowInteropHelper (window). Handle; return System. Windows. Forms. Screen. FromHandle (handle );