Sign in to follow this  
corrington_j

why won't my dialog close

Recommended Posts

corrington_j    148
I am trying to use a dialog for my main window in a c++ win32 application and i can't get it to close when the x is clicked. Here is my code, I got it from charles petzolds programming windows book, so i can't see why it doesn't work. Oh also i am trying to do this with visual studio .net 2003. Do i need to change some of the dialog properties in the dialog editor.
#include <windows.h>

#include "resource.h"

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, PSTR cmdLine, INT cmdShow)
{
	char appName[] = "dialog";
	HWND hWnd;
	MSG msg;
	WNDCLASS wndClass;

	wndClass.style = CS_HREDRAW | CS_VREDRAW;
	wndClass.lpfnWndProc = WndProc;
	wndClass.cbClsExtra = 0;
	wndClass.cbWndExtra = DLGWINDOWEXTRA;
	wndClass.hInstance = hInst;
	wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndClass.lpszMenuName = NULL;
	wndClass.lpszClassName = appName;

	RegisterClass(&wndClass);

	hWnd = CreateDialog(hInst, MAKEINTRESOURCE(IDD_DIALOG1), 0, NULL);

	ShowWindow(hWnd, cmdShow);

	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}

	return DefWindowProc(hWnd, msg, wParam, lParam);
}
Thanks for the help

Share this post


Link to post
Share on other sites
Aprosenf    372
Add this to your dialog procedure:

case WM_SYSCOMMAND:
{
if(wparam == SC_CLOSE))
{
EndDialog(hwnd);

return TRUE;
}

break;
}


Also, you should NOT be calling DefWindowProc() with a dialog box. Return TRUE for messages that you handle, FALSE otherwise.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this