Trouble drawing from a function which has been passed the HDC

Hi,

I am using a single wndproc and calling specific functions in the wrapper objects corresponding to particular windows instances.

But it is not working: Here, I am trying to write some text in the window. The window object is passed the HDC but fails to write the text. If I insert exactly the same code in the actual wndproc, the text is written.

Any ideas what I am doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)   
{   
//... NB 'window' is a retrieved pointer to a window wrapper object
//    and 'ps' is a declared PAINTSTRUCT
 
		case WM_PAINT:
			{
				// Prepare the window for painting.			
				HDC hdc = BeginPaint (hwnd, &ps);
				int xPos=0;
				int yPos=0;
				//	Check we have not scrolled away from origin
				if ((styles&WS_HSCROLL)==WS_HSCROLL)
				{
					GetScrollInfo(hwnd,SB_HORZ,&si);
					xPos=si.nPos;
				}
				if ((styles&WS_HSCROLL)==WS_VSCROLL)
				{
					GetScrollInfo(hwnd,SB_VERT,&si);
					yPos=si.nPos;
				}
				RECT rect;
				GetClientRect(hwnd,&rect);

// -------------------------------------------
// If instead of passing to the On_WM_PAINT function, 
// the code is inserted here, there is no problem.
// -------------------------------------------

				// Pass to window instance
				window->On_WM_PAINT(hdc,rect,xPos,yPos);


				// End Paint
				EndPaint(hwnd,&ps);
				return 0;
			}
//...
}

bool Start_Up_Window::On_WM_PAINT(HDC hdc,RECT& rect,int,int)
{
	rect.top=rect.bottom/5;
	HFONT hf=CreateFont(100,0,0,0,550,0,0,0,0,0,0,0,0,0);
	SelectObject(hdc,hf);
	DrawText(hdc,_T("Title"),-1,&rect,DT_CENTER);
	return true;
}
This is unrelated but you have a copypasta error on line 19 (HSCROLL instead of VSCROLL)

Other than that I don't see anything wrong. Only thing I can think of is that On_WM_PAINT isn't being called for some reason? Did you forget to make it virtual or something?
Thanks.

You were right - On_WM_PAINT was not being called. It wasn't being called in the initial create window function (since I now have a two stage process because of the issue with calling a virtual On_WM_CREATE() in a WM_CREATE when in the wrapper object constructor), but I was trying to call it by calling InvalidateRect in my second stage (an initiation function for the window wrapper).

Turns out that I needed to also include an UpdateWindow call. Though I am not entirely sure why.

Cheers
Last edited on
Topic archived. No new replies allowed.