CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2009
    Posts
    103

    is there a way to tell what "class" a dialog item is?

    I have a subclass of CEdit called CEditColor. I also have a OnCtlColor function in my dialog. I need to skip the OnCtlColor function when the control is CEditColor. Is there a way to tell what the dialog item class is so I can code the skip?


    Here is my OnCtlColor....
    Code:
    	if(m_bCustomColors)
    	{
    		HBRUSH hbr = NULL;
    		switch(nCtlColor)
    		{
    		case CTLCOLOR_MSGBOX:
    			break;
    		case CTLCOLOR_EDIT:
    			pDC->SetBkColor(m_colorListBoxBk);
    			pDC->SetTextColor(m_colorListBoxText);
    			hbr = m_brushListBox;
    			break;
    		case CTLCOLOR_SCROLLBAR:
    			break;
    		case CTLCOLOR_MAX:
    			break;
    		case CTLCOLOR_STATIC:
    			pDC->SetBkMode(TRANSPARENT);
    			pDC->SetTextColor(m_colorStaticText);
    			hbr = m_bkBrush;
    			break;
    		case CTLCOLOR_DLG:
    			hbr = m_bkBrush;
    			break;
    		case CTLCOLOR_LISTBOX:
    			pDC->SetBkColor(m_colorListBoxBk);
    			pDC->SetTextColor(m_colorListBoxText);
    			hbr = m_brushListBox;
    			break;
    		case CTLCOLOR_BTN:
    			pDC->SetBkColor(m_colorButtonBk);
    			hbr = m_brushButton;
    			break;
    		default:
    			break;
    		}
    	//	hbr = NULL;
    		if(hbr == NULL)
    			hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    
    		return hbr;
    	}
    	else
    	{
    		return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    	}

  2. #2
    Join Date
    May 2002
    Posts
    1,435

    Re: is there a way to tell what "class" a dialog item is?

    You can use Run-Time-Type-Information. MFC implements RTTI via MACROS and the CRuntimeClass structure.

    Inside your CEditColor class definition (.h file), add this if it isn't already there:

    DECLARE_DYNAMIC(CEditColor)

    You won't need to do this if DECLARE_DYNCREATE() is there because DECLARE_DYNCREATE() implements DECLARE_DYNAMIC().

    Next, add this to your CEditColor .cpp file (again, if it or IMPLEMENT_DYNCREATE() isn't already there):

    IMPLEMENT_DYNAMIC(CEditColor, CEdit)

    Now you can tell from the RTTI contained in the CWnd object passed to OnCtlColor() if it's a CEditColor control:

    BOOL bIsEditColor = pWnd->IsKindOf(RUNTIME_CLASS(CEditColor));

  3. #3
    Join Date
    May 2009
    Posts
    103

    Re: is there a way to tell what "class" a dialog item is?

    THANK YOU!!!! This worked well. This has been bugging me for a couple weeks.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: is there a way to tell what "class" a dialog item is?

    Quote Originally Posted by pbrama View Post
    THANK YOU!!!! This worked well. This has been bugging me for a couple weeks.
    However, a program filled with RTTI like this indicates the program design is flawed.

    In an OO program, it is not a good design to know exactly what classes you're dealing with at runtime like this. A C++ program should attempt to minimize the number of times it needs to do things like this:
    Code:
    if it's object A, do this 
    else if it's object B do that,
    else if it's object C do something else,
    else if it's object D do this thing here
    etc...
    Once you have multiple blocks of code looking like that, the design of your program needs to be looked at again. In C++, you have virtual functions to take care of issues such as this.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: is there a way to tell what "class" a dialog item is?

    Quote Originally Posted by pbrama View Post
    I have a subclass of CEdit called CEditColor. I also have a OnCtlColor function in my dialog. I need to skip the OnCtlColor function when the control is CEditColor. Is there a way to tell what the dialog item class is so I can code the skip?
    This should be handled by the framework using message reflection. Have a look at http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx.

    Edit: I see this is answered in your other thread too. Please don't start multiple threads for the same problem.
    Last edited by D_Drmmr; June 6th, 2011 at 10:54 AM.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: is there a way to tell what "class" a dialog item is?

    Or just derive your control from CEdit and override the message handler.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured