?
Solved

How to determine in which monitor an application is being displayed in?

Posted on 2005-02-25
14
Medium Priority
?
510 Views
Last Modified: 2013-12-03
I have dual monitors on my computer, and I want my application to be able to remember the location it was it and in which monitor it was displayed in.

In other words, when my application shuts down, I want it to save this information, so that when it starts backup again, it nows exactly where to display itself, and in which monitor to display in?

How can I determine which monitor the application is being displayed in?
0
Comment
Question by:Axter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 4
14 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 400 total points
ID: 13407710
You could use 'MonitorFromPoint()' or 'MonitorFromWindow()' to determine which monitor it is shown on. See also http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/monitor_5isz.asp ("Positioning Objects on Multiple Display Monitors") and the associated code sample at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/monitor_9t0w.asp ("Positioning Objects on a Multiple Display Setup"):

#include <windows.h>
#include "multimon.h"    

#define MONITOR_CENTER     0x0001        // center rect to monitor
#define MONITOR_CLIP     0x0000        // clip rect to monitor
#define MONITOR_WORKAREA 0x0002        // use monitor work area
#define MONITOR_AREA     0x0000        // use monitor entire area

//
//  ClipOrCenterRectToMonitor
//
//  The most common problem apps have when running on a
//  multimonitor system is that they "clip" or "pin" windows
//  based on the SM_CXSCREEN and SM_CYSCREEN system metrics.
//  Because of app compatibility reasons these system metrics
//  return the size of the primary monitor.
//
//  This shows how you use the multi-monitor functions
//  to do the same thing.
//
void ClipOrCenterRectToMonitor(LPRECT prc, UINT flags)
{
    HMONITOR hMonitor;
    MONITORINFO mi;
    RECT        rc;
    int         w = prc->right  - prc->left;
    int         h = prc->bottom - prc->top;

    //
    // get the nearest monitor to the passed rect.
    //
    hMonitor = MonitorFromRect(prc, MONITOR_DEFAULTTONEAREST);

    //
    // get the work area or entire monitor rect.
    //
    mi.cbSize = sizeof(mi);
    GetMonitorInfo(hMonitor, &mi);

    if (flags & MONITOR_WORKAREA)
        rc = mi.rcWork;
    else
        rc = mi.rcMonitor;

    //
    // center or clip the passed rect to the monitor rect
    //
    if (flags & MONITOR_CENTER)
    {
        prc->left   = rc.left + (rc.right  - rc.left - w) / 2;
        prc->top    = rc.top  + (rc.bottom - rc.top  - h) / 2;
        prc->right  = prc->left + w;
        prc->bottom = prc->top  + h;
    }
    else
    {
        prc->left   = max(rc.left, min(rc.right-w,  prc->left));
        prc->top    = max(rc.top,  min(rc.bottom-h, prc->top));
        prc->right  = prc->left + w;
        prc->bottom = prc->top  + h;
    }
}

