Skip to content

Simple Dialog

The minimal Win32 dialog procedure — set title and icon, wire up an edit box and static text, run a one-second timer.

Published Apr 29, 1995

The smallest complete dialog callback you can learn by heart. WM_INITDIALOG sets the caption and icon, grabs handles to the edit box (ID 1000) and the static label (ID 1001), seeds the edit with text, and starts a 1000 ms timer; WM_COMMAND copies the edit's contents into the label on Okay and ends the dialog on Exit; WM_CLOSE mirrors the exit path. The empty WM_TIMER handler is where periodic work would go. If you only remember one dialog structure from the older Win32 samples, make it this one - everything else in the folder is this shape plus more controls.

/////////////////////////////////////////////////////////////////////

extern int MainDilaog (HWND hDlg, int Msg, int wParam, int lParam)
{
	static HWND TextEdit_hWnd;
	static HWND TextStatic_hWnd;

	while(1) // Loop Forever
	{
		if(Msg==WM_INITDIALOG)
		{
			MainDialog_hWnd=hDlg;

			SendMessage(MainDialog_hWnd,(UINT)WM_SETTEXT,(WPARAM)0,(LPARAM)TitleCaption);
			SendMessage(MainDialog_hWnd,WM_SETICON,TRUE,(LPARAM)LoadIcon(GetWindowInstance(MainDialog_hWnd),MAKEINTRESOURCE(100)));

			TextEdit_hWnd=GetDlgItem(MainDialog_hWnd,1000);
			TextStatic_hWnd=GetDlgItem(MainDialog_hWnd,1001);

			SetText(TextEdit_hWnd,"Text Input.");
			SetTimer(MainDialog_hWnd,0,1000,NULL); //SET TIMER

			CenterWindow(MainDialog_hWnd);
			break;
		}

		if(Msg==WM_COMMAND)
		{
			if(wParam==100) // Okay (Dialog Command)

			{
				SetText(TextStatic_hWnd,GetText(TextEdit_hWnd));
			}

			if(wParam==101) // Exit (Dialog Command)
			{
				KillTimer(MainDialog_hWnd,1);
				EndDialog(MainDialog_hWnd,0);
			}
			break;
		}

		if(Msg==WM_CLOSE) //Exit (Windows Message)
		{
			KillTimer(MainDialog_hWnd,1);
			EndDialog(MainDialog_hWnd,0);
		}

		if(Msg==WM_TIMER) //Timer  (Windows Message)
		{

		}

		break;
	}
	return 0;
}

Note the classic while/break idiom: each message type is handled in its own block that breaks out of the loop, which keeps the old-style code flat instead of nested. Also note the timer-ID mismatch carried over from the original - SetTimer uses ID 0 but KillTimer uses ID 1; they should match or the timer will never be killed.

An unhandled error has occurred. Reload 🗙