Click here to Skip to main content
Click here to Skip to main content
Go to top

Repositioning Windows on Multiple Monitor Systems

, 6 Aug 2007
Rate this:
Please Sign up or sign in to vote.
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.

License

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

Share

About the Author

KarstenK
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 Pinmemberdxlee6-Oct-09 4:00 
QuestionWhat about RTL mode? Pinmember_Stilgar_8-Aug-07 1:35 
AnswerRe: What about RTL mode? PinmemberKarstenK8-Aug-07 1:55 
GeneralNice, but missing some PinmemberLuc Pattyn7-Aug-07 5:02 
AnswerRe: Nice, but missing some PinmemberKarstenK7-Aug-07 21:02 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 7 Aug 2007
Article Copyright 2007 by KarstenK
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid