DC not working

Hey,

I'm having a strange issue with these DC's. This is my code:

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
LRESULT CALLBACK
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc = 0;
	HDC tHDC = 0;
	HWND hWndApp = 0;

	switch(msg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;

	case WM_PAINT:
		hWndApp = FindWindow(0,"Test");

		hdc = BeginPaint(hWnd, &ps);
		tHDC = GetDC(hWndApp);
		
		BitBlt(hdc, 0, 0, 500, 500, tHDC, 0, 0, SRCCOPY);

		ReleaseDC(hWndApp, tHDC);
		EndPaint(hWnd, &ps);

		return 0;
	}
	return DefWindowProc(hWnd, msg, wParam, lParam);
}


I just get a white screen (I want a screenshot of the window with the caption "Test"). The strange thing is that, when I put tHDC INSIDE the case, it works.

This code works:
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
LRESULT CALLBACK
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc = 0;
	HWND hWndApp = 0;

	switch(msg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;

	case WM_PAINT:
                HDC tHDC = 0;
		hWndApp = FindWindow(0,"Test");

		hdc = BeginPaint(hWnd, &ps);
		tHDC = GetDC(hWndApp);
		
		BitBlt(hdc, 0, 0, 500, 500, tHDC, 0, 0, SRCCOPY);

		ReleaseDC(hWndApp, tHDC);
		EndPaint(hWnd, &ps);

		return 0;
	}
	return DefWindowProc(hWnd, msg, wParam, lParam);
}


Is there any reason why I should put it there and not global, like the other HDC's? I was thinking about DC leaks, but can't think of any here..

Thanks
Last edited on
I'm not that familiar with what you are doing, but it wouldn't surprise me if there were multiple invocations of WM_PAINT or other WNDPROC messages on the stack, causing rentrancy or variable scoping issues. With tHDC as a non-static global, each invocation of the WNDPROC will get a new one.
Hey,

But the strange this is: it works with all the other HDC's. Only this one can't be declared in the case part...
The original code - works for me.
Topic archived. No new replies allowed.