As showing in attached picture, I want to remove the background of static text ("Quality : 100" and "Resoultion...").
The program I write is following:
Simply use WM_CTLCOLORSTATIC to change background color of a static Control
ovidiucucu
January 18th, 2006, 03: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, 12: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, 01: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, 04: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/).
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, 07: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 :