Usage of GetDlgItem 
Author Message
 Usage of GetDlgItem

I am trying access a CButton object so I can control enabling & disabling.  I
have 2 questions:

1.  Where in the project can I use GetDlgItem(int) vs. GetDlgItem(HWND, int)?
I can't use the first call in some cases and

2. The second gives me problems (getting a valid HWND).  For example, the code
below fails on the ASSERT, even though hd is valid after the GetActiveWindow()
call.

        HWND hd = ::GetActiveWindow();
        CButton* ptCACI = (CButton*)GetDlgItem( hd, IDC_SUBSCRIBERCACI);
        ASSERT(ptCACI != NULL);  // FAILS ON THIS ASSERT
        ptCACI->EnableWindow(TRUE);



Mon, 27 Oct 2003 02:23:44 GMT  
 Usage of GetDlgItem

Quote:
>1.  Where in the project can I use GetDlgItem(int) vs. GetDlgItem(HWND, int)?
>I can't use the first call in some cases and

>2. The second gives me problems (getting a valid HWND).  For example, the code
>below fails on the ASSERT, even though hd is valid after the GetActiveWindow()
>call.
>    HWND hd = ::GetActiveWindow();
>    CButton* ptCACI = (CButton*)GetDlgItem( hd, IDC_SUBSCRIBERCACI);
>    ASSERT(ptCACI != NULL);  // FAILS ON THIS ASSERT
>    ptCACI->EnableWindow(TRUE);

Richard,

The window you supply to GetDlgItem is the window that is the parent
of the control.

For an MFC application you would normally call CWnd::GetDlgItem from a
member function of the control's parent window (usually a dialog box).
It's unlikely that the control's parent window would be the active
window, so that's why your example is failing.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.



Mon, 27 Oct 2003 03:23:16 GMT  
 Usage of GetDlgItem

Quote:

> 1.  Where in the project can I use GetDlgItem(int) vs. GetDlgItem(HWND, int)?
> I can't use the first call in some cases and

Call the first form from within a method of a CWnd or class derived thereof.
Remember the object in question must already be ::Creat()ed.

Quote:
> 2. The second gives me problems (getting a valid HWND).  For example, the code
> below fails on the ASSERT, even though hd is valid after the GetActiveWindow()
> call.
>    HWND hd = ::GetActiveWindow();
>    CButton* ptCACI = (CButton*)GetDlgItem( hd, IDC_SUBSCRIBERCACI);
>    ASSERT(ptCACI != NULL);  // FAILS ON THIS ASSERT
>    ptCACI->EnableWindow(TRUE);

Are you sure IDC_SUBSCRIBERCACI is really a button the ActiveWindow()?

cu bart

--
http://www.irule.be/bvh/



Mon, 27 Oct 2003 07:25:41 GMT  
 Usage of GetDlgItem

Quote:

> I am trying access a CButton object so I can control enabling & disabling.  I
> have 2 questions:

> 1.  Where in the project can I use GetDlgItem(int) vs. GetDlgItem(HWND, int)?
> I can't use the first call in some cases and

> 2. The second gives me problems (getting a valid HWND).  For example, the code
> below fails on the ASSERT, even though hd is valid after the GetActiveWindow()
> call.

>         HWND hd = ::GetActiveWindow();
>         CButton* ptCACI = (CButton*)GetDlgItem( hd, IDC_SUBSCRIBERCACI);
>         ASSERT(ptCACI != NULL);  // FAILS ON THIS ASSERT
>         ptCACI->EnableWindow(TRUE);

The first form simply calls the second form: Anywhere you can't use
one the other won't work either!  Stop confusing yourself and use
CWnd::GetDlgItem(int) exclusively.  It must be called in the context
of the control's parent window, i.e.

   CWnd* p = GetDlgItem(id); // within the parent window class
   CWnd* p = pParent->GetDlgItem(id); // when not within the parent
window class

...and it can only work if the control has already been created.  The
usual newbie problem is calling it before the dialog and its controls
have actually been created. That's probably why you are getting a
NULL.

Your code above is using the form of GetDlgItem that returns an HWND
and you are casting the HWND to a CWnd*.  That (shudder) is an utterly
invalid thing to do.

--
Scott McPhillips [VC++ MVP]



Mon, 27 Oct 2003 10:19:55 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. MEM Usage and CPU Usage

2. Difference: GetDlgItem from CFormView vs. GetDlgItem from CDialog ????

3. Difference: GetDlgItem from CFormView vs. GetDlgItem from CDialog ????

4. Usage of MFC & Usage of Platform SDK or Usage of MF Framework

5. Usage of MFC & Usage of Platform SDK or Usage of MF Framework

6. GetDlgItem in composite control

7. How to get HWND in ::GetDlgItem(HWND hDlg,ID_SECTIONS) before dlg.DoModal()

8. GetDlgItem return NULL, help!

9. ATL Composite Control and GetDlgItem

10. GetDlgItem

11. GetDlgItem extension and pointer passing

12. Global Function problem with GetDlgItem

 

 
Powered by phpBB® Forum Software