Why the DialogBox() function returns 0 instead of 10, or -10 ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<div style="color:Black;background-color:White; <pre>
#include <windows.h>
#include <span style="color:#A31515; "resource.h"

LRESULT CALLBACK WndProc(HWND, UINT, UINT, LONG);
BOOL CALLBACK DialogProc(HWND, UINT, WPARAM, LPARAM);

HINSTANCE ghInstance;

<span style="color:Blue; int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, <span style="color:Blue; int nCmdShow)
{
ghInstance = hInstance;

WNDCLASSEX wndclassx;

wndclassx.cbSize = <span style="color:Blue; sizeof(WNDCLASSEX);
wndclassx.style = CS_HREDRAW | CS_VREDRAW;
wndclassx.lpfnWndProc = WndProc;
wndclassx.cbClsExtra = 0;
wndclassx.cbWndExtra = 0;
wndclassx.hInstance = hInstance;
wndclassx.hIcon = NULL;
wndclassx.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclassx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndclassx.lpszMenuName = NULL;
wndclassx.lpszClassName = L<span style="color:#A31515; "WndProc";
wndclassx.hIconSm = NULL;

<span style="color:Blue; if( !RegisterClassEx(&wndclassx) ) <span style="color:Blue; return 0;

HWND hWnd = CreateWindow(L<span style="color:#A31515; "WndProc", L<span style="color:#A31515; "WndProc", WS_OVERLAPPEDWINDOW, 0, 0, 200, 300, NULL, NULL,
hInstance, NULL);

ShowWindow(hWnd, SW_MAXIMIZE);
UpdateWindow(hWnd);

MSG msg;

<span style="color:Blue; while( GetMessage(&msg, NULL, 0, 0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

<span style="color:Green; // Retorna msg.wParam

<span style="color:Blue; return (<span style="color:Blue; int)msg.wParam;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, UINT wParam, LONG lParam)
{
<span style="color:Blue; int i;

<span style="color:Blue; switch( message )
{
<span style="color:Blue; case WM_CREATE:

<span style="color:Blue; if( (i = DialogBox(ghInstance, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, (DLGPROC)DialogProc) == -1) ) <span style="color:Blue; return -1;
<span style="color:Blue; break;

<span style="color:Blue; default:

<span style="color:Blue; return DefWindowProc(hwnd, message, wParam, lParam);
}
<span style="color:Blue; return 0;
}

BOOL CALLBACK DialogProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
<span style="color:Blue; int ix;
<span style="color:Blue; switch( message )
{
<span style="color:Blue; case WM_COMMAND:

<span style="color:Blue; if( LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL )
{
<span style="color:Blue; if( HIWORD(wParam) == BN_CLICKED )
{
<span style="color:Blue; if( LOWORD(wParam) == IDOK ) ix = 10;
<span style="color:Blue; else ix = -10;

EndDialog(hDlg, ix);

<span style="color:Blue; return TRUE;
}
}
}
<span style="color:Blue; return FALSE;
}
[/code]
<br/>
Dialog box as defined in the resource file

IDD_DIALOG1 DIALOGEX 0, 0, 183, 89<br/>
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU<br/>
CAPTION "Dialog"<br/>
FONT 8, "MS Shell Dlg", 400, 0, 0x1<br/>
BEGIN
DEFPUSHBUTTON "OK",IDOK,39,37,50,14
PUSHBUTTON "Cancel",IDCANCEL,93,37,50,14
END



View the full article
 
Back
Top