Develop Windows programs supporting multiple monitors

Source: Internet
Author: User

Microsoft provides some new API calls to support multi-monitor mode. Their functions are described as follows:

1. hmonitor monitorfrompoint (point PT, DWORD dwflags)
Monitorfrompoint returns a display handle containing a specific point (PT. If Pt does not belong to any monitor, the returned display handle is determined by the dwflags flag:
Monitor_defaulttonull returns NULL
When monitor_defaulttoprimary is returned, the hmonitor handle representing the primary monitor is returned.
Monitor_defaulttonearest returns the hmonitor handle of the display closest to the PT point.

2. hmonitor monitorfromrect (lpcrect LPRC, DWORD dwflags)
Monitorfromrect returns the display handle containing the rectangle represented by LPRC; if more than one display area contains this rectangle, the display handle containing the largest part of the rectangle is returned; if the rectangle does not belong to any display area, the returned handle is determined by dwflags:
Monitor_defaulttonull returns NULL
When monitor_defaulttoprimary is returned, the hmonitor handle representing the primary monitor is returned.
Monitor_defaulttonearest returns the hmonitor handle of the display closest to the rectangle.

3. hmonitor monitorfromwindow (hwnd, DWORD dwflags)
Similar to monitorfromrect, the input is a handle hwnd representing the window rather than a pointer pointing to the rectangle.

4. bool getmonitorinfo (hmonitor, lpmonitorinfo lpmi)
Getmonitorinfo returns information about the display represented by hmonitor, which is stored in lpmi, a pointer to the monitorinfo structure.

Typedef struct tagmonitorinfo

{

DWORD cbsize;

Rect rcmonitor;

Rect rcwork;

DWORD dwflags;

} Monitorinfo, * lpmonitorinfo;

This information includes the size of the display area of the display in the rect structure (if the display is not the main display, the rect coordinates may be negative ), and the size of the display's work area in the rect structure. The work area is the remaining area in the display area except the system taskbar and the application shortcut bar, you can also determine whether the monitor is the primary monitor and return a flag (determine whether dwflags contains monitorinfof_primary to determine whether it is the primary Monitor ).

5. bool enumdisplaymonitors (HDC, lpcrect lprcclip, monitorenumproc lpfnenum, lparam dwdata)
HDC is a handle that shows the device environment, and lprcclip is a pointer to a rectangular area. Intersection the rectangular area and the visible area in the device environment, and the obtained area may be distributed in the display area of multiple monitors, enumdisplaymonitors calls a monitorenumproc function once for each display area that contains an intersection. Dwdata is the data passed to the monitorenumproc function.

6. bool callback monitorenumproc (hmonitor, HDC hdcmonitor, LPRC lprcmonitor, DWORD dwdata)
Monitorenumproc is a callback function called by the enumdisplaymonitors function. Its content can be customized by users. Using these two functions, you can use different display features of each monitor when performing cross-display across multiple monitors.
Of course, not all plotting programs must call these two functions. In this case, assume that all displays use the same color resolution.

7. enumdisplaydevices (lpvoid lpreserved, int idevicenum, display_device × pdisplaydevice, DWORD dwflags)
Enumdisplaydevices lists information about a display device (with idevicenum as the serial number) in the system. Compared with getmonitorinfo, the display corresponding to getmonitorinfo must be part of the Windows virtual desktop, and enumdisplaydevices can list information of all the monitors installed on the system in standalone display mode. The returned information is stored in the display_device structure, including the display device name, description of the display device, and status of the display device.

Examples of the above APIs:
1. Determine whether the window is in the valid coordinate:
Hmonitor = monitorfromwindow (m_hwnd, monitor_defaulttonull );
If hmonitor is null, the window is not on any monitor, and the position of the window cannot be seen.
Even if the hmonitor handle is valid, the window may be blocked behind the taskbar, so you need to continue to get the work area of the screen where the window is located to determine whether the window is visible.
If (hmonitor)
{
Monitorinfo Info;
Info. cbsize = sizeof (monitorinfo );
If (getmonitorinfo (hmonitor, & info ))
{
// Obtain the window coordinate and info. rcwork to check whether the window coordinate is valid.
}
}

2. Determine whether the screen where the rectangle is located is the main display:
Hmonitor = monitorfromrect (& rctitle, monitor_defaulttonull );
If (hmonitor)
{
Monitorinfo Info;
Info. cbsize = sizeof (monitorinfo );
If (getmonitorinfo (hmonitor, & info) & info. dwflags & monitorinfof_primary)
{
// Most areas of the rectangle are in the main display.
}
}


Notes:
1. the work area obtained through systemparametersinfo (spi_getworkarea, 0, (pvoid) & rcworkarea, 0) is the work area of the primary monitor, if the window needs to support multiple monitors, it is not easy to calculate the work area. The alternative solution is to use getsystemmetrics, by using the sm_xvirtualscreen, sm_yvirtualscreen, sm_cxvirtualscreen, and sm_cyvirtualscreen parameters, you can obtain the coordinates and the length and width of the entire upper-left corner of the virtual desktop (the virtual desktop uses multiple monitors as one desktop ).

2. windows with negative coordinates or greater than sm_cxscreen and sm_cyscreen under a single monitor will be hidden, which are valid in Multi-monitor mode, therefore, we recommend that you do not directly use the () and (cxscreen, cyscreen) regions to determine the window position when using multiple monitors. Instead, you should select the virtual desktop coordinates mentioned above.


3. Check the validity of these window coordinates Before restoring the originally stored window positions. For example, you can use the above-mentioned judgment to determine whether the display is displayed in a display, whether it is in the working area of the display, and whether it is within the reasonable range of virtual desktop coordinates.


4. Some Windows API calls only process a single monitor. You must consider multiple monitors during the call. For example, if you use centerwindow (cwnd: getshorttopwindow () to set a window center screen, the screen is centered on the main screen, if you need to consider that the main window in the secondary display shows that the pop-up window is centered in the secondary display, you need to add the necessary processing, and no centerwindow input parameters are found to center it in the secondary display, this method is to center the main window in the secondary monitor or manually calculate the Center Coordinate setwindowpos in the secondary monitor.


5. (This is a summary of the current phenomenon and further verification is required.) When switching the display mode, the display handle changes, even if the display is not changed, the handles obtained through monitorfrom ***** may also be different. If the toolwindow attribute is not set in the window, the system will automatically change the position of the window to adapt to resolution or display changes when switching the display mode (whether switching between two screens or changing the resolution of a single screen, this location change will not cause the onwindowposchanging message. In the onwindowposchanged log, the lpwndpos-> flags is 0x815, that is, swp_nosize & swp_nozorder & swp_noactivate & 0x800.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.