Windows can map multiple monitors into virtual desktops, allowing us to use this to design easy-to-work applications. For example, Powerpoint fully utilizes the advantages of dual monitors (most laptops support it). It can play slideshows on one monitor and notes on another monitor, which can control the playback process and enable users to do business He was very skilled in his speech. So how do we develop such an application? This article will show you if you implement applications using multiple monitors using Delphi.
Windows also supports cloning display methods, and each monitor outputs the same content, which makes sense for some applications. Although some graphics cards also support two monitors, they are not truly multi-displays, but virtual high-resolution display modes (such as 2048×768 or 1024×1536), which displays the screen to two monitors through the graphics card. superior. Neither of these two display modes are zhongdian introduced in this article, and they are also very simple, so we will not repeat them again.
Windows supports up to 10 monitors, and Windows maps all monitors into a large virtual desktop. The display can be understood as a partial view of the desktop. These displays can be arbitrarily arranged in the display properties according to the physical location of the display. If the monitor is arranged irregularly, some parts of the virtual desktop may not be displayed on either monitor. In order not to make a form appear between two monitors, Windows uses a display as the main monitor. When the computer is started, the login dialog box is displayed in the main monitor. Most program startup displays will be displayed in the main monitor.
According to the above introduction, it is not difficult to find several important concepts: desktop, monitor, main monitor, etc. First, we must first figure out these concepts and their previous relationship. This is the key to mastering the development method of multi-display application. Once you understand these concepts, the other parts will be very easy to understand.
A desktop actually refers to the logical area that Windows can display. In fact, a form can be displayed outside the desktop. However, this does not mean that all parts of the desktop will be displayed on a certain monitor (the reason is as mentioned before); but conversely, the content displayed by any monitor must be part of the desktop.
A desktop is a rectangular area that describes the size of the desktop by its vertex coordinates (Top, Left) and width and height. Why do you still need vertex coordinates? Because the vertex coordinates are not taken for granted (0,0). So where is (0,0)? It's a long story, so let's first review the concept mentioned just now - the main monitor. Windows wants the general program to be displayed on the main monitor at the beginning, because people are used to paying attention to a monitor closest to them. Windows cannot force users to use the leftmost monitor as the main monitor, so that applications need to worry about calculations in order to display themselves on the main monitor. However, most users only have one monitor (two monitors take up too much space), and ordinary applications do not want to spend a lot of effort to calculate where the main monitor is and where they should be displayed. So Windows proposes a reasonable solution: use the vertex coordinates of the main display as the origin of the coordinate system. In this way, ordinary programs need to consider problems in a single monitor environment.
The monitor is a partial view of the desktop. It’s like looking at the scenery outside the window through the window, standing in front of different windows, you can see different pictures. Similarly, the display is also a rectangular area, and its dimensions can also be described by vertex coordinates (Top, Left) and width and height. Vertex coordinates are relative to the origin of the desktop coordinate system, that is, to the vertices of the main display.
The concept of a workspace is relatively simple. It refers to a rectangular area in the monitor except for the task bar and other forms docked on the desktop.
Windows provides a set of APIs for the development of multi-display applications. VCL encapsulates these APIs and integrates them into the entire Framework very naturally, making it very easy to develop multi-display applications. The following is a related content.
The most familiar VCL is probably TCustomForm, which is the base class of all forms. The Position property of TCustomForm is used to set the actual position of the form. Two of its optional values are worthy of concern: one is the poScreenCenter. When the Position property is set to poScreenCenter, the form will be displayed in the center of the main display; One is poDesktopCenter. When the Position property is set to poDesktopCenter, the form is displayed in the center of the entire desktop. If this property is set to poDesktopCenter and the program runs on a system with multiple monitors, then this window will be displayed between the two monitors, causing unnecessary trouble to the user. Therefore, even if our program is not designed for multi-display, this value should be handled carefully. Another property is DefaultMonitor, which has a similar function to Position, which determines which monitor the window is initially displayed. It has four alternative values: dmDesktop, dmPRimary, dmMainForm, and dmActiveForm. Their meanings are as follows:
Value | Meaning |
dmDesktop | No special treatment |
dmPrimary | Display the form on the first monitor. This is another trap, literally the main monitor, and in fact it refers to the Screen.Monitor[0] monitor. |
dmMainForm | Display the form to the monitor where the main form is located |
dmActiveForm | Show the form to the monitor where the active form is located on the desktop |
TCustomForm also has a read-only common property (without Published) Monitor, which provides a reference to access the display instance where the form is located. This value is closely related to the DefaultMonitor.
So how do you move the form between different displays? This is not difficult, probably you thought of it too. Here are two methods:
First, you can set the Top and Left of TCustomForm to display the form anywhere on the desktop. As mentioned earlier, the desktop is made up of all monitors. They have a common coordinate system, so the position of the form can be determined based on the logical position of the display. The question now is how to get the logical position and size of each monitor, which will be introduced later.
Second, you can call the MakeFullyVisible method of TCustomForm to fully display the form to the specified display. This method can be used to prevent the window from displaying a part of each of the two monitors.
We just asked a question: how to get the logical position and size of each monitor. In order to answer this question, we need to introduce another category: TScreen and TMonitor.
TScreen describes some information related to display devices, and we are mainly concerned with information related to the logical position and size of the display. Other aspects can be found in Delphi's documentation. When the program is running, VCL automatically creates an instance of TScreen - a global variable, so usually the program does not need to instantiate TScreen.
TScreen has a set of properties shaped like Desktop*, which describe the size of the entire desktop and the coordinates of each vertex. There are also a number of properties that are of great significance to the development of multi-monitor applications: MonitorCount and Monitors. Through these two properties, we can enumerate all the instances of the monitors (TMonitors) in the system. Each instance reflects the relative position and resolution of the corresponding monitor (described in detail later).
Among the many properties of TScreen, we will find the two properties: Height and Width. Be especially wary of not referring to the size of the entire desktop, but to the height and width of the main display. This is very easy to make people feel illusioned that they cannot think of the size of the entire desktop. Similarly, there is a set of properties shaped like WorkArea*, which describe the size of the working area of the main display and the coordinates of each vertex. Do you think something is missing? Why are the properties of the relative position of the main monitor not getting? The reason is as mentioned earlier: Windows uses the upper left corner of the main monitor as the origin of the coordinate system, so the relative position of the main monitor must be (0, 0).
In addition to these properties, we will also introduce three member functions of TScreen: MonitorFromPoint, MonitorFromRect and MonitorFromWindow. As the name implies, they are instances of the display where a coordinate, a certain area, and a certain window are located. It may also be used in actual development.
Best, let’s take a look at the TMonitor class. It encapsulates the relevant properties of the physical display—all read-only. The following table briefly describes the meaning of these properties, which are very useful for writing multi-display applications:
property | illustrate |
Handle | Get the Windows handle of this monitor |
MonitorNum | Get the monitor number |
Primary | Gets whether the monitor is the main monitor. And the Primary with only one monitor is True. |
Top | Get the upper boundary of the monitor |
Left | Get the left border of the monitor |
Height | Get the height of the monitor |
Width | Get the width of the monitor |
BoundsRect | Get the corresponding desktop area of the monitor, which is equivalent to the above four properties |
WorkareaRect | Get the area corresponding to the desktop of the monitor's workspace. |
After a clear understanding of TScreen and TMonitor, the previous problems will naturally be solved. By this point, this article has covered all the knowledge needed to develop multi-display applications. I believe you can use this knowledge to develop very practical software products.
Appendix: You can download a DEMO to help understand this article.