Problems subclassing a window 
Author Message
 Problems subclassing a window

Hi all...

If I have a CListCtrl placed at design time on a dialog
resource and this member variable:

CSheetListCtrl m_sheet;

Where CSheetListCtrl is derived from CListCtrl, I can call

m_sheet.SubclassWindow(GetDlgItem(IDC_LIST_EX)->m_hWnd);

But if I create that CListCtrl dynamically by using its
Create method, subclassing doesn't work. It asserts in the
call to FromHandlePermanent(hWndNew) in the following
method:

BOOL CWnd::Attach(HWND hWndNew)
{
        ASSERT(m_hWnd == NULL);     // only attach once,
detach on destroy
        ASSERT(FromHandlePermanent(hWndNew) == NULL);
                // must not already be in permanent map

        if (hWndNew == NULL)
                return FALSE;

        CHandleMap* pMap = afxMapHWND(TRUE); // create map
if not exist
        ASSERT(pMap != NULL);

        pMap->SetPermanent(m_hWnd = hWndNew, this);

#ifndef _AFX_NO_OCC_SUPPORT
        AttachControlSite(pMap);
#endif

        return TRUE;

Quote:
}

How can I do it then?
Thanks

Jaime



Mon, 28 Nov 2005 16:25:20 GMT  
 Problems subclassing a window
Not sure exactly what you are doing but if you are calling create window on
a listbox that is already associated with an existing ctrl, i.e. another
window, that sounds like it would cause the assert you are seeing?  You may
try calling DestroyWindow before the create.


Quote:
> Hi all...

> If I have a CListCtrl placed at design time on a dialog
> resource and this member variable:

> CSheetListCtrl m_sheet;

> Where CSheetListCtrl is derived from CListCtrl, I can call

> m_sheet.SubclassWindow(GetDlgItem(IDC_LIST_EX)->m_hWnd);

> But if I create that CListCtrl dynamically by using its
> Create method, subclassing doesn't work. It asserts in the
> call to FromHandlePermanent(hWndNew) in the following
> method:

> BOOL CWnd::Attach(HWND hWndNew)
> {
> ASSERT(m_hWnd == NULL);     // only attach once,
> detach on destroy
> ASSERT(FromHandlePermanent(hWndNew) == NULL);
> // must not already be in permanent map

> if (hWndNew == NULL)
> return FALSE;

> CHandleMap* pMap = afxMapHWND(TRUE); // create map
> if not exist
> ASSERT(pMap != NULL);

> pMap->SetPermanent(m_hWnd = hWndNew, this);

> #ifndef _AFX_NO_OCC_SUPPORT
> AttachControlSite(pMap);
> #endif

> return TRUE;
> }

> How can I do it then?
> Thanks

> Jaime



Mon, 28 Nov 2005 16:44:48 GMT  
 Problems subclassing a window

Quote:
> Hi all...

> If I have a CListCtrl placed at design time on a dialog
> resource and this member variable:

> CSheetListCtrl m_sheet;

> Where CSheetListCtrl is derived from CListCtrl, I can call

> m_sheet.SubclassWindow(GetDlgItem(IDC_LIST_EX)->m_hWnd);

> But if I create that CListCtrl dynamically by using its
> Create method, subclassing doesn't work. It asserts in the
> call to FromHandlePermanent(hWndNew) in the following
> method:

If you call through its Create member function, MFC should be performing
the subclassing automatically. There is no need to do it again.

--
Jeff Partch [VC++ MVP]



Mon, 28 Nov 2005 16:45:42 GMT  
 Problems subclassing a window

Thanks a lot Jeff and River for replying. The problem was
that I had to Create( ) a CSheetListCtrl object with the
CListCtrl previously created as its parent. And when I
don't need it anymore, I need to call CSheetListCtrl
object DestroyWindow( ).

Regards,
Jaime

Quote:
>-----Original Message-----
>Not sure exactly what you are doing but if you are

calling create window on
Quote:
>a listbox that is already associated with an existing
ctrl, i.e. another
>window, that sounds like it would cause the assert you

are seeing?  You may
Quote:
>try calling DestroyWindow before the create.



>> Hi all...

>> If I have a CListCtrl placed at design time on a dialog
>> resource and this member variable:

>> CSheetListCtrl m_sheet;

>> Where CSheetListCtrl is derived from CListCtrl, I can
call

>> m_sheet.SubclassWindow(GetDlgItem(IDC_LIST_EX)->m_hWnd);

>> But if I create that CListCtrl dynamically by using its
>> Create method, subclassing doesn't work. It asserts in
the
>> call to FromHandlePermanent(hWndNew) in the following
>> method:

>> BOOL CWnd::Attach(HWND hWndNew)
>> {
>> ASSERT(m_hWnd == NULL);     // only attach once,
>> detach on destroy
>> ASSERT(FromHandlePermanent(hWndNew) == NULL);
>> // must not already be in permanent map

>> if (hWndNew == NULL)
>> return FALSE;

>> CHandleMap* pMap = afxMapHWND(TRUE); // create map
>> if not exist
>> ASSERT(pMap != NULL);

>> pMap->SetPermanent(m_hWnd = hWndNew, this);

>> #ifndef _AFX_NO_OCC_SUPPORT
>> AttachControlSite(pMap);
>> #endif

>> return TRUE;
>> }

