Drawing in Dialog Box

Is it possible to draw on Dialog Box? I've tried using WM_PAINT to draw and INITDIALOG to draw and no luck.

I'm trying to make a program where I can input quiz score and a push button outputs the quiz scores on a line graph.
It's possible to draw on a dialog box though unusual.
Can you show us the code of your WM_PAINT
Here's the code - three files:
Resource.h
1
2
3
4
5
6
7
8
#define IDD_PRIMARY_DLG   102
#define IDD_SECOND_DLG    104
#define IDC_CALCULATE_BTN    106
#define IDC_QUIZ1   108
#define IDC_QUIZ2    110
#define IDC_QUIZ3    112
#define IDC_QUIZ4    114
#define IDC_QUIZ5    116 

Resource.rc
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
#include <windows.h>
#include "Resource.h"
IDD_PRIMARY_DLG DIALOG 0, 0, 500, 400
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_APPWINDOW
CAPTION "Net Price Calculation"
FONT 8, "MS Shell Dlg"
BEGIN
LTEXT           "Quiz 1:", IDC_STATIC, 10, 11, 32, 8
EDITTEXT        IDC_QUIZ1, 65, 9, 45, 12, ES_RIGHT | ES_AUTOHSCROLL
LTEXT           "Quiz 2:", IDC_STATIC, 10, 32, 32, 8
EDITTEXT        IDC_QUIZ2, 65, 30, 45, 12, ES_RIGHT | ES_AUTOHSCROLL
LTEXT           "Quiz 3:", IDC_STATIC, 10, 53, 32, 8
EDITTEXT        IDC_QUIZ3, 65, 51, 45, 12, ES_RIGHT | ES_AUTOHSCROLL
LTEXT           "Quiz 4", IDC_STATIC, 10, 74, 32, 8
EDITTEXT        IDC_QUIZ4, 65, 72, 45, 12, ES_RIGHT | ES_AUTOHSCROLL
LTEXT           "Quiz 5", IDC_STATIC, 10, 95, 32, 8
EDITTEXT        IDC_QUIZ5, 65, 93, 45, 12, ES_RIGHT | ES_AUTOHSCROLL
//LTEXT           "%", IDC_STATIC, 112, 32, 10, 8
PUSHBUTTON      "Calculate", IDC_CALCULATE_BTN, 130, 30, 50, 14
PUSHBUTTON      "Close", IDCANCEL, 130, 70, 50, 14
END

/* -- Dialog Box: Second -- */

IDD_SECOND_DLG DIALOGEX 0, 0, 186, 90
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION |
WS_SYSMENU
CAPTION "Second"
FONT 8, "MS Shell Dlg"
BEGIN
DEFPUSHBUTTON   "OK", IDOK, 129, 7, 50, 14
PUSHBUTTON      "Cancel", IDCANCEL, 129, 24, 50, 14
END

Source.cpp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <windows.h>
#include "Resource.h"

