1 Attachment(s)
remove the background of static text
As showing in attached picture, I want to remove the background of static text ("Quality : 100" and "Resoultion...").
The program I write is following:
Code:
// Print Prompt Text
hwndStaticTextQuality = CreateWindowEx(0L, "STATIC", NULL, WS_CHILD | WS_VISIBLE | ES_LEFT ,
FrameX+5, FrameY, 80, 15, hwnd, (HMENU) ID_STATIC_LABEL_QUALITY, GetModuleHandle(NULL), 0 );
sprintf(prompt_text,"Quality:%d",QUALITY_DEFAULT);
SetWindowText(hwndStaticTextQuality, prompt_text);
hwndStaticTextSpatial = CreateWindowEx(0L, "STATIC", NULL, WS_CHILD | WS_VISIBLE | ES_LEFT ,
FrameX+5, FrameY+36, 100, 15, hwnd, (HMENU) ID_STATIC_LABEL_SPATIAL, GetModuleHandle(NULL), 0 );
sprintf(prompt_text,"Resolution:1/%d",SPATIAL_DEFAULT);
SetWindowText(hwndStaticTextSpatial, prompt_text);
What should I modify?
Thank you.
Re: remove the background of static text
Does:
HDC hdc = GetDC(hwnd);
SetBkMode(hdc,TRANSPARENT);
work?
Just posting it because that's what I'm using when I output text using TextOut for example...
Re: remove the background of static text
Did you check
www.codeguru.com
on that? Looking in C++ -> Controls -> Static you will find exactly what you have asked for.
eg.
http://www.codeguru.com/cpp/controls...cle.php/c8829/
Re: remove the background of static text
Simply use WM_CTLCOLORSTATIC to change background color of a static Control
Re: remove the background of static text
Just you have to handle WM_CTLCOLORSTATIC message which is sent to parent window when the static control is to be redrawn. Inside the handler function, set backround mode TRANSPARENT and return a NULL (HOLLOW) brush.
Here is a brief example for all static controls from a dialog.
You can adapt it to your needs.
Code:
#include <WINDOWSX.H>
// ...
HBRUSH Dlg_OnCtlColor(HWND hwnd, HDC hdc, HWND hwndChild, int type);
// ...
// dialog procedure
BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
BOOL bRet = TRUE; // handled
switch(uMsg)
{
// ...
case WM_CTLCOLORSTATIC:
// // NOTE: HANDLE_WM_CTLCOLORSTATIC is defined in <WINDOWSX.H>
bRet = HANDLE_WM_CTLCOLORSTATIC(hwndDlg, wParam, lParam, Dlg_OnCtlColor);
break;
// ...
}
return bRet;
}
// WM_CTLCOLORSTATIC message handler
HBRUSH Dlg_OnCtlColor(HWND hwnd, HDC hdc, HWND hwndChild, int type)
{
SetBkMode(hdc, TRANSPARENT);
return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
}
Note: WM_CTLCOLORSTATIC is also sent for read-only or disabled edit controls.
1 Attachment(s)
Re: remove the background of static text
Quote:
Originally Posted by ovidiucucu
Just you have to handle WM_CTLCOLORSTATIC message which is sent to parent window when the static control is to be redrawn. Inside the handler function, set backround mode TRANSPARENT and return a NULL (HOLLOW) brush.
Here is a brief example for all static controls from a dialog.
You can adapt it to your needs.
Code:
#include <WINDOWSX.H>
// ...
HBRUSH Dlg_OnCtlColor(HWND hwnd, HDC hdc, HWND hwndChild, int type);
// ...
// dialog procedure
BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
BOOL bRet = TRUE; // handled
switch(uMsg)
{
// ...
case WM_CTLCOLORSTATIC:
// // NOTE: HANDLE_WM_CTLCOLORSTATIC is defined in <WINDOWSX.H>
bRet = HANDLE_WM_CTLCOLORSTATIC(hwndDlg, wParam, lParam, Dlg_OnCtlColor);
break;
// ...
}
return bRet;
}
// WM_CTLCOLORSTATIC message handler
HBRUSH Dlg_OnCtlColor(HWND hwnd, HDC hdc, HWND hwndChild, int type)
{
SetBkMode(hdc, TRANSPARENT);
return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
}
Note: WM_CTLCOLORSTATIC is also sent for read-only or disabled edit controls.
I add the following code:
Code:
case WM_CTLCOLORSTATIC:
hdc = GetDC(hwnd);
SetBkMode(hdc, TRANSPARENT);
GetStockObject(NULL_BRUSH);
ReleaseDC(hwnd,hdc);
break;
case WM_SIZE:
...
But the result as attached picture.
Re: remove the background of static text
try that:
Quote:
Originally Posted by MSDN
wParam
Handle to the device context for the static control window.
Code:
case WM_CTLCOLORSTATIC:
SetBkMode((HDC)wParam, TRANSPARENT);
return GetStockObject(NULL_BRUSH);
break;
Cheers
Re: remove the background of static text
Just an aside note:
I used in my example a message cracker macro (defined in WINDOWSX.H) with the corresponding message handler function, in order to make the code easier to read/understand/develop/maintain.
Imagine a window procedure handling tens of messages with all stuff inside it. It becomes huge and can make the programmer's life a mess. ;)
Returning to the subject:
As golanshahar already showed in his example, and also I said (maybe not enough underlined) you have to return a valid brush handle to be used by the system for painting the background of the control (in our case a null brush also known as hollow brush).
EDIT:
See this EXCELLENT ARTICLE.
Re: remove the background of static text
This code works :
Code:
HBRUSH CDigiDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr;
if ( nCtlColor == CTLCOLOR_STATIC )
{
pDC->SetBkMode(TRANSPARENT);
hbr = (HBRUSH)GetStockObject( NULL_BRUSH );
}
else
{
hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}
return hbr;
}
Re: remove the background of static text
I get the error message:
error C2440: 'return' : cannot convert from 'void *' to 'long'
Why?
Quote:
Originally Posted by golanshahar
try that:
Code:
case WM_CTLCOLORSTATIC:
SetBkMode((HDC)wParam, TRANSPARENT);
return GetStockObject(NULL_BRUSH);
break;
Cheers
Re: remove the background of static text
Like this?
case WM_CTLCOLORSTATIC:
HBRUSH hbr;
SetBkMode((HDC)wParam, TRANSPARENT);
hbr = (HBRUSH)GetStockObject(NULL_BRUSH);
break;
But the problem still exists! :confused:
Quote:
Originally Posted by FireJocker
This code works :
Code:
HBRUSH CDigiDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr;
if ( nCtlColor == CTLCOLOR_STATIC )
{
pDC->SetBkMode(TRANSPARENT);
hbr = (HBRUSH)GetStockObject( NULL_BRUSH );
}
else
{
hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}
return hbr;
}
Re: remove the background of static text
Quote:
Originally Posted by Cooker
I get the error message:
error C2440: 'return' : cannot convert from 'void *' to 'long'
Why?
I saw that you are using MFC so try this out:
Code:
HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if ( pWnd->GetDlgCtrlID() == IDC_STATIC2 /*Id of the static control you want to change*/)
{
pDC->SetBkMode(TRANSPARENT);
return (HBRUSH)::GetStockObject(HOLLOW_BRUSH);
}
return hbr;
}
Just make sure you are adding the OnCtlColor(..) with wizard.
Cheers
Re: remove the background of static text
Code I gave you will remove the background of all your static text,
If you want to remove only some of them, use code like golanshahar wrote :
Code:
HBRUSH CDigiDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr;
if ( ( pWnd->GetDlgCtrlID() == IDC_STATIC1 )
||( pWnd->GetDlgCtrlID() == IDC_STATIC2 ) ) // etc...
{
pDC->SetBkMode(TRANSPARENT);
hbr = (HBRUSH)GetStockObject( NULL_BRUSH );
}
else
{
hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}
return hbr;
}