?
Solved

GetWindowRect failed

Posted on 2008-10-14
6
Medium Priority
?
706 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

771 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