Program hanging up on button click

Hello everyone,

I'm trying to build a simple dialog app wherein, when a user clicks a button, the entered text is diplayed in a message box. But whenever I click on the button, the app hangs up.

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
#include <windows.h>
#include <tchar.h>
#include "CustCtrl.h"
#include "resource.h"

BOOL CALLBACK DlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_INITDIALOG:
		SetDlgItemText(hwnd, IDC_CUSTOM1, _T("Custom Control Practice") );
		return TRUE;

	case WM_CLOSE:
		EndDialog(hwnd, 0);
		return TRUE;

	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case IDCANCEL: case IDOK:
			EndDialog(hwnd, 0);
			return TRUE;

		case IDShowText:
			
			TCHAR tcInput [256];
			GetWindowText (hwnd, tcInput, 256);
			MessageBox (hwnd, tcInput, "Test message", NULL);
			return TRUE;
		}

		return FALSE;
	}

	return FALSE;
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nShowCmd)
{
	InitCustomControl();

	DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), 0, DlgProc);
	return 0;
}


I'm so confused as to what I'm doing wrong.

Thanks a lot for the help!!

I figured out the problem. The program is in C and tchar isn't supported.
So now I'll just have to change all the C commands to C++. *sigh*
The only thing unusual I can see in your code in the handling of WM_CLOSE. This is not normally done (it's handled by the dialog WinProc that Windows runs for you.) See the example dialog procs here:

Using Dialog Boxes
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644996%28v=vs.85%29.aspx#modal_box

(I have seen some people using WM_CLOSE on the web, but it's not required under normal circumstances. There no sign of it in the Microsoft examples.)

Other than that, your code looks ok But I don't know what your custom control library is up to!

Andy
Last edited on
The program is in C and tchar isn't supported.

tchar is supported in C

But your code won't compile as C due to the placement of some of your variable declaration. You need to ove TCHAR tcInput [256]; to the top of the function if you want to compile as C

Andy

PS the last parameter of MessageBox is a UINT not some kind of pointer, so you should be using

MessageBox(hwnd, tcInput, "Test message", 0);

rather than

MessageBox (hwnd, tcInput, "Test message", NULL);
Last edited on
Thanks andy! :)
But your code won't compile as C due to the placement of some of your variable declaration. You need to ove TCHAR tcInput [256]; to the top of the function if you want to compile as C


That's because VS compiler is not C99 compliant, other C compilers does not have this problem.
Topic archived. No new replies allowed.