Solved

GetWindowRect failed

Posted on 2008-10-14
6
666 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 31

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Creating Labels and Frequency list style reports in SAS code 9 92
Separate files macro - Errors on larger data set 18 65
only14 challenge 19 69
Change to event 1 101
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 …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

895 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

14 Experts available now in Live!

Get 1:1 Help Now