CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Threaded View

  1. #1
    Join Date
    Aug 2011
    Location
    Poland
    Posts
    12

    SelectObject() doesn't work in non-static wndproc

    I have a base class for my window. All is working fine excluding SelectObject(). This function
    doesn't set font handle to device context, so text is painting using system font. Why? Because of different place in memory? Why other functions such as SetBkMode() work?

    P.S. Of course, always work, only one WM_PAINT. I typed two to show the essence of the problem.

    Code:
    // In this static function SelectObject() works, but not in non-static (NewWindowFunction):
    LRESULT CALLBACK CChildWnd::StaticWindowFunction(HWND hWnd, UINT uMsg, WPARAM wPar, LPARAM lPar)
    {
    	if (uMsg == WM_NCCREATE)
    		SetWindowLong(hWnd, GWL_USERDATA, (LONG) ((CREATESTRUCT *)lPar)->lpCreateParams);
    	CChildWnd *pSelf = (CChildWnd *)GetWindowLong(hWnd, GWL_USERDATA);
    
    	if (uMsg == WM_PAINT) // Only for test
    	{
    		PAINTSTRUCT ps;
    		HDC hDC;
    		HFONT hFont;
    		HFONT hOldFont;
    		RECT rect;
    
    		hDC = BeginPaint(hWnd, &ps);
    			hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
    			hOldFont = (HFONT)SelectObject(hDC, hFont); // Here it's ok!
    
    			GetClientRect(hWnd, &rect);
    			DrawTextExW(hDC, L"1111", 4, &rect, DT_LEFT | DT_NOPREFIX | DT_SINGLELINE | DT_INTERNAL, NULL);
    
    			SelectObject(hDC, hOldFont);
    		EndPaint(hWnd, &ps);
    
    		return (LRESULT) 1;
    	}
    
    	return pSelf->NewWindowFunction(hWnd, uMsg, wPar, lPar);
    }
    
    LRESULT CALLBACK CChildWnd::NewWindowFunction(HWND hWnd, UINT uMsg, WPARAM wPar, LPARAM lPar)
    {
    	PAINTSTRUCT ps;
    	HDC hDC;
    	HFONT hFont;
    	HFONT hOldFont;
    	RECT rect;
    
    	switch (uMsg)
    	{
    		case WM_PAINT:
    			hDC = BeginPaint(hWnd, &ps);
    			        hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
    				hOldFont = (HFONT)SelectObject(hDC, hFont); // Does not work.
    
    				GetClientRect(hWnd, &rect);
    				DrawTextExW(hDC, L"1111", 4, &rect, DT_LEFT | DT_NOPREFIX | DT_SINGLELINE | DT_INTERNAL, NULL);
    		
    				SelectObject(hDC, hOldFont);
    			EndPaint(hWnd, &ps);
    
    			return (LRESULT) 1;
    		break;
    		case WM_DESTROY:
    			PostQuitMessage(0);
    		break;
    		default:
    			return DefWindowProc(hWnd, uMsg, wPar, lPar);
    	}
    
    	return 0;
    }
    Last edited by Distrust; June 11th, 2012 at 04:37 PM.

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

Featured