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?
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;
}
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 );
}
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;
}
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;
}