Questo video mostra un sorgente C di un programma che gira sotto Windows. IL programma genera 2 finestre, che rispondono a diversi eventi: WM_DESTROY, WM_KEYUP, WM_KEYDOWN, WM_MOUSEMOVE. Agli eventi WM_DESTROY e WM_KEYDOWN rispondono entrambe le finestre; invece agli eventi WM_KEYUP (tasto rilasciato) e WM_MOUSEMOVE (movimento del mouse) risponde soltanto una delle 2 finestre e l'altra no.
In particolare WM_KEYUP (tasto rilasciato), viene sentito da finestra. Se il tasto rilasciato è 'H' allora la finestra2 viene nascosta. Se il tasto rilasciato è 'V', la finestra2 viene mostrata. Riporto il sorgente C del programma...
#include windows.h
#include stdio.h
char nomeclasse[ ] = "WindowsApp";
char captionfinestra[ ] = "La mia finestra (H per nascondere finestra2, V per mostrare finestra2)\0";
char captionfinestra2[ ] = "La mia finestra2\0";
int larghezza = 600;
int altezza = 200;
int x,x2;
int y,y2;
HWND finestra,finestra2;
MSG messaggi,messaggi2;
WNDCLASSEX strutturafinestra;
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK ProceduraWindows(HWND oggettofinestra, UINT messaggiowindows, WPARAM wParam, LPARAM lParam)
{
if (messaggiowindows == WM_DESTROY) {PostQuitMessage (0);};
if (oggettofinestra == finestra) {if (messaggiowindows == WM_KEYUP) {printf("Tasto rilasciato... %c
",wParam); if (wParam == 'H') {ShowWindow (finestra2, SW_HIDE);};if (wParam == 'V') {ShowWindow (finestra2, SW_SHOW);};};};
if (messaggiowindows == WM_KEYDOWN) {printf("Tasto premuto... %c
",wParam);};
if (oggettofinestra == finestra2) {if (messaggiowindows == WM_MOUSEMOVE) {printf("IL mouse si muove...
");};};
if (messaggiowindows != WM_MOUSEMOVE && messaggiowindows != WM_KEYDOWN && messaggiowindows != WM_KEYUP && messaggiowindows != WM_DESTROY) {return DefWindowProc (oggettofinestra, messaggiowindows, wParam, lParam);};
}
int WINAPI
WinMain (HINSTANCE istanzaquestoprogramma,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
strutturafinestra.hInstance = istanzaquestoprogramma;
strutturafinestra.lpszClassName = nomeclasse;
strutturafinestra.lpfnWndProc = ProceduraWindows;
strutturafinestra.style = CS_DBLCLKS;
strutturafinestra.cbSize = sizeof (WNDCLASSEX);
strutturafinestra.hIcon = LoadIcon (NULL, IDI_APPLICATION);
strutturafinestra.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
strutturafinestra.hCursor = LoadCursor (NULL, IDC_ARROW);
strutturafinestra.lpszMenuName = NULL; /* No menu */
strutturafinestra.cbClsExtra = 0;
strutturafinestra.cbWndExtra = 0;
strutturafinestra.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&strutturafinestra))
return 0;
/* The class is registered, let's create the program*/
finestra = CreateWindowEx (
0,
nomeclasse,
captionfinestra,
WS_OVERLAPPEDWINDOW,
x,
y,
larghezza,
altezza,
HWND_DESKTOP,
NULL, /* No menu */
istanzaquestoprogramma,
NULL
);
finestra2 = CreateWindowEx (
0,
nomeclasse, /* Classname */
captionfinestra2, /* Title Text */
WS_OVERLAPPEDWINDOW,
x2, /* Windows decides the position */
y2, /* where the window ends up on the screen */
larghezza, /* The programs width */
altezza, /* and height in pixels */
HWND_DESKTOP,
NULL, /* No menu */
istanzaquestoprogramma,
NULL
);
/* Make the window visible on the screen */
ShowWindow (finestra, SW_SHOW); // SW_SHOW oppure SW_HIDE
ShowWindow (finestra2, SW_SHOW); // SW_SHOW oppure SW_HIDE
while (GetMessage (&messaggi, NULL, 0, 0))
{
/* Translate virtual-key messaggi into character messaggi */
TranslateMessage(&messaggi);
/* Send messaggio to ProceduraWindows */
DispatchMessage(&messaggi);
}
return messaggi.wParam;
while (GetMessage (&messaggi2, NULL, 0, 0))
{
/* Translate virtual-key messaggi into character messaggi */
TranslateMessage(&messaggi2);
/* Send messaggio to ProceduraWindows */
DispatchMessage(&messaggi2);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messaggi2.wParam;
}
Негізгі бет windows eventi
Пікірлер