January 18th, 2006, 02:51 AM
#1
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.
Attached Images
January 18th, 2006, 03:54 AM
#2
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...
January 18th, 2006, 03:58 AM
#3
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/
January 18th, 2006, 04:01 AM
#4
Re: remove the background of static text
Simply use WM_CTLCOLORSTATIC to change background color of a static Control
January 18th, 2006, 04:02 AM
#5
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.
Last edited by ovidiucucu; January 18th, 2006 at 04:05 AM .
January 20th, 2006, 01:16 AM
#6
Re: remove the background of static text
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.
Attached Images
January 20th, 2006, 02:25 AM
#7
Re: remove the background of static text
try that:
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
January 20th, 2006, 05:46 AM
#8
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 .
Last edited by ovidiucucu; January 20th, 2006 at 06:35 AM .
February 13th, 2006, 04:12 AM
#9
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;
}
February 14th, 2006, 08:54 PM
#10
Re: remove the background of static text
I get the error message:
error C2440: 'return' : cannot convert from 'void *' to 'long'
Why?
Originally Posted by
golanshahar
try that:
Code:
case WM_CTLCOLORSTATIC:
SetBkMode((HDC)wParam, TRANSPARENT);
return GetStockObject(NULL_BRUSH);
break ;
Cheers
February 14th, 2006, 08:57 PM
#11
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!
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;
}
February 15th, 2006, 02:23 AM
#12
Re: remove the background of static text
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
February 15th, 2006, 02:57 AM
#13
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;
}
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