SubClassDlgitem question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: SubClassDlgitem question

  1. #1
    Join Date
    Sep 1999
    Location
    Salisburyl UK
    Posts
    324

    SubClassDlgitem question

    Morning all..

    Im hoping someone can help me with something.
    I want to call SubClassDlgitem() against an object that I have just dynamically creted, so I have a pointer to the item.(its a Cstatic object in fact).
    I thought I would be able to get this from the Hwnd, but I dont think I can now.

    Does anyone know how I can get the handle to the object I have just created??

    Looking around a bit, it seems it may be rather orkward..

    Many thanks for your time

    Phill

  2. #2
    Join Date
    Jul 2002
    Posts
    2,494

    Re: SubClassDlgitem question

    Try CWnd::SubclassWindow.

  3. #3
    Join Date
    Apr 1999
    Posts
    3,585

    Re: SubClassDlgitem question

    What is the context of your call to SubclassDlgItem ()? Are you calling it from within a dialog, or, a control class? Please post appropriate code.
    Gort...Klaatu, Barada Nikto!

  4. #4
    Join Date
    Sep 1999
    Location
    Salisburyl UK
    Posts
    324

    Re: SubClassDlgitem question

    Hi .. Thanks for the reply.

    I'm implementing this from within the main dialog class of my dialog based app.

    Im trying to subclass using 'CColorStaticST', a class which does exactly what I want that I found here on CodeGuru.
    My code looks like this:

    Code:
    	for(i=0; i<(m_GridXSize * m_GridYSize); i++)
    	{
    
    		if((i % m_GridXSize) == 0 && (bFirst == FALSE))
    		{
    			SlabRect.top = SlabRect.top + m_SquareSize;
    			SlabRect.bottom = SlabRect.bottom + m_SquareSize;
    			SlabRect.left = 50;
    			SlabRect.right = SlabRect.left + m_SquareSize;
    
    		}
    
    		m_NumberGrid.Add(new CColorStaticST());
    		((CColorStaticST*)m_NumberGrid.ElementAt(i))->Create(" ",WS_CHILD |WS_BORDER|WS_VISIBLE, SlabRect, this);
    		
    		((CColorStaticST*)m_NumberGrid.ElementAt(i))->SubclassDlgItem(/*NEED TO PUT MY OBJECTS HANDLE HERE */, this);
    		SlabRect.OffsetRect(m_SquareSize,0);
    
    		
    		bFirst = FALSE;
    	}
    The author of CColorStaticST obviously intended that a CStatic drawn on the dialog would be used.

    Hope you can help.
    Thanks for your time
    Phil

  5. #5
    Join Date
    Apr 1999
    Posts
    3,585

    Re: SubClassDlgitem question

    You're going to need to save the pointer to the newly created static control so that you can use it in the call to SubclassDlgItem. More importantly, as written, your code allows for a memory leak because you are creating those controls on the heap and do not retain a pointer to them so their memory can be released when they are no longer needed.
    Gort...Klaatu, Barada Nikto!

  6. #6
    Join Date
    Feb 2005
    Posts
    2,160

    Re: SubClassDlgitem question

    It looks like he's saving them in m_NumberGrid.

    The nID you need is part of the CStatic::Create() method. It has a default of 0xffff as coded above. You need to supply one, then use it in your call to SubclassDlgItem().

  7. #7
    Join Date
    Sep 1999
    Location
    Salisburyl UK
    Posts
    324

    Re: SubClassDlgitem question

    H guys..
    Thanks for the reply.
    yep hoxiew I have the pointers in the m_numberGrid aray.
    Are you suggesting that I invent an ID dynamically and suplly it to both the Create and SubClassDlgItem methods ?? If so, is it sufficient to just add 1 to the default each time through the loop ??
    Ill look into that when I get back to my desk!
    Thanks again
    Phill

  8. #8
    Join Date
    Feb 2005
    Posts
    2,160

    Re: SubClassDlgitem question

    You can assign them any ID you'd like as long as they don't conflict with other dialog controls IDs. When you use a dialog template resource to create controls, VS does this automatically for you and keeps track of them in the resource.h file. You can add a range of your own IDs to this file using the "Resource Symbols" wizard. You can access this from the "Resources" tab by right-clicking on the <YourAppName>.rc entry and selecting "Resource Symbols".

  9. #9
    Join Date
    Jul 2002
    Posts
    2,494

    Re: SubClassDlgitem question

    Code:
    // from wincore.cpp
    
    BOOL CWnd::SubclassDlgItem(UINT nID, CWnd* pParent)
    {
        HWND hWndControl = ::GetDlgItem(pParent->m_hWnd, nID);
        if (hWndControl != NULL)
            return SubclassWindow(hWndControl);
    
       ...
    }
    After generating unique item ID, you can use SubclassDlgItem, which effectively calls SubclassWindow. You can call SubclassWindow without item ID, as I suggested in my first post.

  10. #10
    Join Date
    Feb 2005
    Posts
    2,160

    Re: SubClassDlgitem question

    I should have read the whole thread.

  11. #11
    Join Date
    Sep 1999
    Location
    Salisburyl UK
    Posts
    324

    Re: SubClassDlgitem question

    Guys..
    Thank you both very much.. Ive tried it both ways and both work well.
    Now I can get on !!
    thanks again for your help
    Cheers
    Phill

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center