Thread: Colouring Edit Box Child Windows

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Colouring Edit Box Child Windows

    How do I colour the background of a edit box child window using the api (no crap MFC for me, thanks )

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227

    Smile

    *whispers conspiracy theory: search this board for WM_CTLCOLOREDIT and WM_CTLCOLORSTATIC*

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Also, if your interested in fancy edit boxes, then its probably time for you to look at rich edits....Ken's site has info...here's a basic one

    Code:
    #include <windows.h>
    #include <tchar.h>
    #include <richedit.h>
    
    LRESULT CALLBACK WndProc(HWND hWnd, 
    						 UINT msg, 
    						 WPARAM wParam, 
    						 LPARAM lParam)
    {
    
    	static HWND hEdWnd;
    	const COLORREF col = 0x00FF8800;//Half green,full blue
    	RECT rect;
    
    
     	switch(msg){
    	case WM_CREATE:
    
    		GetClientRect(hWnd,&rect);
    		
    
    		hEdWnd = CreateWindow(RICHEDIT_CLASS,
    							  NULL,
    							  WS_CHILD | WS_VISIBLE | ES_MULTILINE,
    							  rect.left,
    							  rect.top,
    							  rect.right,
    							  rect.bottom,
    							  hWnd,
    							  NULL,
    						      GetModuleHandle(NULL),
    							  NULL);
    		SendMessage(hEdWnd,
    					EM_SETBKGNDCOLOR,
    					0,
    					(LPARAM)col);
    
    	
    		break;
    	case WM_CLOSE:
    		PostQuitMessage(0);
    		break;
    	default:
    		return DefWindowProc(hWnd, msg, wParam, lParam);
    	}
    
    	return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInst,
    				   HINSTANCE,LPSTR,int nShow){
    
    	TCHAR szWindowName[]= _T("ColourEdit");
    	WNDCLASSEX wc= {0};
    	HWND hWnd;
    	MSG msg;
    
    	HMODULE hMod = LoadLibrary(_T("riched20.DLL")); //Need this!!
    	if(!hMod) {
    		return EXIT_FAILURE; 
    	}
    
    	wc.cbSize= sizeof(WNDCLASSEX);
    	wc.style= CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
    	wc.lpfnWndProc= WndProc;
    	wc.hInstance= hInst;
    	wc.hIcon= LoadIcon(NULL, IDI_WINLOGO);
    	wc.hCursor= LoadCursor(NULL,IDC_ARROW);
    	wc.lpszClassName= szWindowName;
    	wc.hbrBackground= CreateSolidBrush(RGB(255, 255, 255));
    	wc.hIconSm= LoadIcon(NULL, IDI_WINLOGO);
    
    	if(!RegisterClassEx(&wc))return EXIT_FAILURE;
    
    	hWnd= CreateWindowEx(WS_EX_CLIENTEDGE | WS_EX_ACCEPTFILES,
    						   szWindowName, 
    						   szWindowName, 
    						   WS_OVERLAPPEDWINDOW,
    						   CW_USEDEFAULT, 
    						   CW_USEDEFAULT, 
    						   400, 
    						   400, 
    						   HWND_DESKTOP, 
    						   NULL, 
    						   hInst, 
    						   NULL);
    	if(!hWnd) {
    		return EXIT_FAILURE; 
    	}
    
    	ShowWindow(hWnd, nShow);
    	UpdateWindow(hWnd);
    
    	while(GetMessage(&msg, NULL, 0, 0) > 0)
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	FreeLibrary(hMod);
    
    	return msg.wParam;
    		
    	return 0;
    }

  4. #4
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    *whispers: ok, time to thwart their attempts to keep us all gray and launch 'operation colour' *

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  2. Creating an Edit Box (Subclassing)
    By csonx_p in forum Windows Programming
    Replies: 9
    Last Post: 05-05-2008, 06:36 AM
  3. Replies: 9
    Last Post: 02-13-2008, 02:59 PM
  4. Replies: 3
    Last Post: 07-23-2005, 08:00 AM
  5. Edit Box Questions PT. II
    By Quantrizi in forum Windows Programming
    Replies: 16
    Last Post: 08-12-2003, 10:42 PM