>> How can I do it then?
>> Thanks

>> Jaime

>.



Mon, 28 Nov 2005 17:35:58 GMT  
 Problems subclassing a window
Using SubclassWindow is rarely if ever appropriate. Certainly it makes no sense if you
have alreaday subclassed the window by binding it to the member variable. Just get rid of
that line; it isn't doing anything of value, and, as you observe, it actually causes a
failure. Use the ClassWizard to set up the association; there is no reason to call
SubclassWindow yourself.

Likewise, if you create it dynamically, you have the same problem. You have already
associated the control with the member variable, and trying to do it a second time simply
generates an assertion fault.

So in neither case is it appropriate.
                                        joe

Quote:

>Hi all...

>If I have a CListCtrl placed at design time on a dialog
>resource and this member variable:

>CSheetListCtrl m_sheet;

>Where CSheetListCtrl is derived from CListCtrl, I can call

>m_sheet.SubclassWindow(GetDlgItem(IDC_LIST_EX)->m_hWnd);

>But if I create that CListCtrl dynamically by using its
>Create method, subclassing doesn't work. It asserts in the
>call to FromHandlePermanent(hWndNew) in the following
>method:

>BOOL CWnd::Attach(HWND hWndNew)
>{
>    ASSERT(m_hWnd == NULL);     // only attach once,
>detach on destroy
>    ASSERT(FromHandlePermanent(hWndNew) == NULL);
>            // must not already be in permanent map

>    if (hWndNew == NULL)
>            return FALSE;

>    CHandleMap* pMap = afxMapHWND(TRUE); // create map
>if not exist
>    ASSERT(pMap != NULL);

>    pMap->SetPermanent(m_hWnd = hWndNew, this);

>#ifndef _AFX_NO_OCC_SUPPORT
>    AttachControlSite(pMap);
>#endif

>    return TRUE;
>}

>How can I do it then?
>Thanks

>Jaime

Joseph M. Newcomer [MVP]

Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm


Tue, 29 Nov 2005 02:32:33 GMT  
 Problems subclassing a window
It sounds like you are trying to create a CListCtrl which is a child control of another
CListCtrl. Is this what you intended to do? "Parent" is a very specific relationship in
Windows, and is not the same as the C++ "superclass".
                                        joe

Quote:

>Thanks a lot Jeff and River for replying. The problem was
>that I had to Create( ) a CSheetListCtrl object with the
>CListCtrl previously created as its parent. And when I
>don't need it anymore, I need to call CSheetListCtrl
>object DestroyWindow( ).

>Regards,
>Jaime

>>-----Original Message-----
>>Not sure exactly what you are doing but if you are
>calling create window on
>>a listbox that is already associated with an existing
>ctrl, i.e. another
>>window, that sounds like it would cause the assert you
>are seeing?  You may
>>try calling DestroyWindow before the create.



>>> Hi all...

>>> If I have a CListCtrl placed at design time on a dialog
>>> resource and this member variable:

>>> CSheetListCtrl m_sheet;

>>> Where CSheetListCtrl is derived from CListCtrl, I can
>call

>>> m_sheet.SubclassWindow(GetDlgItem(IDC_LIST_EX)->m_hWnd);

>>> But if I create that CListCtrl dynamically by using its
>>> Create method, subclassing doesn't work. It asserts in
>the
>>> call to FromHandlePermanent(hWndNew) in the following
>>> method:

>>> BOOL CWnd::Attach(HWND hWndNew)
>>> {
>>> ASSERT(m_hWnd == NULL);     // only attach once,
>>> detach on destroy
>>> ASSERT(FromHandlePermanent(hWndNew) == NULL);
>>> // must not already be in permanent map

>>> if (hWndNew == NULL)
>>> return FALSE;

>>> CHandleMap* pMap = afxMapHWND(TRUE); // create map
>>> if not exist
>>> ASSERT(pMap != NULL);

>>> pMap->SetPermanent(m_hWnd = hWndNew, this);

>>> #ifndef _AFX_NO_OCC_SUPPORT
>>> AttachControlSite(pMap);
>>> #endif

>>> return TRUE;
>>> }

>>> How can I do it then?
>>> Thanks

>>> Jaime

>>.

Joseph M. Newcomer [MVP]

Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm


Tue, 29 Nov 2005 02:33:53 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Subclassing Problem with Unicode Windows

2. Q: subclassing problem, with hook and windows messages

3. Subclassing window

4. Subclassing foreign window and changing memory in it

5. Subclassing a window in different process

6. Subclassing window

7. Subclassing Windows

8. Subclassing Windows class in MFC

9. Subclassing a window

10. Subclassing window

11. subclassing a MFC subclassed window

12. Subclassing a window in another application?

 

 
Powered by phpBB® Forum Software