Use SetWindowPos
SetWindowPosition code fragment only sets the window position to the main display.
Understand the display of the display and the virtual screen
In the multi -display environment, the display has a specific layout and coordinates:
Main screen:
The upper left corner is located in the (0, 0) display. -
The left display: It has negative X coordinates. The original point of the screen is the main screen of the screen to minus the sum of the front screen width.
- Right display: There is a positive X coordinate, the original point of the screen is the main screen of the screen and the sum of the front screen width including the main screen.
- Calculating the screen coordinates
To set the window position on a specific display, we need to calculate the correct X coordinate based on the screen layout.
Use the Screen object
.NET Screen class provided several methods to determine the screen currently displayed by the window:
Screen.fromControl (Control)
Screen.fromhandle (Window Handle)
Screen.frompoint (Point) -
Screen.fromRctangle (RECTANGLE)
-
These methods return the Screen object containing the largest part of the specified element.
- Retrieve the hmonitor's handle
.NET reference source display, you can obtain the HMONITOR handle of the Screen object by calling the - Gethashcode () method of the object. Alternatively, you can use native Win32 functions such as MonitorFromWindow or MonitorFrompoint to retrieve the Hmonitor handle.
Solution
Based on the understanding of the display layout and screen coordinates, you can modify the setwindowposition method as follows:
This updated method uses a monitor index as an additional parameter, and calculates the corresponding X coordinate based on the screen layout.
The above is the detailed content of How to Correctly Position Windows Across Multiple Monitors Using SetWindowPos?. For more information, please follow other related articles on the PHP Chinese website!