PDA

View Full Version : [C]Non mi risponde ai messaggi


Gino+89+
03-10-2006, 18:00
Guardate questo codice:


#include <windows.h>


#define DIM_MAX 30

#define IDC_MAIN_STATIC 101
#define IDC_MAIN_BUTTON1 102
#define IDC_MAIN_BUTTON2 103
#define IDC_MAIN_BUTTON3 104
#define IDC_MAIN_BUTTON4 105
#define IDC_MAIN_BUTTON5 106
#define IDC_MAIN_BUTTON6 107
#define IDC_MAIN_BUTTON7 108
#define IDC_MAIN_BUTTON8 109

typedef struct{
char numeroDiVolo[5]; /*chiave primaria*/
char compagnia[20];
double orarioDiPartenza;
double orarioDiArrivo;
int numPosti;
int numPostiPrenotati;
char destinazione[30];
}dati;

const char g_szClassName[]="WndClassPrincipale";
const char g_szControlStatic[]="STATIC";
const char g_szControlButton1[]="BUTTON";
const char g_szControlButton2[]="BUTTON";
const char g_szControlButton3[]="BUTTON";
const char g_szControlButton4[]="BUTTON";
const char g_szControlButton5[]="BUTTON";
const char g_szControlButton6[]="BUTTON";
const char g_szControlButton7[]="BUTTON";
const char g_szControlButton8[]="BUTTON";

LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);

int WINAPI
WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
dati array[DIM_MAX];
int numVoli=0,i;
WNDCLASSEX wc;
HWND hwnd,hWndStatic,hWndButton1,hWndButton2,hWndButton3,
hWndButton4,hWndButton5,hWndButton6,hWndButton7,hWndButton8;
MSG msg;

for(i=0;i<DIM_MAX;i++)
array[i].numPostiPrenotati=0;

/*Registrazione della Window Class*/
wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground =(HBRUSH)(COLOR_WINDOW+11);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = NULL;

if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Registrazione della Finestra Fallita!", "Errore!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

hwnd=CreateWindow(g_szClassName,"Es2 Compito vacanze info Peliz", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, 300, 440,
NULL, NULL, hInstance, NULL);

hWndStatic=CreateWindow(g_szControlStatic,"Compiti Vacanze Info Esercizio Num.2\n---Cappelli Gino---\nCreato con le Windows API",
WS_CHILD | WS_VISIBLE | SS_CENTER | SS_SUNKEN,
20, 350, 250, 50,
hwnd,(HMENU)IDC_MAIN_STATIC,hInstance, NULL);
hWndButton1=CreateWindow(g_szControlButton1,"Nuovo numero di volo",
WS_CHILD | WS_VISIBLE | BS_NOTIFY | BS_PUSHBUTTON,
40, 20, 200, 30,
hwnd,(HMENU)IDC_MAIN_BUTTON1,hInstance, NULL);
hWndButton2=CreateWindow(g_szControlButton2,"Cambio orario di partenza",
WS_CHILD | WS_VISIBLE | BS_NOTIFY | BS_PUSHBUTTON,
40, 60, 200, 30,
hwnd,(HMENU)IDC_MAIN_BUTTON2,hInstance, NULL);
hWndButton3=CreateWindow(g_szControlButton3,"Prenotazione di 1 o + posti",
WS_CHILD | WS_VISIBLE | BS_NOTIFY | BS_PUSHBUTTON,
40, 100, 200, 30,
hwnd,(HMENU)IDC_MAIN_BUTTON3,hInstance, NULL);
hWndButton4=CreateWindow(g_szControlButton4,"Cancellazione di 1 o + posti",
WS_CHILD | WS_VISIBLE | BS_NOTIFY | BS_PUSHBUTTON,
40, 140, 200, 30,
hwnd,(HMENU)IDC_MAIN_BUTTON4,hInstance, NULL);
hWndButton5=CreateWindow(g_szControlButton5,"Ricerca info su 1 volo",
WS_CHILD | WS_VISIBLE | BS_NOTIFY | BS_PUSHBUTTON,
40, 180, 200, 30,
hwnd,(HMENU)IDC_MAIN_BUTTON5,hInstance, NULL);
hWndButton6=CreateWindow(g_szControlButton6,"Stampa num. voli posti finiti",
WS_CHILD | WS_VISIBLE | BS_NOTIFY | BS_PUSHBUTTON,
40, 220, 200, 30,
hwnd,(HMENU)IDC_MAIN_BUTTON6,hInstance, NULL);
hWndButton7=CreateWindow(g_szControlButton7,"Stampa voli x h.part.crescente",
WS_CHILD | WS_VISIBLE | BS_NOTIFY | BS_PUSHBUTTON,
40, 260, 200, 30,
hwnd,(HMENU)IDC_MAIN_BUTTON7,hInstance, NULL);
hWndButton8=CreateWindow(g_szControlButton8,"Ricerca voli x 1 data destinaz.",
WS_CHILD | WS_VISIBLE | BS_NOTIFY | BS_PUSHBUTTON,
40, 300, 200, 30,
hwnd,(HMENU)IDC_MAIN_BUTTON8,hInstance, NULL);

ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);

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

return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
PostQuitMessage(0);
return 0;
break;

case BN_CLICKED:
{
case IDC_MAIN_BUTTON1:
MessageBox(hwnd, "Hello, World!", "Messaggio al mondo", MB_OK);
break;
}
break;
}

return DefWindowProc(hwnd,msg,wParam,lParam);
}


Ci sono delle strutture e variabili inizializzate inutilmente ma lasciate stare in quanto devi essere finito...

...nella WndProc non mi invia il MessageBox con Hello world in risposta al mio click sul primo bottone...perchè??...

andbin
03-10-2006, 19:26
...nella WndProc non mi invia il MessageBox con Hello world in risposta al mio click sul primo bottone...perchè??...Non ho provato realmente il tuo codice ma ti posso dire subito precisamente il perché. ;)

Il motivo è semplice: BN_CLICKED non è il messaggio che viene inviato! BN_CLICKED è solo un codice di notifica che viene inviato attraverso il messaggio WM_COMMAND.

Quindi, tradotto in codice:
switch(msg)
{
....

case WM_COMMAND:
{
switch (HIWORD (wParam)) /* Notification Code */
{
case BN_CLICKED:
{
switch (LOWORD (wParam)) /* Control Id */
{
case IDC_MAIN_BUTTON1:
MessageBox(hwnd, "Hello, World!", "Messaggio al mondo", MB_OK);
return 0;
}
break;
}
}
break;
}

...
}Nota che se rispondi al messaggio WM_COMMAND, dovresti ritornare 0.

Gino+89+
03-10-2006, 21:07
Grazie mille...