DrawText will not draw to window


I have tried numerous examples in both MDI and SDI forms to get the following function to output anything to a window. The draw function is called from the main Callback loop.

Any ideas?

thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
				case IDM_EXIT1:
					PostMessage(hwnd, WM_CLOSE, 0, 0);
				break;
				case IDM_NEW1:
					SetDlgItemText(hwnd, IDC_MAIN_EDIT, "");
				break;
				case IDM_OPEN1:
					thisFile->DoFileOpen(hwnd);
				break;
				case IDM_DRAWME1:
					myDrawText(hwnd);
					UpdateWindow(hwnd);
				break;
				case IDM_SAVE_AS1:
					thisFile->DoFileSave(hwnd);
				break;
			}



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
51
52
53
54
55
56
void myDrawText(HWND thisHwnd){
// Start Font
		HFONT hf = 0;
		HDC hdc;
		long lfHeight;
		HGDIOBJ  g_hfFont = GetStockObject(DEFAULT_GUI_FONT);
		int i = 14;

		hdc = GetDC(thisHwnd);
		lfHeight = -MulDiv(12, GetDeviceCaps(hdc, LOGPIXELSY), 72);

		hf = CreateFont(lfHeight, 0, 0, 0, 0, TRUE, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");

		if(hf)
		{
			DeleteObject(g_hfFont);
			g_hfFont = (HGDIOBJ)hf;
		}
		else
		{
			MessageBox(thisHwnd, "Font creation failed!", "Error", MB_OK | MB_ICONEXCLAMATION);
		} 
// End font

// Start Text
		char szSize[100];
		char szTitle[]				= "These are the dimensions of your client area:";
		HGDIOBJ hfOld				= SelectObject(hdc, g_hfFont);
		COLORREF g_rgbText			= RGB(0,0,0);
		COLORREF g_rgbBackground	= RGB(255,255,255);
		BOOL g_bOpaque				= TRUE;
					
		RECT rc;
		LPRECT prc = &rc;
		GetClientRect(thisHwnd, prc);
		prc->top = 20;

		SetBkColor(hdc, g_rgbBackground);
		i = SetTextColor(hdc, g_rgbText);

		if(g_bOpaque){
			SetBkMode(hdc, OPAQUE);
		}
		else{
			SetBkMode(hdc, TRANSPARENT);
		}

		i = DrawText(hdc, szTitle, -1, prc, DT_WORDBREAK);
		i = wsprintf(szSize, "\n{This is the left = %d,\n This is the top = %d \n\n\n\n\n, %d \n, %d \n}", prc->left, prc->top, prc->right, prc->bottom);
		i = DrawText(hdc, szSize, i, prc, DT_CENTER | DT_VCENTER | DT_NOCLIP );

		SelectObject(hdc, hfOld);
		ReleaseDC(NULL, hdc);

// End Text
}
IT did something for me.
The formatting is a bit off but you can work on that.

However, please note that the writing may not stay (it may dissapear if your window gets covered then uncovered, or you if you miximize/minimize the window - basically any action that
causes windows to send a WM_PAINT message may causesome or all of the text to be erased)

EDIT:

PS - You should call DeleteObject on the font you created when you have finished with it.
1
2
3
        SelectObject(hdc, hfOld);
        DeleteObject(hf);//Delete the font - we have finished with it.
        ReleaseDC(NULL, hdc);
Last edited on
I solved my problem.

1
2
3
void myDrawText(HWND hwnd){
// Start Font
		HWND thisHwnd = GetWindow(hwnd, GW_CHILD);


the HWND in the Callback function is a handle to the frame, not the child. I called the child Window to the frame, and it now works.

I still think I am missing something fundamental with the drawtext function as the text appears to be painted on the screen, ignoring the cursor position, and refusing to activate the scroll feature.


Topic archived. No new replies allowed.