Solved

GetWindowRect failed

Posted on 2008-10-14
6
651 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:userd
6 Comments
 
LVL 30

Expert Comment

by:Zoppo
ID: 22709543
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
0
 

Accepted Solution

by:
bobarctor earned 125 total points
ID: 22777231
hi,

does it work only when your parent dialog is maximized ?

It would make sense :
- you retrieve the coordinates of  m_mybutton using method CWnd::GetWindowRect, which returns screen coordinates
- you set coordinates to mycbitmapbutton using CButton::Create, which expects client coordinates (relative to the parent window you pass as 4th parameter : this)
=> it would be normal if your mycbitmapbutton is too much on the right/bottom.

Before setting to mycbitmapbutton the coordinates retrieved from m_mybutton, you should convert these coordinates to client coordinates of you parent dialog by using this->ScreenToClient (&rect).

bobarctor.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 22854007
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.  
0
 

Expert Comment

by:radeus
ID: 22887302
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

0
 
LVL 2

Expert Comment

by:etking
ID: 23896874
Try ::ScreenToClient();
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

12 Experts available now in Live!

Get 1:1 Help Now