Link to home
Start Free TrialLog in
Avatar of AndyAinscow
AndyAinscowFlag for Switzerland

asked on

Multi-monitor problem with CRect and CPoint

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?
ASKER CERTIFIED SOLUTION
Avatar of RichieHindle
RichieHindle

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AndyAinscow

ASKER

BOOL CMyEdit::HitButtonTest(CPoint ptTest)
{
      ASSERT(::IsWindow(GetSafeHwnd()));

      if(!IsButtonVisible())
            return FALSE;

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


AND

CRect CMyEdit::CalcButtonRect() const
{
      ASSERT(::IsWindow(GetSafeHwnd()));

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

      if(IsButtonVisible())
      {
            GetClientRect(rect);
            rect.left=rect.right;
            rect.right=rect.left+GetButtonWidth();
      }

      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;

                  if(HitButtonTest(CPoint(LOWORD(lParam),HIWORD(lParam))))
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.
Oh bleep
CPoint(GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam)))