proplem with messagebox

this is my first GUI program ..... has a weird error.... no compilation errors ...but when i opened it....it is supposed that when i hit ok ...a message should appear...the message controls showed no responding
also i couldnt bring the dialog box handle please help ...check the code:
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
#include <Windows.h>
#include <C:\Users\mohammed\Desktop\res\resource.h>
INT_PTR CALLBACK DlgProc(HWND hwnd,UINT mg,WPARAM wp,LPARAM lp);
MSG msg;
INT WINAPI wWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPWSTR lpCmdLine,int nShowCmd){
	MessageBox(0,L"test",L"test",0);
	 DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),0,DlgProc);
	while (GetMessage(&msg,0,0,0)>0)
	{TranslateMessage(&msg);
	DispatchMessage(&msg);}
	return msg.wParam;}
INT_PTR CALLBACK DlgProc(HWND hwnd,UINT mg,WPARAM wp,LPARAM lp){
	switch (mg)
	{case WM_INITDIALOG:
			return TRUE;break;
	case WM_COMMAND:
		switch(LOWORD(wp)){
		case IDOK:
			MessageBoxA(0,"Test","Test",MB_OK);
			break;
		case IDCANCEL:
			PostQuitMessage(0);break;
		default:
			break;
		}
	case WM_CLOSE:
		PostQuitMessage(0);break;
	default :
		break;}
	return DefWindowProc(hwnd,mg,wp,lp);}
That code has many problems.

1. Check DialogBox documentation:
DialogBox does not return control until the specified callback function terminates the modal dialog box by calling the EndDialog function.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms645452(v=vs.85).aspx

2. Your DlgProc must return TRUE if you process the message and FALSE if not, not calling DefWindowProc:
Typically, the dialog box procedure should return TRUE if it processed the message, and FALSE if it did not. If the dialog box procedure returns FALSE, the dialog manager performs the default dialog operation in response to the message.


3. You end a modal dialog by calling EndDialog(), not using PostQuitMessage:
Dialog boxes created by the DialogBox, DialogBoxParam, DialogBoxIndirect, and DialogBoxIndirectParam functions must be destroyed using the EndDialog function. An application calls EndDialog from within the dialog box procedure; the function must not be used for any other purpose.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms645472(v=vs.85).aspx


It is so hard to read the documentation ?
thanks a lot
enddialog function require a handle to the dialog box....how can i get it
It is the HWND parameter of the callback. In your example it is the variable named hwnd.
ok....but when i am using it like this:
 
SetDlgItemText(hwnd,IDC_STATIC,L"Test");

the function didnt work can you tell why
i thought that it was a wrong handle
You want to change the title? You need to do:

SetWindowText(hwnd,L"Test");
no .... i want to write to the label (static text) with ID :IDC_STATIC
You must then change that control's ID to another ID. The Controls who have the IDC_STATIC id cannot have their text changed in a simple way.
Go in your resource editor and choose another ID, and use that one.

Just letting you notice, MSVC by default sets all the Static Texts to the Same ID (which is exactly IDC_STATIC).
For the buttons and other controls, it adds another number, like IDC_BUTTON1, IDC_BUTTON2... That's the reason why you should switch IDC_STATIC.
Topic archived. No new replies allowed.