Find a control type base on it's ID in dialogbox 
Author Message
 Find a control type base on it's ID in dialogbox

Anyone knows how to find out a control on a dialog box is push button, or
sliderctrl base on its resource ID? so that I can cast GetDlgItem()
properly?

Thanks



Mon, 18 Mar 2002 03:00:00 GMT  
 Find a control type base on it's ID in dialogbox
I know that you can use the GetRuntimeClass() method on a CObject object.
Since CWnd class derives from CObject class, that's your aim.

Do it like this (pseudo code):

// BEGIN CODE
CWnd* wnd = yourDlg.GetDlgItem(yourControlID);
CRuntimeClass* rtc = wnd->GetRuntimeClass();
if(rtc->m_lpszClassName == "CSliderCtrl")
    // .... slider
else
    // .... button
// END CODE

If this code doesn't work, answer this post. I'll check it out. Maybe you
wanna try posting some code.

TiTi
(Don't mail me - post to this newsgroup)


Quote:
> Anyone knows how to find out a control on a dialog box is push button, or
> sliderctrl base on its resource ID? so that I can cast GetDlgItem()
> properly?

> Thanks



Tue, 19 Mar 2002 03:00:00 GMT  
 Find a control type base on it's ID in dialogbox
Thanks Titi, But it doesn't work, because GetDlgItem() returns a pointer to
CTempWnd object, it derived from CWnd.(your code: rtc->m_lpszClassName
holds "CTempWnd").

following code doesn't work either.

   CWnd* pActiveWnd;
   if((pActiveWnd=GetDlgItem(CtlID))==NULL) return FALSE;

   if((pActiveWnd)->IsKindOf ( RUNTIME_CLASS(CSliderCtrl) ) )
{
 ////never goes here even CtlID is ID of a sliderctrl.
 ....

Quote:
}

Eric


Quote:
> I know that you can use the GetRuntimeClass() method on a CObject object.
> Since CWnd class derives from CObject class, that's your aim.

> Do it like this (pseudo code):

> // BEGIN CODE
> CWnd* wnd = yourDlg.GetDlgItem(yourControlID);
> CRuntimeClass* rtc = wnd->GetRuntimeClass();
> if(rtc->m_lpszClassName == "CSliderCtrl")
>     // .... slider
> else
>     // .... button
> // END CODE

> If this code doesn't work, answer this post. I'll check it out. Maybe you
> wanna try posting some code.

> TiTi
> (Don't mail me - post to this newsgroup)



> > Anyone knows how to find out a control on a dialog box is push button,
or
> > sliderctrl base on its resource ID? so that I can cast GetDlgItem()
> > properly?

> > Thanks



Tue, 19 Mar 2002 03:00:00 GMT  
 Find a control type base on it's ID in dialogbox
Hello.

Try using the following command instead (taken from the MSDN)
=-=-=-=-=-=-=-=-=-=-=-=-=-=-
GetClassName
The GetClassName function retrieves the name of the class to which the
specified window belongs.

int GetClassName(
  HWND hWnd,           // handle of window
  LPTSTR lpClassName,  // address of buffer for class name
  int nMaxCount        // size of buffer, in characters
);

Parameters
hWnd
    Handle to the window and, indirectly, the class to which the window
belongs.
lpClassName
    Pointer to the buffer that is to receive the class name string.
nMaxCount
    Specifies the length, in characters, of the buffer pointed to by the
lpClassName parameter. The class name string is truncated if it is longer
than the buffer.

Return Values
    If the function succeeds, the return value is the number of characters
copied to the specified buffer.
    If the function fails, the return value is zero. To get extended error
information, callGetLastError.

=-=-=-=-=-=-
Even though "GetDlgItem() returns a pointer to CTempWnd" This should work...

Tomer.


Quote:
> Thanks Titi, But it doesn't work, because GetDlgItem() returns a pointer
to
> CTempWnd object, it derived from CWnd.(your code: rtc->m_lpszClassName
> holds "CTempWnd").

> following code doesn't work either.

>    CWnd* pActiveWnd;
>    if((pActiveWnd=GetDlgItem(CtlID))==NULL) return FALSE;

>    if((pActiveWnd)->IsKindOf ( RUNTIME_CLASS(CSliderCtrl) ) )
> {
>  ////never goes here even CtlID is ID of a sliderctrl.
>  ....
> }

> Eric



> > I know that you can use the GetRuntimeClass() method on a CObject
object.
> > Since CWnd class derives from CObject class, that's your aim.

> > Do it like this (pseudo code):

> > // BEGIN CODE
> > CWnd* wnd = yourDlg.GetDlgItem(yourControlID);
> > CRuntimeClass* rtc = wnd->GetRuntimeClass();
> > if(rtc->m_lpszClassName == "CSliderCtrl")
> >     // .... slider
> > else
> >     // .... button
> > // END CODE

> > If this code doesn't work, answer this post. I'll check it out. Maybe
you
> > wanna try posting some code.

> > TiTi
> > (Don't mail me - post to this newsgroup)



> > > Anyone knows how to find out a control on a dialog box is push button,
> or
> > > sliderctrl base on its resource ID? so that I can cast GetDlgItem()
> > > properly?

> > > Thanks



Tue, 19 Mar 2002 03:00:00 GMT  
 Find a control type base on it's ID in dialogbox
Thanks, this one works.

Eric


Quote:
> Hello.

> Try using the following command instead (taken from the MSDN)
> =-=-=-=-=-=-=-=-=-=-=-=-=-=-
> GetClassName
> The GetClassName function retrieves the name of the class to which the
> specified window belongs.

> int GetClassName(
>   HWND hWnd,           // handle of window
>   LPTSTR lpClassName,  // address of buffer for class name
>   int nMaxCount        // size of buffer, in characters
> );

> Parameters
> hWnd
>     Handle to the window and, indirectly, the class to which the window
> belongs.
> lpClassName
>     Pointer to the buffer that is to receive the class name string.
> nMaxCount
>     Specifies the length, in characters, of the buffer pointed to by the
> lpClassName parameter. The class name string is truncated if it is longer
> than the buffer.

> Return Values
>     If the function succeeds, the return value is the number of characters
> copied to the specified buffer.
>     If the function fails, the return value is zero. To get extended error
> information, callGetLastError.

> =-=-=-=-=-=-
> Even though "GetDlgItem() returns a pointer to CTempWnd" This should
work...

> Tomer.



> > Thanks Titi, But it doesn't work, because GetDlgItem() returns a pointer
> to
> > CTempWnd object, it derived from CWnd.(your code: rtc->m_lpszClassName
> > holds "CTempWnd").

> > following code doesn't work either.

> >    CWnd* pActiveWnd;
> >    if((pActiveWnd=GetDlgItem(CtlID))==NULL) return FALSE;

> >    if((pActiveWnd)->IsKindOf ( RUNTIME_CLASS(CSliderCtrl) ) )
> > {
> >  ////never goes here even CtlID is ID of a sliderctrl.
> >  ....
> > }

> > Eric



> > > I know that you can use the GetRuntimeClass() method on a CObject
> object.
> > > Since CWnd class derives from CObject class, that's your aim.

> > > Do it like this (pseudo code):

> > > // BEGIN CODE
> > > CWnd* wnd = yourDlg.GetDlgItem(yourControlID);
> > > CRuntimeClass* rtc = wnd->GetRuntimeClass();
> > > if(rtc->m_lpszClassName == "CSliderCtrl")
> > >     // .... slider
> > > else
> > >     // .... button
> > > // END CODE

> > > If this code doesn't work, answer this post. I'll check it out. Maybe
> you
> > > wanna try posting some code.

> > > TiTi
> > > (Don't mail me - post to this newsgroup)



> > > > Anyone knows how to find out a control on a dialog box is push
button,
> > or
> > > > sliderctrl base on its resource ID? so that I can cast GetDlgItem()
> > > > properly?

> > > > Thanks



Tue, 19 Mar 2002 03:00:00 GMT  
 Find a control type base on it's ID in dialogbox
Sounds a bit tricky!

Are you sure there isn't a better way to do what you're doing.

No way of doing it via resource ID. If you must do this sort of thing,
you'll have to use runtime type information.
ie dynamic_cast of similar

Jeremy

Quote:

>Anyone knows how to find out a control on a dialog box is push button, or
>sliderctrl base on its resource ID? so that I can cast GetDlgItem()
>properly?

>Thanks



Mon, 25 Mar 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Find dialogbox or Find/replace dialogbox?

2. Find Window Handle based on Thread ID

3. Why can't overload type cast to base type (object)

4. Development environment hanging when using #import directive with type-lib id'si

5. How to find the control ID

6. Finding the Dialog, a Control-Id belongs to

7. Find the Dialog that contains a Control identified by its ID

8. Finding Control's own ID (Window to ID) ?

9. Finding Control ID's own Dialog_Control_ID (Window to ID) ?

10. Type Casting between Base and Derived Type (Serialization of objects with Type members)

11. Accessing Base Class's control's events

12. #imported dll - can't find base class error

 

 
Powered by phpBB® Forum Software