|
-
June 4th, 2011, 09:44 PM
#1
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);
}
-
June 5th, 2011, 09:05 AM
#2
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));
-
June 5th, 2011, 11:23 AM
#3
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.
-
June 5th, 2011, 02:54 PM
#4
Re: is there a way to tell what "class" a dialog item is?
 Originally Posted by pbrama
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
-
June 6th, 2011, 10:47 AM
#5
Re: is there a way to tell what "class" a dialog item is?
 Originally Posted by pbrama
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
-
June 6th, 2011, 10:54 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|