Multi-monitor problem with CRect and CPoint

Posted on 2007-09-28
Last Modified: 2013-12-14
I've just been alerted to some wierd behaviour (bug in op system / MFC code ?)

I have a user that has a two monitor system (in Europe) and, for some reason, has the primary monitor defined as the monitor on the right and the secondary monitor is on the left.  So the system thinks the top left of the primary monitor is 0, 0
This, of course, means the co-ordinates on the left hand monitor are 'strange'.

I am performing a hit test on a control drawn into the NC area of an edit box.  It isn't responding to the mouse.  basically       return rectButton.PtInRect(ptTest);
rectButton is a CRect and ptTest is a CPoint.
The value in ptTest has an x-co-ordinate of 64786 and in the rectButton the l, r is -761, -744
So the PtInRect call is failing - despite the mouse being in the rectangle the return is false.

Is there a flag/function to make it compare the signed/unsigned values correctly or do I have to code some sort of hack?
Question by:AndyAinscow
    LVL 14

    Accepted Solution

    The X values for the left-hand monitor are negative, which MFC should cope with.  Is it possible that somewhere you are stripping the sign, by using an unsigned variable to store an intermediate value, for instance?  Where is ptTest coming from?  (Show us some code!)
    LVL 43

    Author Comment

    BOOL CMyEdit::HitButtonTest(CPoint ptTest)

                return FALSE;

          CRect rectButton=CalcButtonRect();
          return rectButton.PtInRect(ptTest);


    CRect CMyEdit::CalcButtonRect() const

          CRect rect(0,0,0,0);


          return rect;

    AND in the WindowProc of the custom edit

    LRESULT CMyEdit::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
          case WM_NCLBUTTONDOWN:
                      LRESULT lResult=CEdit::WindowProc(message, wParam, lParam);
                      if(!IsWindowEnabled() || (GetStyle()&ES_READONLY))
                            return lResult;

    LVL 43

    Author Comment

    I think the problem is the CPoint and CRect are using LONG variables internally.
    I suspect I am going to have to do a hack.
    LVL 43

    Author Comment

    Oh bleep

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
    Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project ( automates most of the tasks discussed in this article. You can even fin…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

    734 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

    22 Experts available now in Live!

    Get 1:1 Help Now