Click to See Complete Forum and Search --> : remove the background of static text


Cooker
January 18th, 2006, 02:51 AM
As showing in attached picture, I want to remove the background of static text ("Quality : 100" and "Resoultion...").
The program I write is following:

// 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.

Xatrix
January 18th, 2006, 03:54 AM
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...

HoM
January 18th, 2006, 03:58 AM
Did you check

www.codeguru.com (http://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/staticctrl/datadisplaycontrols/article.php/c8829/

humptydumpty
January 18th, 2006, 04:01 AM
Simply use WM_CTLCOLORSTATIC to change background color of a static Control

ovidiucucu
January 18th, 2006, 04:02 AM
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.
#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.

Cooker
January 20th, 2006, 01:16 AM
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.
#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:

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.

golanshahar
January 20th, 2006, 02:25 AM
try that:


wParam
Handle to the device context for the static control window.



case WM_CTLCOLORSTATIC:
SetBkMode((HDC)wParam, TRANSPARENT);
return GetStockObject(NULL_BRUSH);
break;


Cheers

ovidiucucu
January 20th, 2006, 05:46 AM
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 (http://www.codeguru.com/cpp/w-p/win32/tutorials/article.php/c4527/).

FireJocker
February 13th, 2006, 04:12 AM
This code works :

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;
}

Cooker
February 14th, 2006, 08:54 PM
I get the error message:
error C2440: 'return' : cannot convert from 'void *' to 'long'
Why?

try that:




case WM_CTLCOLORSTATIC:
SetBkMode((HDC)wParam, TRANSPARENT);
return GetStockObject(NULL_BRUSH);
break;


Cheers

Cooker
February 14th, 2006, 08:57 PM
Like this?
case WM_CTLCOLORSTATIC:
HBRUSH hbr;
SetBkMode((HDC)wParam, TRANSPARENT);
hbr = (HBRUSH)GetStockObject(NULL_BRUSH);
break;
But the problem still exists! :confused:
This code works :

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;
}

golanshahar
February 15th, 2006, 02:23 AM
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:


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

FireJocker
February 15th, 2006, 02:57 AM
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 :


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;
}