65.9K
CodeProject is changing. Read more.
Home

Repositioning Windows on Multiple Monitor Systems

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.20/5 (6 votes)

Jul 24, 2007

CPOL

2 min read

viewsIcon

46679

downloadIcon

1724

How to reposition Windows on multiple monitor systems.

Introduction

On systems with more than one display, you can get into trouble because of compatibility issues for functions like:

SystemParametersInfo( SPI_GETWORKAREA, 0, &rc, 0 );//only primary Monitor

Here is a possible setup for two displays: the left (1) is the standard monitor, and the right (2) is the extension monitor, for instance, for less needed information like help, e-mail, or browsing.

Screenshot - Multiscreen.jpg

Background

Last week, I got a problem of restoring a window on a computer which had multiple monitors. I looked for some samples, but found no good ones, so I wrote some code to solve the issue. And, I want to demonstrate my new knowledge and publish it here.

But there are other setups possible; it might look strange, for instance, to have the right monitor as the primary one. This means that the coordinates of the left monitor have negative values.

Screenshot - Multiscreen2.jpg

The taskbar

The taskbar also needs some space, so it is better that this area wont be used to show windows. The taskbar can have different positions. This is illustrated from the standard position (1) up to three different positions (2)-(4).

Screenshot - Multiscreen_with_Taskbar.jpg

Using the code

I packed all the interesting code in a designed class to concentrate it and for ease of use. Because of #define COMPILE_MULTIMON_STUBS, you got to pay some attention on how to use the class. If you find a better way, let me know it.

Now I have coded a global object, so you don't need an extra object, and the object is available if you include the files in your project.

//Fetch informations
int cnt ) gMonitor.GetCount(); 
int iMonitor = gMonitor.GetPrimary();

//test for repositioning
CRect rc;
GetWindowRect( rc );

iMonitor = gMonitor.GetNearestDisplay( rc ); //also overloaded for point

if( !gMonitor.EnsureInMonitor( rc, iMonitor ) )
    MoveWindow( rc );

Points of interest

This code can help you for a better user experience with multiple monitor systems, because a window will be moved if it doesn't fit on one physical screen.

It gets real tricky. When the primary display is the right one (or the upper?), then the left (top) has negative values and sizing needs some extra lines of code.

Also, watch out for the taskbar area, which is another pitfall.

History

  • Initial release: 24 July 2007.
  • Update: 07 August 2007: with pictures and the workarea extensions.