Results 1 to 5 of 5

Thread: Error when returning a brush handle

  1. #1
    ChimpFace9000
    Guest

    Post 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?

  2. #2
    ChimpFace9000
    Guest
    Ive found out that the error happens whenever i access m_hBrushRed. I think this is because the function that calls OnCtlColorStaticRed is OnCtlColorStatic, and that is called by a static method of the class. And when OnCtlColorStatic is called, i dont think the "this" pointer is being pushed. If thats the case, then when OnCtlColorStaticRed tries to use the "this" pointer, it gets some unknown number and it tries to use protected memory, giving the error. Any ideas?

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I think it is because you use the DirectX color format when windows expects a COLORREF.
    Always use one of the three colorref macros (RGB, PALETTERGB or PALETTEINDEX) to create a COLORREF value.
    In your case: RGB(255, 255, 255) is white, RGB(0, 0, 0) is black and RGB(255, 0, 0) is red.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    There is another possibility:
    You should make sure that the pointer that you get from the create params is valid. It's just routine work.

    Another thing: I hope you are aware of the fact that this every additional dialog of this kind effectively replaces the one before! The static member of WndProc is the same for all class instances, therefore every new class will replace the last one.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    ChimpFace9000
    Guest
    Actually, i found out late last night that the initlization wasnt being sent. http://board.win32asmcommunity.net/s...&threadid=4117

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width