Error when returning a brush handle
Ive got a dialog as a class and i get an error when i return a brush handle on the wm_ctlcolorstatic message. This is how the class is set up...
cdlgmain.h
Code:
#pragma once
class CDlgMain
{
public:
CDlgMain(); // constructor
~CDlgMain(); // de-constructor
HWND hWnd(); // return window handle
const char* ClassName(); // returns pointer to class name
void ShowWindow(); // show the window in current pos
LRESULT OnCtlColorStatic(HWND, UINT, WPARAM, LPARAM); // called on WM_CTLCOLORSTATIC
LRESULT OnCtlColorStaticRed(HDC); // called by WM_CTLCOLORSTATIC
LRESULT OnDestroy(); // called on WM_DESTROY
private:
HBRUSH m_hBrushRed; // red brush
HCURSOR m_hCursorArrow; // normal arrow cursor
HICON m_hIconMain; // main icon
HINSTANCE m_hInstance; // applications instance handle
HWND m_hWnd; // handle of window
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // window procedure
};
CDlgMain::CDlgMain()
Code:
CDlgMain::CDlgMain()
{
WNDCLASSEX WndClassEx;
// get instance handle
m_hInstance = ::GetModuleHandle(NULL);
// load icons
m_hIconMain = ::LoadIcon(NULL, IDI_APPLICATION);
// load cursors
m_hCursorArrow = ::LoadCursor(NULL, IDC_ARROW);
// load brushes
m_hBrushRed = ::CreateSolidBrush(0xff);
// register window class
WndClassEx.cbClsExtra = 0;
WndClassEx.cbSize = sizeof(WNDCLASSEX);
WndClassEx.cbWndExtra = DLGWINDOWEXTRA;
WndClassEx.hbrBackground = (HBRUSH)COLOR_WINDOW;
WndClassEx.hCursor = m_hCursorArrow;
WndClassEx.hIcon = m_hIconMain;
WndClassEx.hIconSm = NULL;
WndClassEx.hInstance = m_hInstance;
WndClassEx.lpfnWndProc = WndProc;
WndClassEx.lpszClassName = ClassName();
WndClassEx.lpszMenuName = NULL;
WndClassEx.style = CS_HREDRAW | CS_VREDRAW;
::RegisterClassEx(&WndClassEx);
// create the window
m_hWnd = ::CreateDialogParam(m_hInstance, (LPCTSTR)IDD_MAIN, NULL, NULL, (long)this);
}
CDlgMain::WndProc()
Code:
LRESULT CALLBACK CDlgMain::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static CDlgMain* THIS;
switch(uMsg)
{
case WM_CTLCOLORSTATIC:
return(THIS->OnCtlColorStatic(hWnd, uMsg, wParam, lParam));
case WM_CREATE:
THIS = (CDlgMain*)((LPCREATESTRUCT)lParam)->lpCreateParams;
return(0);
case WM_DESTROY:
return(THIS->OnDestroy());
}
return(::DefWindowProc(hWnd, uMsg, wParam, lParam));
}
CDlgMain::OnCtlColorStatic
Code:
LRESULT CDlgMain::OnCtlColorStatic(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(GetDlgCtrlID((HWND)lParam))
{
case IDC_MAIN_STATIC_RED: return(OnCtlColorStaticRed((HDC)wParam));
}
return(::DefWindowProc(hWnd, uMsg, wParam, lParam));
}
CDlgMain::OnCtlColorStaticRed
Code:
LRESULT CDlgMain::OnCtlColorStaticRed(HDC hDC)
{
::SetTextColor(hDC, 0xffffff);
::SetBkColor(hDC, 0xff);
return((long)m_hBrushRed);
}
The problem is in OnCtlColorStaticRed when i return the handle to the red brush. I get an illegal operation error on it. I know it has something to do with the class itself because when i replace...
Code:
return((long)m_hBrushRed);
With...
Code:
return((long)::CreateSolidBrush(0xff)(;
Everything works fine.
Any ideas?