CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2009
    Posts
    8

    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....

  2. #2
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: CStatic GetClientRect Acts weird

    Quote Originally Posted by JitterJaw View Post
    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

  3. #3
    Join Date
    Aug 2009
    Posts
    8

    Re: CStatic GetClientRect Acts weird

    Quote Originally Posted by ProgramArtist View Post
    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

  4. #4
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: CStatic GetClientRect Acts weird

    Quote Originally Posted by JitterJaw View Post
    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

  5. #5
    Join Date
    Aug 2009
    Posts
    8

    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

  6. #6
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: CStatic GetClientRect Acts weird

    Quote Originally Posted by JitterJaw View Post
    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 ...

  7. #7
    Join Date
    Aug 2009
    Posts
    8

    Re: CStatic GetClientRect Acts weird

    Quote Originally Posted by ProgramArtist View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured