Unable to draw on button click

I've been learning win32, so been playing around with the code to understand things.
Using this tutorial : http://www.cplusplus.com/forum/windows/7033/
I've been trying to draw some lines when a button is clicked. But the difference is that, instead of creating the window and button and edit box in the code, I've designed the dialog box and controls using the resource editor.

When the button is clicked, I'm able to display a message box but unable to draw lines.

Any help is really appreciated!! Thanks! :)

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
// CppWin32Dialog.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "CppWin32Dialog.h"
#include <CommCtrl.h>
// For nice controls look
#pragma comment(linker, \
	"\"/manifestdependency:type='Win32' "\
	"name='Microsoft.Windows.Common-Controls' "\
	"version='6.0.0.0' "\
	"processorArchitecture='*' "\
	"publicKeyToken='6595b64144ccf1df' "\
	"language='*'\"")

#pragma comment(lib, "ComCtl32.lib")

INT_PTR CALLBACK DialogProc(HWND ,UINT ,WPARAM ,LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine,  int nCmdShow)
{
	HWND hDlg;
	MSG msg;
	BOOL ret;
	InitCommonControls();
	hDlg = CreateDialogParam(hInstance, MAKEINTRESOURCE(frmMain), 0, DialogProc, 0);
	ShowWindow(hDlg, nCmdShow);

	// Main message loop:
	while((ret = GetMessage(&msg, 0, 0, 0)) != 0)
	{
		if(ret == -1)
			return -1;

		if(!IsDialogMessage(hDlg, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}

INT_PTR CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{

	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDOK:
			PostQuitMessage(0);
			break;
		case IDCANCEL:
			::MessageBox (hDlg, "Checking cancel",
				"Retro Label", MB_ICONINFORMATION | MB_OK);
			break;

		// My button drawn in resource editor
		case IDShowText:
			/*// char array to hold the text from the textbox
			TCHAR szInput[MAX_PATH];

			// Obtains input from the textbox and puts it into the char array
			GetWindowText(hDlg, szInput, MAX_PATH);  

			// message box is correctly displayed
			::MessageBox (hDlg, L"Checking show text",
			L"Retro Label", MB_ICONINFORMATION | MB_OK);*/

			//No lines are drawn
			hdc = BeginPaint(hDlg, &ps);
			MoveToEx(hdc,100,100,0); // move the drawing position to 100, 100
			LineTo(hdc,150,150); // draw a line from the drawing position to 150, 150
			MoveToEx(hdc,200,200,0); // move the drawing position to 200, 200
			LineTo(hdc,300,200);
			EndPaint(hDlg, &ps);

		}
		break;
	case WM_PAINT:

		//Trying to draw a line
		/*hdc = BeginPaint(hDlg, &ps);
		MoveToEx(hdc, 60, 20, NULL);
		LineTo(hdc, 264, 122);
		// TODO: Add any drawing code here...
		EndPaint(hDlg, &ps);*/
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	case WM_CLOSE:
		PostQuitMessage(0);
		break;

	}
	return (INT_PTR)FALSE;
}

Do you even check MSDN documentation ?

An application should not call BeginPaint except in response to a WM_PAINT message. Each call to BeginPaint must have a corresponding call to the EndPaint function.

http://msdn.microsoft.com/en-us/library/windows/desktop/dd183362(v=vs.85).aspx
Thank you modoran. I found and rectified my mistake.

And I did not read the msdn documentation. As a beginner its very "intimidating and confusing". So I'm learning through tutorials.

Topic archived. No new replies allowed.