?
Solved

GetWindowRect failed

Posted on 2008-10-14
6
Medium Priority
?
715 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

649 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