Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / MFC

Repositioning Windows on Multiple Monitor Systems

Rate me:
Please Sign up or sign in to vote.
2.20/5 (6 votes)
6 Aug 2007CPOL2 min read 45.8K   1.7K   14   5
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:

C++
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.

C++
//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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
I am living in germany and now living from programming for some Years. In my spare time I like sports as jogging, playing football (soccer) and basketball.

We must take care for our planet, because we and our family has no other. And everybody has to do something for it.

Comments and Discussions

 
GeneralMy vote of 1 Pin
dxlee6-Oct-09 4:00
dxlee6-Oct-09 4:00 
QuestionWhat about RTL mode? Pin
_Stilgar_8-Aug-07 1:35
_Stilgar_8-Aug-07 1:35 
Hi,

Can you please explain what will be the case when the OS, or maybe just the window are in RTL (right to left mode)? This should change some of the coordinates you're using.

Stilgar.
AnswerRe: What about RTL mode? Pin
KarstenK8-Aug-07 1:55
mveKarstenK8-Aug-07 1:55 
GeneralNice, but missing some Pin
Luc Pattyn7-Aug-07 5:02
sitebuilderLuc Pattyn7-Aug-07 5:02 
AnswerRe: Nice, but missing some Pin
KarstenK7-Aug-07 21:02
mveKarstenK7-Aug-07 21:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.