Solved

GetWindowRect failed

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pressing cancel button with C# UI Automation 3 101
Problem to open Excel file 15 255
Is there a simple front-end menu system. 9 141
Problem to Office 1 63
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

740 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