Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Background colors

30 views
Skip to first unread message

Lawrence Marigold

unread,
Jun 12, 2000, 3:00:00 AM6/12/00
to
How may I change the background colour of a window ?

Specifically a control whose bgcolor has been preregistered in
wndclassex ?

And even more specifically, and edit control, which I would like to
appear greyed ?

Thankyou.

Todd Osborne

unread,
Jun 13, 2000, 3:00:00 AM6/13/00
to
You have a couple of options. If you want to change the background of ALL
edit controls in your application, you could use SetClassLong() and set the
new background brush. If you want to be more selective, handle the
WM_CTLCOLORxxx messages in the parent window and return the brush to fill
the control with.

Todd

"Lawrence Marigold" <cen...@lineone.net> wrote in message
news:8i2t98$h8u$1...@supernews.com...

Dave Ballinger

unread,
Jun 13, 2000, 3:00:00 AM6/13/00
to
Handle the message:

case WM_CTLCOLOREDIT:
if( (HWND) lParam == GetDlgItem( hwndParent, ID_YOUR_EDITBOX ) )
{
/* You can set your text color, background, and modes here, too */
SetTextMode( (HDC)wParam, TRANSPARENT ); /* also OPAQUE */
SetTextColor( (HDC)wParam, RGB( 255,255,255 ) /*WHITE_TEXT*/ );
return ( LRESULT )GetStockObject( LTGRAY_BRUSH );
}
return DefWindowProc( hwndParent, uMsg, wParam, lParam );

Adam Foster

unread,
Jun 13, 2000, 3:00:00 AM6/13/00
to
here is what i use to change each edit control on the windows.
RecvEdit, SendEdit,... are all HWND delcared.
HBRUSH hBrush;
case WM_CTLCOLOREDIT:
{
if((HWND) lParam == RecvEdit)
{
hBrush=CreateSolidBrush( RGB(0,0,200) );
SetBkColor( (HDC)wParam, RGB(0,0,200) );
SetTextColor((HDC)wParam,RGB(0,255,255));
SelectObject( (HDC)wParam, hBrush );
return((LONG)hBrush);
}
if((HWND)lParam == SendEdit)
{
hBrush=CreateSolidBrush(RGB(0,0,0));
SetBkColor((HDC)wParam,RGB(0,0,0));
SetTextColor((HDC)wParam,RGB(0,255,0));
SelectObject((HDC)wParam,hBrush);
return((LONG)hBrush);
}
if((HWND)lParam == ServerAddyEdit)
{
hBrush=CreateSolidBrush(RGB(50,50,50));
SetBkColor((HDC)wParam,RGB(50,50,50));
SetTextColor((HDC)wParam,RGB(100,200,150));
SelectObject((HDC)wParam,hBrush);
return((LONG)hBrush);
}
if((HWND)lParam == ServerPortAddy)
{
hBrush=CreateSolidBrush(RGB(0,0,0));
SetBkColor((HDC)wParam,RGB(0,0,0));
SetTextColor((HDC)wParam,RGB(150,0,250));
SelectObject((HDC)wParam,hBrush);
return((LONG)hBrush);
}
}

Adam "Nasiry" Foster
"Dave Ballinger" <dball...@worldnet.att.net> wrote in message
news:j2j15.7958$iy.5...@bgtnsc06-news.ops.worldnet.att.net...

John A. Grant

unread,
Jun 13, 2000, 3:00:00 AM6/13/00
to
"Adam Foster" <nas...@gamebox.net> wrote in message
news:skdrua...@corp.supernews.com...

This is bad news. You're going to get lots of WM_CTLCOLOR...
messages and each time, you create a new HBRUSH.
How are you deleting all of those brushes when your
dialog box ends? You've got a resource leak.

In addition, you do NOT select your HBRUSH into
the HDC provided. Just return the HBRUSH and the
control's drawing procedure will take care of selecting
it in and out of the HDC.

Here's the way you should be doing it:
static HBRUSH hbrush_recv,hbrush_send,
hbrush_serveraddy,hbrush_serverport;
...
case WM_INITDIALOG:
hbrush_recv=CreateSolidBrush(...);
hbrush_send=CreateSolidBrush(...);
hbrush_serveraddy=CreateSolidBrush(...);
hbrush_serverport=CreateSolidBrush(...);
break;

case WM_COMMAND
int id=GET_WM_COMMAND_ID(wparam,lparam);
switch(id){
case IDCANCEL:
case IDOK:
DeleteObject(hbrush_recv);
DeleteObject(hbrush_send);
DeleteObject(hbrush_serveraddy);
DeleteObject(hbrush_serverport);
EndDialog(hdlg,id==IDOK);
break;

Then you just return the appropriate HBRUSH at
WM_CTLCOLOR*

Also you don't have to hang on to the HWND of the controls.
Assuming you have some constants:
#define IDC_RECV 101
#define IDC_SEND 102
#define IDC_SERVERADDY 103
#define IDC_SERVERPORT 104

Then you can just do this:
case WM_CTLCOLOREDIT:
HDC hdc= GET_WM_CTLCOLOR_HDC(wparam,lparam);
HWND hwnd=GET_WM_CTLCOLOR_HWND(wparam,lparam);
int type=GET_WM_CTLCOLOR_TYPE(wparam,lparam);

int id=GetDlgCtrlID(hwnd);

switch(id){
case IDC_RECV:
...
break;

case IDC_SEND:
...
break;

case IDC_SERVERADDY:
...
break;

case IDC_SERVERPORT:
...
break;

}

If this is in a DlgProc(), you should be casting the HBRUSH
to a BOOL since the DlgProc() returns a BOOL. Yes, a
BOOL is always large enough to hold an HBRUSH.

--
John A. Grant * I speak only for myself * (remove 'z' to reply)
Radiation Geophysics, Geological Survey of Canada, Ottawa
If you followup, please do NOT e-mail me a copy: I will read it here


0 new messages