void ClipOrCenterWindowToMonitor(HWND hwnd, UINT flags)
{
    RECT rc;
    GetWindowRect(hwnd, &rc);
    ClipOrCenterRectToMonitor(&rc, flags);
    SetWindowPos(hwnd, NULL, rc.left, rc.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}

0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 1600 total points
ID: 13407714
>How can I determine which monitor the application is being displayed in?
This is not really needed, different monitors have differents coordinates, so will be enough to remember position.
By example, if your app is maximized in the secondary monitor, maybe coordinates could be:
1024,0,2047,767 (may vary)
You can use WinAPI's GetWindowRect and SetWindowPos() functions to acomplish this.
0
 
LVL 30

Author Comment

by:Axter
ID: 13407760
>>This is not really needed, different monitors have differents coordinates, so will be enough to remember position.

I didn't think it would be that simple, but I'll test it out, to see if it works.

jkr,
If jaime_olivares's method doesn't work, I'll then try your posted method.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13407972
>I didn't think it would be that simple, but I'll test it out, to see if it works.
Well, I have designed a half dozen systems with 2 and 3 monitors...
0
 
LVL 30

Author Comment

by:Axter
ID: 13408006
>>Well, I have designed a half dozen systems with 2 and 3 monitors...

One of the reasons I didn't think it would be that simple is because applications that I have that remeber screen location and window size, always seem to popup on my main monitor.

I'll have to wait until I get home to test this out.
0
 
LVL 86

Expert Comment

by:jkr
ID: 13408741
The weird thing is that we both are right - the coodinate way will work also (*duh*, you should not give away your thinking cap for dry cleaning, this as a mental note to myself :o) and probably is more on point regarding your Q - my approach applies if you want to e.g. center something for the left monitor specifically.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13410571
>One of the reasons I didn't think it would be that simple is because applications that I have that remeber screen location and window size, always seem to popup on my main monitor.
That can't be true, windows in another monitor will not return a left-top corner of 0,0.
jkr's answer is more suitable for other cases. You have requested to remember position for your own computer, from last run. jkr's will be helpful if you want to launch an application in monitor 2 or 3 specifically, regarding previous session's position.
0
 
LVL 30

Author Comment

by:Axter
ID: 13410646
>>That can't be true, windows in another monitor will not return a left-top corner of 0,0.

It positively is true.
I just tested it again, just to be sure.

If you have a dual monitor, you can test this out with the QuickPost program.

If you close the QuickPost program, it will remember the window size, and location on the screen.
However, if I move it to the second monitor, and then close the QuickPost program, when I start the program back up again,  it opens in the primary monitor, and the position is offset by what it was in the secondary monitor.


However, I just tested NotePad, and I notice that it does remeber it's location, even if the location is in the Second monitor.

I will be creating my test program this afternoon, and I'll post result then.
0
 
LVL 30

Author Comment

by:Axter
ID: 13410785
I just finished a quick test program, and no matter what I did, it seem to work with the GetWindowRect/SetWindowPos combination.
I even maximized it on the second monitor, thinking that might throw off the logic, but it still worked.

I'm not sure what code logic in the QuickPost program is using, to get it to come up on the wrong monitor.

Thanks jaime_olivares for the easy solution.

And thanks jkr, if I ever need centering logic, I'll keep your code in mine.
0
 
LVL 86

Expert Comment

by:jkr
ID: 13411762
>>the QuickPost program

How unsportsmanlike *shudder* :-(
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13411910
Thanks for the points, Axter.
I think quickpost must tie to the first monitor to put the ticker there... just a suposition.
0
 
LVL 30

Author Comment

by:Axter
ID: 13412045
>>How unsportsmanlike *shudder* :-(

I plan to create my own version of the QuickPost program.

I actually started it in Java, but I finally got tire of Java, and now I plan to do it in MFC.

The more I learn about Java, the more I start to agree with what one expert told me before: "Java was created just to piss off C++ programmers"
:-)
0
 
LVL 86

Expert Comment

by:jkr
ID: 13412822
>>I plan to create my own version of the QuickPost program

That's something I could live with better, yet I still would question using tools like QuickPost in general.

Meanwhile it's a bummer to learn that I to some extent am competing with a VB program. I know that there are no restrictions on using some "little herlper" SW to get notified about new Qs, but I consider that to be unfair - a browser and an email client should do, since this is the common basis everyone on EE shares.

Well, I know I am not the person to tell you what you should use or not, but I actually am a little disappointed that you think you'd really need something like that. That's not a move to increase my respect, and I really don't know what to think... except that this is particularly unfair to the not-so-regulars who so have an even smaller chance get a hold to a question.
0
 
LVL 30

Author Comment

by:Axter
ID: 13413037
I didn't start using QuickPost until Jan of this year.  And the main reason I used it, was because I just don't have time to visit each topic area to check for new questions.
If you notice, that before Jan, I only mainly stuck to the C++ topic area.  But now with the QuickPost, it's far easier for me to monitor multiple topic areas.

IMHO, EE should have such a feature already build into it, where you can select multiple topic areas that you want to check for new questions.

In any case, I don't consider this cheating in anyway, since any member can download QuickPost and use it, and I've advocate its use to other members.
If they don't choose to use it, that is not my problem, nor do I consider it unsportsmanlike just because I choose to use it.

>>but I actually am a little disappointed that you think you'd really need something like that.

Yes, I do feel I need it, because it helps me manage my time better, and it helps me help questioners that much faster.
Questioners don't have to sit and wait for half an hour or more for an answer, and instead can get a responds within minutes instead of hours.

>>That's not a move to increase my respect,
I'm sorry jkr, but I don't participate in EE to get anyone's respect.  I do it because I really enjoy it, and because I gain a lot of knowledge in the process.

>>except that this is particularly unfair to the not-so-regulars who so have an even smaller chance get
>>a hold to a question.
There's nothing to stop them from using QuickPost.
We're not talking about a secret program that only a few people have access to.
This program is free, and anyone can use it.

When I finish with my version of QuickPost, I will also try to encourage members to use it.  But if they don't use it, I'm not going to feel guilty over using it myself.

If you don’t want to use it, then that’s your choice, but your choice should not, and will not affect my choice.

That would be like me saying, I’m disappointed in you for not using QuickPost, and not doing everything you can to help questioners as expediently as possible.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

765 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