Link to home
Start Free TrialLog in
Avatar of userd
userdFlag for India

asked on

GetWindowRect failed

HI all,

   I have created MFC Dialog based application. I placed one button in invisible mode on dialog. In OnInitDialog handler, I am taking the button coordinates by using mybutton.GetWindowRect() function. I am placing the bitmap(CBItmapButton object) in the rectangle area returned by GetWindowRect(). This is fine on development machines and some machines.

  But the placement of bitmap is not proper on some different machine resolutions. How to fix this?

rough code:
      RECT rc;
                     CBitmapButton mycbitmapbutton;
      m_mybutton.GetWindowRect(&rc);
                      //m_mybutton is member variable and attached to button control on dialog
      mycbitmapbutton.Create(NULL, WS_VISIBLE | BS_OWNERDRAW| WS_EX_CLIENTEDGE, CRect( rc.left, rc.top  ,rc.left + 65 , rc.top+23), this,100);
      mycbitmapbutton.LoadBitmaps(IDR_BMP_PIC);

Regards,
UserD.
Avatar of Zoppo
Zoppo
Flag of Germany image

Hi userd,

you cannot call GetWindowRect (and a lot of other CWnd-functions) for CWnd-derived objects before they are created with 'Create' or 'CreateEx' (or before they are attached to an existing window).

So, you need to specify the rect anyhow.

ZOPPO
ASKER CERTIFIED SOLUTION
Avatar of bobarctor
bobarctor
Flag of France image

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
I concur with bobarctor; the issue is global/scrren coordinats vs local/client coordinates (the rc.left+65 , rc.top+23 is the giveaway).  Many beginners make everything fullscreen and then think that the two coordinate sets are identical.  
Avatar of radeus
radeus

Dan and bobarctor are right- you have to use ScreenToClient before using the CRect. It should go like this:

RECT rc;
CBitmapButton mycbitmapbutton;
m_mybutton.GetWindowRect(&rc);
ScreenToClient(rc);
mycbitmapbutton.Create(NULL, WS_VISIBLE|BS_OWNERDRAW|WS_EX_CLIENTEDGE, rc, this, 100);
mycbitmapbutton.LoadBitmaps(IDR_BMP_PIC);

Open in new window

Try ::ScreenToClient();