center window in current monitor

Posted on 2005-05-05
Last Modified: 2013-12-03
We are attempting to add minimal multi monitor support to our application. We have some windows that either are centered in our application, centered in the monitor, or take up the whole monitor.  I can get the position of our window in the secondary(or more) monitor, but I am not sure how to determine what offset to use for centering our window in the monitor.  I can fudge it by using GetWindowRect(GetDesktopWindow(),&rectDesktop); but that doesnt take into account different resolutions on different monitors.  Ideally there should be a way to get the the x,y of top left and bottom right of the monitor a window is in.  Our application needs to run on Win95,NT, and above although the multi monitor support does not need to function in 95/NT so I cannot imlicitly link with User32.lib and use GetMonitorInfor(), etc.
Question by:PhilC
    LVL 13

    Accepted Solution

    Why not just check the operating system and use GetMonitorInfo if it's available otherwise ignore it? I do this for lots of things (Printing, GDI, etc.). That way you have the best of both worlds - compatible + modern :)

    Author Comment

    man was I wayyy over thinking this....example on MSDN "Positioning Objects on a Multiple Display Setup"
    thank you
    LVL 11

    Expert Comment

    Actually, you can link implicitly.  The link to the DLL is not created until the first call that needs it, so if you just make sure never to call the multi-monitor functions on Win95 then you will have no problems.  And yes, I do have a program that does just this.  Here's the function to tell if multimon is supported:

            OSVERSIONINFO version;
            version.dwOSVersionInfoSize = sizeof(version);
            GetVersionEx (&version);
            if ( // Check for NT
                ((version.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
                 (version.dwMajorVersion <= 4)) ||
                // Check for Win95
                ((version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) &&
                 (version.dwMajorVersion == 4) &&
                 (version.dwMinorVersion == 0))
                m_bMultimon = false;
                m_bMultimon = true;

    Hope this helps.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
    This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now