Click here to Skip to main content
15,069,297 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Especially for Eugen:
What r u speaking about? :) I can't understand u!
Guys!! can somebody tell me what Eugen speaks about? :)
CWnd??? g_wnd???
tried.. result is the same!!

Eugen... English is not my native language, but I've tried to explain what is the wrong. There where also words about CView and CDialog.
The whole program is posted here. U can compile it if you really want to know what I c.

Thanks to Jast_in. you reallye helped me.. catch my 5 :)

/* Edited after Eugen Podsypalnikov's post*/
As I've already mentioned I'm unable to change static controls colors. I.e. I want to change background and text colors of static control (and I do that in function CtlColor ) but the colors are still defaults.

and what I c now... I c static control with the default background color with some text in default text color.

what is not clear in the question?

/* end of edit */

This CStaticColor works fine when the control is placed on CDialog, or CView. But When I create window with CreateWindow I'm unable to change static control's colors.
CStaticColor::CtlColor is called for static control, but nothing is changed.
Can anybody explain me where is the problem and how to fix it?

//CStaticColor control class

class CStaticColor : public CStatic
{
	DECLARE_DYNAMIC(CStaticColor)

public:
	CStaticColor( COLORREF color );
	virtual ~CStaticColor() {};

	void			AttachChild( CStatic* attach ) { m_attach = attach; }
	HBRUSH			CtlColor( CDC * pDC, UINT nCtlColor );

private:
	CFont		m_font;
	COLORREF	m_color;
	CStatic*	m_attach;

protected:
	DECLARE_MESSAGE_MAP()
};


IMPLEMENT_DYNAMIC(CStaticColor, CStatic)

CStaticColor::CStaticColor( COLORREF color )
:
	m_color( color ),
	m_attach( 0 )
{
}

CStaticColor staticColor( RGB( 0, 0, 0 ) );

BEGIN_MESSAGE_MAP(CStaticColor, CStatic)
  ON_WM_CTLCOLOR_REFLECT()
END_MESSAGE_MAP()

HBRUSH CStaticColor::CtlColor( CDC* pDC, UINT nCtlColor ) 
{
	pDC->SetTextColor( m_color );
	pDC->SetBkColor( RGB( 255, 0, 0 ) );
	static CBrush brush( 0xffffff );

	return brush;
}
// end of CStaticColor control class

// WndProc
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam )
{
	
	switch(uMessage)
	{
	case WM_CTLCOLORSTATIC:
		::SendMessage( staticColor.GetSafeHwnd(), WM_CTLCOLORSTATIC, (WPARAM)GetDC( staticColor.GetSafeHwnd() ), (LPARAM)staticColor.GetSafeHwnd() );
		break;
		
	case WM_ERASEBKGND :
		return true;
		
	case WM_KEYDOWN:
		{
			switch (wParam)
			{
			case VK_ESCAPE:
				PostQuitMessage(0);
				break;
			}
			break;
		}
	}
	
	return DefWindowProc( hWnd, uMessage, wParam, lParam );
}
// end of WndProc

// Makewindow function
bool MakeWindow( int iWidth, int iHeight )
{
	WNDCLASS wndWc;
	
	wndWc.style = CS_OWNDC;
	wndWc.lpfnWndProc = (WNDPROC) WndProc;
	wndWc.cbClsExtra = 0;
	wndWc.cbWndExtra = 0;
	wndWc.hInstance = GetModuleHandle(NULL);
	wndWc.hIcon = NULL;
	wndWc.hCursor = LoadCursor(0, IDC_ARROW);
	wndWc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wndWc.lpszMenuName = NULL;
	wndWc.lpszClassName = WND_CLASS_NAME;
	
	if ( !RegisterClass( &wndWc ) ) 
		return false;
	
	int iSw = (WORD)GetSystemMetrics( SM_CXSCREEN );
	int iSh = (WORD)GetSystemMetrics( SM_CYSCREEN );
	
	RECT rc = { ( iSw - iWidth ) / 2, ( iSh - iHeight ) / 2, iWidth, iHeight };
	
	g_hwnd = CreateWindow( WND_CLASS_NAME, WND_CLASS_NAME, WS_POPUP, rc.left, rc.top, iWidth, iHeight, 
		NULL, NULL, GetModuleHandle( NULL ), NULL );
	
	return g_hwnd ? true : false;
}
// end of Makewindow

// WinMain
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
	if( !AfxWinInit( ::GetModuleHandle( NULL ), NULL, ::GetCommandLine(), 0 ) )
	{
		return 1;
	}
	if( !MakeWindow( 200, 200 ) ) 
		return -1;
	
	RECT rc;
	SetRect( &rc, 0, 50, 200, 100 );

	staticColor.Create( TEXT_IN_STATIC_CONTROL, WS_CHILD | WS_VISIBLE, rc, CWnd::FromHandle( g_hwnd ) );

	ShowWindow( g_hwnd, SW_SHOW );
	
	MSG mMsg;
	while( true )
	{
		if( PeekMessage( &mMsg, 0, 0, 0, PM_REMOVE ) )
		{
			if( mMsg.message == WM_QUIT )
				break; 
			TranslateMessage( &mMsg );
			DispatchMessage( &mMsg );
		}
	}

	DestroyWindow( g_hwnd );
	
	return 0;
}
// end of WinMain
Posted
Updated 23-Mar-10 4:02am
v8

Add a message handler for WM_PAINT to CStaticColor class and the function for it.

void CStaticColor::OnPaint() 
{
	CPaintDC dc( this );
	
	dc.SetBkColor( mBackgroundColor );
	dc.SetTextColor( mTextColor );
	
	CString txt;
	GetWindowText( txt );
	dc.TextOut( 0, 0, txt ); 
}


It should work!

Also u can remove function CtlColor and message handler for WM_CTLCOLORSTATIC from funcrion WndProc if u don't need them anymore.
   
v2
Please tell
what are you seeing now and
what are you waiting for ? :)

I post my local result here:


I think, that is correct.
When you will want to see the same -
just replace your usage of HWND g_hwnd with CWnd g_wnd :)


(CDialog and CView are derived from CWnd :)
Just provide your main window as CWnd object
and you will take your success again :) )
   
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900