Multi-screen display programming (3)

zhaozj2021-02-08  279

Programming for Multiple Monitors in Windows 98New Multiple-Monitor Win32 API Functions

Continued from Installing Multiple Monitors

In order to give applications access to multiple-monitor information, Microsoft has added several new Win32 API functions and updated several existing functions. In addition to new functions, there is also a new type of handle, the HMONITOR, which represents a physical display device ..........................

The First of the New Functions We Will Discuss Is MonitorFromPoint, Which Has The Following Definition:

HMonitor MonitorFromPoint (Point Pt,

DWORD DWFLAGS;

The returned HMONITOR is the monitor that contains the POINT passed to the function If no monitor contains the specified point, then the return value of MonitorFromPoint is determined by dwFlags, for which there are three possible values:. MONITOR_DEFAULTTONULL returns NULL if the point is not Contained by Any Monitor, Monitor_Defaulttoprimary Returns The Primary Display Device, And Monitor_Defaulttonearest Returns the Monitor Closest to the point.

HMonitor MonitorFromRect (LPRECT LPRC,

DWORD DWFLAGS;

HMonitor MonitorFromWindow (hwnd hwnd,

DWORD DWFLAGS;

MonitorFromRect and MonitorFromWindow return the handle of the monitor that contains the largest portion of the specified rectangle or window. If no monitor contains the specified rectangle or window, the return value depends upon the value of the dwFlags field, which has the same possible values ​​for .................

Once you have obtained an HMONITOR, you can use the function GetMonitorInfo to retrieve a MONITORINFO structure filled with data relevant to the specified monitor, as follows: BOOL GetMonitorInfo (HMONITOR hmonitor, LPMONITORINFO lpmi);

Typedef struct tagmonitorinfo

{

DWORD CBSIZE;

Rect rcmonitor;

Rect rcwork;

DWORD DWFLAGS;

Monitorinfo, * lpmonitorinfo;

Typedef struct tagmonitorinfoexa

{

Monitorinfo;

Tchar szdevice [cchdevicename];

} MonitorinfoEx, * lpmonitorinfoEx;

There are actually two versions of this structure, MONITORINFO and MONITORINFOEX. The cbSize member contains the size of the structure, and it is used by Windows to determine whether a structure is a MONITORINFO or MONITORINFOEX. After a successful call to GetMonitor, the rcMonitor member contains the rectangle of the monitor within the virtual desktop. The rcWork member contains the work area of ​​the monitor within the virtual desktop. (A monitor's work area is the portion not covered by the taskbar.) The dwFlags member has only one possible value, MONITORINFOF_PRIMARY, which is set if the monitor is the primary display. The MONITORINFOEX structure has one additional parameter, szDevice, which contains the unique name of the device. The device name can be passed to other functions, such as ChangeDisplaySettingsEx, allowing the settings for Each Monitor to Be Changed Independently.

Before you pass GetMonitorInfo a pointer to a MONITORINFO or MONITORINFOEX structure, you must set the cbSize member to the size of the structure so that Windows can determine which structure is being used The following code sample shows how.:

MonitorinfoEx Mix;

Mix.cbsize = sizeof (mix);

Getmonitorinfo (HMonitor,

(Lpmonitorinfo) & mix);

Some changes were made to GetSystemMetrics to allow the use of multiple monitors. Passing SM_CXSCREEN and SM_CYSCREEN as parameters returns the x and y extents, respectively, of only the primary monitor. If you wish to get the x and y extents of the virtual desktop, use the new constants SM_CXVIRTUALSCREEN and SM_CYVIRTUALSCREEN; GetSystemMetrics will return the left and top coordinates of the virtual desktop, respectively you can no longer assume the top-left corner of the desktop is (0,0), and the extents will do you no. good if you do not know the origins. Passing the new constant SM_SAMEDISPLAYFORMAT to GetSystemMetrics will return TRUE if all of the monitors on the system have exactly the same color settings, FALSE otherwise. Use the constant SM_CMONITORS when you want to know how many monitors Make Up That Value Includes Only Installed Monitors That Have Been Added To The Desktop by The User.The SystemParametersInfo Function Has Also Been Changed SLI ghtly. The changes affect only the uiAction values ​​SPI_GETWORKAREA and SPI_SETWORKAREA. Using the former always returns the work area of ​​the primary monitor. If you want to get the work area of ​​another monitor, you need to use GetMonitorInfo. Using SPI_SETWORKAREA sets the work area Of The Monitor That Contains The Rect Structure Passed Into The PvParam Value.

ENUMDISPLAYMONITORS IS A New Function That Allows An Application To Optimize Its Drawing Code WHEN A WINDOW IS Being Partially Displayed On Multiple Monitors That Have Different Color-Bit DePths.

Bool WinApi EnumdisplayMonitors

HDC HDC,

LPCRect Lprcclip,

MonitorenumProc LPFNENUM,

LPARAM DWDATA);

The MONITORENUMPROC callback is called for each monitor that intersects the visible region of hdc and the lprcClip parameter. If the lprcClip parameter is NULL, then no additional clipping is performed. The dwData parameter is for user-defined data and is passed through to the dwData Parameter In The Callback Function. MonitorenumProc Is A User-Defined Callback Function That Must Have The Following Signature: Bool Callback MonitorenumProc

HMonitor HMonitor,

HDC HDCMonitor,

Lprc lprcmonitor,

DWORD DWDATA);

If the hdc passed to EnumDisplayMonitors was NULL, then hdcMonitor is NULL. Otherwise, hdcMonitor contains a valid device context whose color attributes match the display monitor defined by hmonitor. If hdcMonitor is NULL, then lprcMonitor is in virtual-desktop coordinates. It is important to note that no application needs to use EnumDisplayMonitors to handle monitors of differing bit depths, since Windows automatically dithers high-color images on lower-color devices. you should use this function only if you want to do custom dithering to ensure the best possible display .................. ...

ONE USE for EnumdisplayMonitors Is To Make Sures Are Drawn Optimally ON Lower-Color Devices. The Best Way To Do this is to change how your window's WndProc Handles the WM_Paint Message.

Case WM_Paint:

HDC HDC;

HDC = BeginPaint (hwnd,

& PaintStruct);

EnumdisplayMonitors (HDC, NULL, MONITORENUMPAINTPROC, 0);

Endpaint (& PaintStruct);

INSIDE YOUR Callback, You Query The Passed Device Context To Determine ITS Capabilities and Display Area and do your drawing acidingly.

You can also use EnumDisplayMonitors to query every monitor on the desktop. If the device context and RECT passed into EnumDisplayMonitors are both NULL, no clipping is performed and your callback function is called once for each monitor in the virtual desktop. The code in Figure 3 caches the work area of ​​every monitor for later use. (Note that if your application actually did this, you would need to update the cached information when the WM_DISPLAYCHANGE message was received, since the values ​​might change.)

Another Way to Query Display Devices Is The EnumdisplayDevices Function, Which Can Query All Devices Available on The System, WHETER OR NOT TheY're Part of The Virtual Desktop.

BOOL WINAPI ENUMDISPLAYDEVICES

PVOID Unused,

DWORD IDEVNUM,

Pdisplay_Device lpdisplayDevice,

DWORD DWFLAGS;

Unused is reserved for future use and must be set to NULL. There are no valid values ​​for dwFlags, so set it to 0. iDevNum is the index of the device you wish to query and is zero-based. If there is not a device available in the iDevNum position, EnumDisplayDevices returns FALSE. If there is a device in the position indicated by iDevNum, EnumDisplayDevices returns TRUE and fills in lpDisplayDevice. So to examine all devices on the system, you'd call EnumDisplayDevices for each possible index, starting WITH 0, Until It Returned False. The lpdisplayddevice parameter is a Pointer to a display_device structure, Which is defined as Follows:

Typedef struct _display_device {

DWORD CB;

Byte DeviceName [32];

Byte DeviceString [128];

DWord StateFlags;

} Display_Device, * pdisplay_device,

* Lpdisplay_device;

The cb member contains the size of the structure in bytes. DeviceName is the same unique name returned by GetMonitorInfo in the szDevice member of the MONITORINFOEX structure. DeviceString is a user-friendly description of the enumerated device. Unfortunately, I was not able to find Documentation on the Possible Values ​​of Stateflag, But They Seem Self-Explanatory. Here Are Their Definitions As listed in the win32 sdk: #define display_device

_Ttached_to_desktop

0x00000001

#define display_device

_Multi_Driver

0x00000002

#define display_device

_Primary_Device

0x00000004

#define display_device

_Mirroring_Driver

0x00000008

#define display_device

_VGA_COMPATIBLE

0x00000010

Since EnumDisplayDevices allows you to query devices that are not part of the desktop, your application can use monitors in an exclusive manner, without having to share the screen with the desktop. To do this, find a device that does not have the DISPLAY_DEVICE_ATTACHED_TO_DESKTOP bit set and call the Win32 API function CreateDC, passing the name returned in the DeviceName member of lpDisplayDevice. If you create a device context in this manner, you should delete it when you're done with it by calling DeleteDC.

Next: Other Issues

Published as power programming in the 4/7/98 Issue of pc magazine.

转载请注明原文地址:https://www.9cbs.com/read-1696.html

New Post(0)