A Question about Buttons
-
First, I want to thank the group for their responses to my last post. Now, I have it working. Here is what my code looks like:
button1.Create( TEXT( "xxx" ), WS_VISIBLE|WS_CHILD|WS_GROUP|BS_AUTORADIOBUTTON, CRect( 250, 50+charHeight+6, 250 + 9*charWidth, 50+2*(charHeight+6)), this, 2500 ); button2.Create( TEXT( "yyyyyy" ), WS_VISIBLE|WS_CHILD|BS_AUTORADIOBUTTON, CRect( 250, 50+2*(charHeight+6), 250 + 9*charWidth, 50+3*(charHeight+6)), this, 2500 );
The problem is that when it draws the text of the button, it also draws a different background. I would like the background of the buttons to be the same as the backgrounds of the window. Currently, I do not change the background of my main window. I am thinking that one solution would be to put the text out by calling the method TextOut. Thanks Bob -
First, I want to thank the group for their responses to my last post. Now, I have it working. Here is what my code looks like:
button1.Create( TEXT( "xxx" ), WS_VISIBLE|WS_CHILD|WS_GROUP|BS_AUTORADIOBUTTON, CRect( 250, 50+charHeight+6, 250 + 9*charWidth, 50+2*(charHeight+6)), this, 2500 ); button2.Create( TEXT( "yyyyyy" ), WS_VISIBLE|WS_CHILD|BS_AUTORADIOBUTTON, CRect( 250, 50+2*(charHeight+6), 250 + 9*charWidth, 50+3*(charHeight+6)), this, 2500 );
The problem is that when it draws the text of the button, it also draws a different background. I would like the background of the buttons to be the same as the backgrounds of the window. Currently, I do not change the background of my main window. I am thinking that one solution would be to put the text out by calling the method TextOut. Thanks Bob -
Seems a regular way of crating buttons and I don't see any special code that would make the button to draw its background differently... Are you sure that is the text that makes the button draw its bg differently?
-- Arman
Arman, Thanks for the response. When the text is drawn on the screen, the background is a pale green (Note: my color vision is second rate). The normal background is white. When I make the size of the button bigger, I get a large pale green background. Therefore, I am fairly sure that the button is making the background change color. Is there an easy way for me to set the background of the button to be the same as the background of the window? Thanks Bob
-
Arman, Thanks for the response. When the text is drawn on the screen, the background is a pale green (Note: my color vision is second rate). The normal background is white. When I make the size of the button bigger, I get a large pale green background. Therefore, I am fairly sure that the button is making the background change color. Is there an easy way for me to set the background of the button to be the same as the background of the window? Thanks Bob
I think you need to handle WM_CTLCOLOR message and set the buttons background transparent and/or set a specific color for it. Something like this:
HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd->GetDlgCtrlID() == IDC_MYBUTTON)
{
// Set the background mode for text to transparent
pDC->SetBkMode(TRANSPARENT);
}
return hbr;
}-- Arman
-
I think you need to handle WM_CTLCOLOR message and set the buttons background transparent and/or set a specific color for it. Something like this:
HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd->GetDlgCtrlID() == IDC_MYBUTTON)
{
// Set the background mode for text to transparent
pDC->SetBkMode(TRANSPARENT);
}
return hbr;
}-- Arman
-
Arman, Thanks for the response. I am wondering what is the class CMyDlg? Is it a class derived from CButton? Is it the main window class? Bob
-
I didn't mean a CButton derived class but the class inside which you create your buttons. I assumed it could be CDialog derived class (that is why the name CMyDlg) but actually it may be any CWnd derived class.
-- Arman
Arman, Thanks for the response. I am doing this in a small test application first, to make sure I understand what is going on. Here is what the routine that I used:
HBRUSH CMainWindow::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CFrameWnd::OnCtlColor(pDC, pWnd, nCtlColor); if (pWnd->GetDlgCtrlID() == 2500 ) { // Set the background mode for text to transparent pDC->SetBkMode(TRANSPARENT); } return hbr; }
The class CMainWindow is the main window of the application. Here is the message map for the class:BEGIN_MESSAGE_MAP( CMainWindow, CFrameWnd ) ON_WM_CREATE() ON_WM_CTLCOLOR() END_MESSAGE_MAP()
The method CMainWindow::OnCtlColor is being called (I verified this with the debugger) and the call to SetBkMode is happening. However, the background for the button is being changed. I would like to know either what I am doing wrong, how to debug it, or where to look for some more information. Thanks Bob -
Arman, Thanks for the response. I am doing this in a small test application first, to make sure I understand what is going on. Here is what the routine that I used:
HBRUSH CMainWindow::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CFrameWnd::OnCtlColor(pDC, pWnd, nCtlColor); if (pWnd->GetDlgCtrlID() == 2500 ) { // Set the background mode for text to transparent pDC->SetBkMode(TRANSPARENT); } return hbr; }
The class CMainWindow is the main window of the application. Here is the message map for the class:BEGIN_MESSAGE_MAP( CMainWindow, CFrameWnd ) ON_WM_CREATE() ON_WM_CTLCOLOR() END_MESSAGE_MAP()
The method CMainWindow::OnCtlColor is being called (I verified this with the debugger) and the call to SetBkMode is happening. However, the background for the button is being changed. I would like to know either what I am doing wrong, how to debug it, or where to look for some more information. Thanks BobSome quick hints; * try to set background color (instead setting transparent) via CDC::SetBkColor. Here you need to create a HBRUSH object as a member variable in your CMainWindow class (i.g. in constructor; m_brush=::CreateSolidBrush(RGB(my color)) .) Then return that m_brush from OnCrlColor any time you set SetBkColor. * I'm pretty sure here at codeproject there could at least several good articles on WM_CTLCOLOR and/or OnCtlColor. I think you need make a search for those keys. Regards.
-- Arman
-
Some quick hints; * try to set background color (instead setting transparent) via CDC::SetBkColor. Here you need to create a HBRUSH object as a member variable in your CMainWindow class (i.g. in constructor; m_brush=::CreateSolidBrush(RGB(my color)) .) Then return that m_brush from OnCrlColor any time you set SetBkColor. * I'm pretty sure here at codeproject there could at least several good articles on WM_CTLCOLOR and/or OnCtlColor. I think you need make a search for those keys. Regards.
-- Arman