-
March 7th, 2004, 08:43 PM
#1
How to subclass CButton class for Checkbox?
Hi Friends,
I need to subclass a checkbox, and that is of CButton class. I tried that, and overwrite OnPaint(), however the check box disappears, I guess that is because I would need to paint the checkbox as well, well, that is beyond my knowledge, so I tried to overwrite OnCtlColor, however that doesn't get called.
One post said that I would need to set the ownerdraw flag, I tried that it doesn't work with the following:
Code:
m_chk.SubclassDlgItem(IDC_CHK1, this);
DWORD style = m_chk.GetStyle();
style |= BS_OWNERDRAW;
::SetWindowLong(m_chk, GWL_STYLE, style);
I placed this in OnInitDlg.
Please help, am I understanding this correctly and how to fix this.
Basically I need to change the font size/color of the checkbox text but still retain the checkbox bitmap itself since I don't know how to drawt that.
Thanks.
Jiac
-
March 7th, 2004, 09:14 PM
#2
Here is an example of a WM_PAINT handler for a class
derived from CButton. I called it CHECK_EX.
I wrote this on the spot, so I help it works.
Basically I just added a check box as normal, Set a control
variable to CHECK_EX with class wizard. The only thing I did
was handle WM_PAINT.
PHP Code:
void CHECK_EX::OnPaint()
{
CPaintDC dc(this); // device context for painting
dc.SaveDC();
CRect rect(0,0,0,0);
GetClientRect(&rect);
dc.FillSolidRect(&rect, ::GetSysColor(COLOR_BTNFACE));
CRect check_rect(rect.left, rect.top + rect.Height()/6, rect.left+ 4*rect.Height()/6, rect.bottom - rect.Height()/6);
CRect text_rect(check_rect.right + rect.Height()/6, rect.top, rect.right, rect.bottom);
dc.FillSolidRect(&check_rect, RGB(255,255,255));
dc.DrawEdge(&check_rect, EDGE_SUNKEN, BF_RECT);
//CHANGE YOUR FONT HERE
CString text("MY STINKIN CHECK BOX");
dc.SetBkMode(TRANSPARENT);
dc.DrawText(text, &text_rect, DT_SINGLELINE|DT_LEFT|DT_VCENTER);
dc.RestoreDC(-1);
// TODO: Add your message handler code here
// Do not call CButton::OnPaint() for painting messages
}
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
March 7th, 2004, 09:32 PM
#3
OOPS... I forgot to draw the check. you need to add that, but
that just amount to drawing some lines
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
March 8th, 2004, 09:47 AM
#4
Can you also tell me how to draw the check boxes? That is the part that I have the least idea of.
Thanks.
Jiac
-
March 8th, 2004, 01:30 PM
#5
You could use DrawFrameControl() with DFCS_BUTTONCHECK.
However, couldn't you just set a font and color, and NOT do ownerdraw stuff?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
March 9th, 2004, 05:07 PM
#6
I didn't know you could change the color of the text of a CButton
without making it owner drawn.
Can you? If you can then just do that.
If you do go the owner drawn route, then don't do what I did
above.
It seems that BS_AUTOCHECKBOX and BS_OWNERDRAW
do not go together well, but I did not try creating the button
outside of the resource editor
The easiest way to do owner drawn is to create a regular owner
drawn button and use DrawFrameControl() control draw the
check and box. Or you can draw the box and check yourself.
With this method you have to keep track of the state of the check yourself.
Let me know if you need it and I will give you the code
Last edited by souldog; March 9th, 2004 at 05:11 PM.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
March 11th, 2004, 10:18 AM
#7
Can you tell me how to change the font size and font color?
In addition, for custom draw, if I were to use DFCS_BUTTONCHECK, would user's check /uncheck still be trapped? How to do that?
Thanks.
Jiac
-
March 11th, 2004, 01:11 PM
#8
Make a CFont data member in your dialog.
In OnInitDialog(), create the font you like (look up CreateFont(...)), and set it to be used for your checkbox (using SetFont(...)).
Create OnCtlColor() handler and check if the window pointer passed in is your CheckBox, then set the text color to whatever using pDC->SetTextColor(...);
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
March 11th, 2004, 01:43 PM
#9
Darn
I didn't realize OnCtrlColor worked when a CButton has the
BS_AUTOCHECKBOX style.
That is the way to go.
Last edited by souldog; March 11th, 2004 at 02:23 PM.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
March 11th, 2004, 02:25 PM
#10
Originally posted by souldog
OnCtrlCOlor does not work for CButton objects. That is the problem
What do you mean? It works fine for me...
Code:
HBRUSH CDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if(pWnd == &m_Check)
{
pDC->SetTextColor(RGB(255,0,0));
}
return hbr;
}
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
March 11th, 2004, 02:29 PM
#11
Yes, I corrected myself.
Try using that method to change the text color for a regular button.
That is why i got confused
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
March 14th, 2004, 09:06 PM
#12
BS_AUTOCHECKBOX and BS_OWNERDRAW can not work together
if you change the buttong to BS_OWNERDRAW style, it is not CheckBox any more. after you have changed the style to BS_OWNERDRAW , the function SetCheck(), GetCheck() will not work.
-
March 14th, 2004, 09:53 PM
#13
That is correct, you have to manage that yourself.
But in this case it is not needed.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
May 4th, 2004, 01:55 PM
#14
Sorry for bumping this, but I have the very same problem. How can I change the color of the font and background in a CheckBox using _only_ Windows API functions/calls/messages/etc/etc.. ?
I'd apprecitate any help
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
|