-
September 14th, 2009, 04:14 AM
#1
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
-
September 14th, 2009, 04:32 AM
#2
Re: SubClassDlgitem question
Try CWnd::SubclassWindow.
-
September 14th, 2009, 07:32 AM
#3
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!
-
September 14th, 2009, 08:15 AM
#4
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
-
September 14th, 2009, 08:33 AM
#5
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!
-
September 14th, 2009, 09:05 AM
#6
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().
-
September 14th, 2009, 09:16 AM
#7
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
-
September 14th, 2009, 09:27 AM
#8
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".
-
September 14th, 2009, 09:53 AM
#9
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.
-
September 14th, 2009, 10:16 AM
#10
Re: SubClassDlgitem question
I should have read the whole thread.
-
September 14th, 2009, 11:15 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|