Static controls transparency problem
-
Hi, I have a problem with getting a transparent static control to draw correctly on a WTL dialog. Everything I do works, but only if the text color is black. Message handler is like this:
MESSAGE_HANDLER(WM_CTLCOLORSTATIC, OnCtlColorStatic)
Then I have a method to handle it:LRESULT OnCtlColorStatic(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
and in the code I do like this:LRESULT MainDialog::OnCtlColorStatic(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { HWND hWnd = (HWND)lParam; HDC hDc = (HDC)wParam; COLORREF m_crTextColorMaroon; m_crTextColorMaroon = RGB( 128, 0, 0 ); HBRUSH hbr; hbr = (HBRUSH)m_crTextColorMaroon; if( hWnd == (HWND)GetDlgItem(IDC_FILE) || hWnd == (HWND)GetDlgItem(IDC_FILESIZE)) { ::SetTextColor( hDc, m_crTextColorMaroon ); ::SetBkMode(hDc, TRANSPARENT); return (LRESULT) GetStockObject (HOLLOW_BRUSH); } return 0; }
So far so good, everything works. I am only changing the color of the controls that will represent a label, the other static controls on the dialog should display text in the black color. So this works perfectly when the dialog gets displayed. Now the user changes the language on the menu, and I do this:SetDlgItemText( IDC_FILE, (LPCTSTR) SomeNewText);
and the text gets drawn over the previous text, making it indecipherable. What is wrong with this code? Why doesn't the static controls clear its content before the new text is drawn. If I useSetDlgItemText(...)
with any other static control that is not handled in theOnCtlColorStatic
method, it gets drawn correctly. Thanks for your help.Sarajevo, Bosnia
-
Hi, I have a problem with getting a transparent static control to draw correctly on a WTL dialog. Everything I do works, but only if the text color is black. Message handler is like this:
MESSAGE_HANDLER(WM_CTLCOLORSTATIC, OnCtlColorStatic)
Then I have a method to handle it:LRESULT OnCtlColorStatic(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
and in the code I do like this:LRESULT MainDialog::OnCtlColorStatic(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { HWND hWnd = (HWND)lParam; HDC hDc = (HDC)wParam; COLORREF m_crTextColorMaroon; m_crTextColorMaroon = RGB( 128, 0, 0 ); HBRUSH hbr; hbr = (HBRUSH)m_crTextColorMaroon; if( hWnd == (HWND)GetDlgItem(IDC_FILE) || hWnd == (HWND)GetDlgItem(IDC_FILESIZE)) { ::SetTextColor( hDc, m_crTextColorMaroon ); ::SetBkMode(hDc, TRANSPARENT); return (LRESULT) GetStockObject (HOLLOW_BRUSH); } return 0; }
So far so good, everything works. I am only changing the color of the controls that will represent a label, the other static controls on the dialog should display text in the black color. So this works perfectly when the dialog gets displayed. Now the user changes the language on the menu, and I do this:SetDlgItemText( IDC_FILE, (LPCTSTR) SomeNewText);
and the text gets drawn over the previous text, making it indecipherable. What is wrong with this code? Why doesn't the static controls clear its content before the new text is drawn. If I useSetDlgItemText(...)
with any other static control that is not handled in theOnCtlColorStatic
method, it gets drawn correctly. Thanks for your help.Sarajevo, Bosnia
You're returning
HOLLOW_BRUSH
which paints nothing. When the control asks you what brush to use as the background brush, you give it a brush that doesn't paint, so the old contents of the control aren't erased. I'd do this:LRESULT MainDialog::OnCtlColorStatic(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
HWND hWnd = (HWND) lParam;
HDC hDc = (HDC) wParam;if(hWnd == GetDlgItem(IDC_FILE) || hWnd == GetDlgItem(IDC_FILESIZE))
{
SetTextColor(hDc, RGB(128,0,0));
SetBkMode(hDc, TRANSPARENT);
}bHandled = false;
return 0;
}This code changes the attributes of the DC that you care about, then lets the message go on to the default window proc, which will set the right background color.
--Mike-- Visual C++ MVP :cool: LINKS~! CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.
-
You're returning
HOLLOW_BRUSH
which paints nothing. When the control asks you what brush to use as the background brush, you give it a brush that doesn't paint, so the old contents of the control aren't erased. I'd do this:LRESULT MainDialog::OnCtlColorStatic(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
HWND hWnd = (HWND) lParam;
HDC hDc = (HDC) wParam;if(hWnd == GetDlgItem(IDC_FILE) || hWnd == GetDlgItem(IDC_FILESIZE))
{
SetTextColor(hDc, RGB(128,0,0));
SetBkMode(hDc, TRANSPARENT);
}bHandled = false;
return 0;
}This code changes the attributes of the DC that you care about, then lets the message go on to the default window proc, which will set the right background color.
--Mike-- Visual C++ MVP :cool: LINKS~! CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.
Thanks for your help, Mike, but this does not work for me. Now all static controls display a black colored text. Yes, I can change the text and not to get it write over the previous one, but the text is not redish ( RGB(128, 0, 0)) but black. Can I send you the app, it's only one dialog, just a few lines of code above the normal code generated for the ATL exe app? Thanks.
Sarajevo, Bosnia
-
Thanks for your help, Mike, but this does not work for me. Now all static controls display a black colored text. Yes, I can change the text and not to get it write over the previous one, but the text is not redish ( RGB(128, 0, 0)) but black. Can I send you the app, it's only one dialog, just a few lines of code above the normal code generated for the ATL exe app? Thanks.
Sarajevo, Bosnia
OK, this time I actually tested the code that I'm going to post. ;)
LRESULT CMainDlg::OnCtlcolorstatic(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
LRESULT res = ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);
HDC hdc = (HDC) wParam;
HWND hwnd = (HWND) lParam;if(hwnd == GetDlgItem(IDC\_STATIC1) || hwnd == GetDlgItem(IDC\_STATIC2)) { SetTextColor(hdc, RGB(255,0,0)); SetBkMode(hdc, TRANSPARENT); } return res;
}
The first line gets the default brush so the background looks right.
Last modified: 8hrs 3mins after originally posted --
--Mike-- Visual C++ MVP :cool: LINKS~! CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.
-
OK, this time I actually tested the code that I'm going to post. ;)
LRESULT CMainDlg::OnCtlcolorstatic(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
LRESULT res = ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);
HDC hdc = (HDC) wParam;
HWND hwnd = (HWND) lParam;if(hwnd == GetDlgItem(IDC\_STATIC1) || hwnd == GetDlgItem(IDC\_STATIC2)) { SetTextColor(hdc, RGB(255,0,0)); SetBkMode(hdc, TRANSPARENT); } return res;
}
The first line gets the default brush so the background looks right.
Last modified: 8hrs 3mins after originally posted --
--Mike-- Visual C++ MVP :cool: LINKS~! CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.