#ifdef __BORLANDC__
#pragma argsused
#endif
//---------------------------------------------------------------------------
HWND hWnd;
HINSTANCE hInst;
const char *ClsName = "CallingDlg";
const char *WndName = "Calling One Dialog Box From Another";
LRESULT CALLBACK DlgProcPrimary(HWND hWndDlg, UINT Msg,
	WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK DlgProcSecondary(HWND hWndDlg, UINT Msg,
	WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	LPSTR lpCmdLine, int nCmdShow)
{
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_PRIMARY_DLG),
		hWnd, (DLGPROC)DlgProcPrimary);

	hInst = hInstance;
	return FALSE;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProcPrimary(HWND hWndDlg, UINT Msg,
	WPARAM wParam, LPARAM lParam)
{
	HWND hWndQuiz1, hWndQuiz2, hWndQuiz3, hWndQuiz4, hWndQuiz5;

	hWndQuiz1 = GetDlgItem(hWndDlg, IDC_QUIZ1);
	hWndQuiz2 = GetDlgItem(hWndDlg, IDC_QUIZ2);
	hWndQuiz3 = GetDlgItem(hWndDlg, IDC_QUIZ3);
	hWndQuiz4 = GetDlgItem(hWndDlg, IDC_QUIZ4);
	hWndQuiz5 = GetDlgItem(hWndDlg, IDC_QUIZ5);

	switch (Msg)
	{
	case WM_INITDIALOG:
		SetWindowText(hWndQuiz1, L"0.00");
		SetWindowText(hWndQuiz2, L"0.00");
		SetWindowText(hWndQuiz3, L"0.00");
		SetWindowText(hWndQuiz4, L"0.00");
		SetWindowText(hWndQuiz5, L"0.00");
		return TRUE;
	
	case WM_COMMAND:
		switch (wParam)
		{
		case IDC_CALCULATE_BTN:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_SECOND_DLG),
				hWnd, (DLGPROC)DlgProcSecondary);
			return FALSE;
		case IDCANCEL:
			EndDialog(hWndDlg, 0);
			return TRUE;
		}
		break;
	}

	return FALSE;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProcSecondary(HWND hWndDlg, UINT Msg,
	WPARAM wParam, LPARAM lParam)
{
	HDC hDC;
	PAINTSTRUCT Ps;

	switch (Msg)
	{
	case WM_INITDIALOG:
		return TRUE;
	case WM_PAINT:
	{
		const int SIZE = 5;
		int quiz[SIZE] = { 90, 80, 70, 40, 70 };
		int total = 0;
		int average = 0;
		hDC = BeginPaint(hWnd, &Ps);

		//times 3
		MoveToEx(hDC, 100, 400, NULL);
		LineTo(hDC, 100, 100);
		MoveToEx(hDC, 100, 400, NULL);
		LineTo(hDC, 400, 400);
		MoveToEx(hDC, 160, 390, NULL);
		LineTo(hDC, 160, 410);
		MoveToEx(hDC, 90, 340, NULL);
		LineTo(hDC, 110, 340);
		MoveToEx(hDC, 160, 400 - (quiz[0] * 3), NULL);
		Ellipse(hDC, 158, 400 - (quiz[0] * 3) - 2, 162, 400 - (quiz[0] * 3) + 2);
		LineTo(hDC, 220, 400 - (quiz[1] * 3));
		Ellipse(hDC, 218, 400 - (quiz[1] * 3) - 2, 222, 400 - (quiz[1] * 3) + 2);
		LineTo(hDC, 280, 400 - (quiz[2] * 3));
		Ellipse(hDC, 278, 400 - (quiz[2] * 3) - 2, 282, 400 - (quiz[2] * 3) + 2);
		LineTo(hDC, 340, 400 - (quiz[3] * 3));
		Ellipse(hDC, 338, 400 - (quiz[3] * 3) - 2, 342, 400 - (quiz[3] * 3) + 2);
		LineTo(hDC, 400, 400 - (quiz[4] * 3));
		Ellipse(hDC, 398, 400 - (quiz[4] * 3) - 2, 402, 400 - (quiz[4] * 3) + 2);
		TextOut(hDC, 250, 420, L"Quiz #", 6);
		TextOut(hDC, 20, 250, L"Quiz Grade", 11);
		EndPaint(hWnd, &Ps);
		break;
	}
	case WM_COMMAND:
		switch (wParam)
		{
		case IDOK:
			EndDialog(hWndDlg, 0);
			return TRUE;
		case IDCANCEL:
			EndDialog(hWndDlg, 0);
			return TRUE;

		}
		break;
	}

	return FALSE;
}
//--------------------------------------------------------------------------- 
Last edited on
It's a simple mistake. You need to pass the handle of the dialog to BeginPaint.
hDC = BeginPaint(hWnd, &Ps);
should be
hDC = BeginPaint(hWndDlg, &Ps);
Result:
https://pasteboard.co/GZFSUF6.jpg
Registered users can post here. Sign in or register to post.