How to create CreateWindowEx () to create a window on a specific monitor?

I decided that I could use GetSystemMetrics (SM_CMONITORS) to query the number of monitors connected, but is there any way to control which monitor CreateWindowEx () uses for the window?

+4
source share
3 answers

Yes, for the arguments "x" and "y". Use EnumDisplayMonitors (pass two zeros) to list the monitors. The MonitorEnumProc callback gets RECT * in the rectangle of the monitor display. You will get a negative RECT.right if the monitor is to the left of your main one.

+7
source

Each monitor simply displays part of the desktop, so displaying a window on a specific monitor is moving the window to the part of the desktop displayed by that monitor. When you call CreateWindowEx (or CreateWindow), you can specify the x and y coordinates for the window, so displaying on a specific monitor simply means specifying the coordinates that fall within the area displayed by that monitor.

You can find the working areas for monitors in the system using GetMonitorInfo.

+4
source

The x and y parameters determine the location of the new window. This point can be located anywhere on the virtual screen (all rectangles of the monitor are combined).

If you want to create a window on the same monitor as another window, you can call MonitorFromWindow . Otherwise, you can list all monitors using EnumDisplayMonitors .

In any case, if you have an HMONITOR handle, you should then call GetMonitorInfo . Your x and y parameters must be a value inside the rcWork section in the monitor information structure. Usually you select values ​​that put your window in the center of this rectangle.

It is important to use the workarea rectangle rather than the full monitor rectangle, because you do not want your window to appear under the taskbar and other application panels always on top.

0
source

All Articles