-
August 24th, 2009, 01:10 AM
#1
CStatic GetClientRect Acts weird
Hi all,
Here is a problem Im working on for a couple of days now:
I have my main dialog, and when a button is pressed in that main dialog, I call a function in another dialog and load some variables, without showing the dialog,or calling DoModal. In the second dialog, when executing the function the following code is executed:
m_st1.GetClientRect( &rectStaticClient );
where m_st1 is a CStatic control of a picture control. The width & height of the rect I get is zero. but when I call the same function from the second dialog I do get the right size. so my question is, do I have to DoModal in order to get the size of a picture control box ? or am Im missing something ?
Thanks to all answers....
-
August 24th, 2009, 02:40 AM
#2
Re: CStatic GetClientRect Acts weird
Originally Posted by JitterJaw
Hi all,
Here is a problem Im working on for a couple of days now:
I have my main dialog, and when a button is pressed in that main dialog, I call a function in another dialog and load some variables, without showing the dialog,or calling DoModal. In the second dialog, when executing the function the following code is executed:
m_st1.GetClientRect( &rectStaticClient );
where m_st1 is a CStatic control of a picture control. The width & height of the rect I get is zero. but when I call the same function from the second dialog I do get the right size. so my question is, do I have to DoModal in order to get the size of a picture control box ? or am Im missing something ?
Thanks to all answers....
The dialog window must be created before calling GetClientRect() of it or of it's members.
Code:
if (m_st1.GetSafeHwnd())
{
m_st1.GetClientRect( &rectStaticClient );
}
else
{
// not created yet.
}
Windows does not know the size of the controls before the dialog window has been created.
Btw: In a debug session you should get an assertion. Did you try to debug it?
With regards
Programartist
-
August 24th, 2009, 04:22 AM
#3
Re: CStatic GetClientRect Acts weird
Originally Posted by ProgramArtist
The dialog window must be created before calling GetClientRect() of it or of it's members.
Code:
if (m_st1.GetSafeHwnd())
{
m_st1.GetClientRect( &rectStaticClient );
}
else
{
// not created yet.
}
Windows does not know the size of the controls before the dialog window has been created.
Btw: In a debug session you should get an assertion. Did you try to debug it?
With regards
Programartist
Hi and thanks for your quick and practical answer,
I did try to debug it and did not get an assertion. I new to MFC, so Ill have to ask when exactly a picture control, which is part of a dialog is created, and if I would like to get the rectangle of this picturebox, how can I do it after all ?
Thanks
-
August 24th, 2009, 07:53 AM
#4
Re: CStatic GetClientRect Acts weird
Originally Posted by JitterJaw
Hi and thanks for your quick and practical answer,
I did try to debug it and did not get an assertion. I new to MFC, so Ill have to ask when exactly a picture control, which is part of a dialog is created, and if I would like to get the rectangle of this picturebox, how can I do it after all ?
Thanks
To retrieve the sizes of the controls write a handler for the WM_INITDIALOG message. During this message all controls are alredy created and you can call their member functions (in this case GetClientRect) succesfully.
Another possiblilty is during the DoDataExchange function. This is a virtual function alredy overwritten by the class wizzard.
In general keep in mind:
A MFC object which is derived directly or indirectly from CWnd has always a GetSafeHwnd() function available but can return NULL for the following reason: There is a two-step initialization: First the constructor is called (creation of the MFC object) and then later, this MFC object is attached to a windows (operating system) "object". If needed, the MFC object will create it's corresponding windows "object" ("window" with a handle in Win32 terms).
That's why: In unclear situations check whether GetSafeHwnd() returns a valide handle.
With regards
Programartist
-
August 24th, 2009, 08:22 AM
#5
Re: CStatic GetClientRect Acts weird
Thanks again,
but Im still a little bit confused, I thought the problem was that I was trying to access an object in a dialog box that wasnt ".domodal", so in any time Ill try to acess this picture control Ill get NULL, right ? how can I tell the second dialog just to initialize the objects, and not to show itself ?
Thanks for your help again...
JitterJaw
-
August 25th, 2009, 02:37 AM
#6
Re: CStatic GetClientRect Acts weird
Originally Posted by JitterJaw
Thanks again,
but Im still a little bit confused, I thought the problem was that I was trying to access an object in a dialog box that wasnt ".domodal", so in any time Ill try to acess this picture control Ill get NULL, right ? how can I tell the second dialog just to initialize the objects, and not to show itself ?
Thanks for your help again...
JitterJaw
I tried to tell you a more general answer. You can call GetClientRect and other window (in Win32 terms) relevant functions after a window (with a handle) has been created.
CDialog derived objects can be used either modal or modeless.
MSDN has got an article about here.
I recommend you to follow the link "Life cycle of a Dialog Box (MFC)" too.
DoModal encapsulates the creation of all the windows and controls, the showing, the updating, the closing of the dialog box. That's why "inside" DoModal all windows/controls are present with a valid handle.
If you use a modeless dialog (which means that you do not call DoModal) then you have to create the dialog first.
With regards
Programartist
Last edited by ProgramArtist; August 25th, 2009 at 02:38 AM.
Reason: typo ...
-
August 25th, 2009, 07:22 AM
#7
Re: CStatic GetClientRect Acts weird
Originally Posted by ProgramArtist
I tried to tell you a more general answer. You can call GetClientRect and other window (in Win32 terms) relevant functions after a window (with a handle) has been created.
CDialog derived objects can be used either modal or modeless.
MSDN has got an article about here.
I recommend you to follow the link "Life cycle of a Dialog Box (MFC)" too.
DoModal encapsulates the creation of all the windows and controls, the showing, the updating, the closing of the dialog box. That's why "inside" DoModal all windows/controls are present with a valid handle.
If you use a modeless dialog (which means that you do not call DoModal) then you have to create the dialog first.
With regards
Programartist
Thanks, I finally got it
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|