📜 ⬆️ ⬇️

Work with monitors via WinAPI

Opportunities

Using the classes below, you can get information about the number of monitors, their parameters, as well as set work areas.

Why do you need

In particular, I needed this functionality to create my own dashboard in WPF, like a taskbar. Collected information on the Internet and designed in a convenient form.

Using

In our program we write:
DisplayDispatcher dispatcher = new DisplayDispatcher(); 

')
This automatically searches for monitors and determines their parameters. Print the data in the ListBox:
 for (int i = 0; i < dispatcher.Count; i++) lbMonitors.Items.Add(String.Format(" #{0} ({1}x{2}) {3}", i, dispatcher[i].ScreenWidth, dispatcher[i].ScreenHeight, ((dispatcher[i].Availability) ? " " : ""))); 


In addition to the width and height, data on the occupied area on the virtual desktop (MonitorArea) and the workspace on it (WorkArea) are also available.

Now, for example, let's change the working area of ​​the main monitor:
 Rect wa = new Rect { left = 0, right = 0, top = 500, bottom = 500 }; dispatcher.GetMainDisplay().ApplyNewWorkArea(wa); 


If you then try to maximize any window on the main monitor, it will fit into a rectangle (0; 0) - (500; 500).

A bit of theory

The monitor, whose workspace will be changed, is determined automatically. In order to change the area of ​​a specific monitor, the new dimensions must fit into the occupied position on the virtual desktop. Those. WorkArea must be entered in the MonitorArea.
Specifically, in the proposed code, the working area is set by referring to the desired monitor, and the check is carried out before calling WinAPI functions.

To extend WinAPI functionality, I recommend the site www.pinvoke.net .

Download sources (VS2010)

In addition to the one described in the program, you can outline the current work area of ​​the selected monitor.

PS I did not find the answer whether it is possible to cause automatic redrawing of active windows, taking into account the new size of the workspace, I will be grateful if someone writes how to do it (without using window enumeration through EnumWindows).

Source: https://habr.com/ru/post/116886/


All Articles