Come si fa un programma per Windows con una finestra e un Menu? Ecco il procedimento per creare il programma; tutto spiegato in questo breve video. Riporto nella descrizione anche i sorgenti e i comandi di compilazione.
file -- miaico.rc -- rc = resource compiler; E' il file che descrive il Menu e le relative voci. Si compila con windres.
1 ICON "miaico.ico"
#define IDM_New 40001
#define IDM_Quit 40002
#define IDM_Copy 40003
#define IDM_Paste 40004
My_Menu MENU
BEGIN
POPUP "&File"
BEGIN
MENUITEM "&New", IDM_New
MENUITEM "&Quit", IDM_Quit
END
POPUP "&Edit"
BEGIN
MENUITEM "&Copy", IDM_Copy
MENUITEM "&Paste", IDM_Paste
END
END
File -- semplice.cpp -- E' il sorgente C++ che genera la finestra Windows con il Menu.
/* Creation of a simple Windows API program */
#include "windows.h"
#define IDM_New 40001
#define IDM_Quit 40002
#define IDM_Copy 40003
#define IDM_Paste 40004
/* Declare Windows procedure */
LRESULT CALLBACK ProceduraFinestra (HWND, UINT, WPARAM, LPARAM);
char nomeclasse[ ] = "WindowsApp";
int WINAPI
WinMain (HINSTANCE istanzaquestoprogramma,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND finestra; /* This is the handle for our window */
MSG messaggi; /* Here messaggi to the application are saved */
WNDCLASSEX strutturafinestra; /* Data structure for the windowclass */
/* The Window structure */
strutturafinestra.hInstance = istanzaquestoprogramma;
strutturafinestra.lpszClassName = nomeclasse;
strutturafinestra.lpfnWndProc = ProceduraFinestra; /* This function is called by windows */
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 = "My_Menu"; /* menu */
strutturafinestra.cbClsExtra = 0;
strutturafinestra.cbWndExtra = 0;
strutturafinestra.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&strutturafinestra))
return 0;
finestra = CreateWindowEx (
0,
nomeclasse, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
istanzaquestoprogramma, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (finestra, SW_SHOW);
/* Run the messaggio loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messaggi, NULL, 0, 0))
{
/* Translate virtual-key messaggi into character messaggi */
TranslateMessage(&messaggi);
/* Send messaggio to ProceduraFinestra */
DispatchMessage(&messaggi);
}
return messaggi.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK
ProceduraFinestra (HWND carciofi, UINT tagliatelle, WPARAM wParam, LPARAM lParam)
{
switch (tagliatelle)
{
case WM_DESTROY:
PostQuitMessage (0);
break;
case WM_COMMAND:
if (LOWORD(wParam) == IDM_Quit) {PostQuitMessage (0);};
if (LOWORD(wParam) == IDM_New) {MessageBox(NULL,"New","New",NULL);};
if (LOWORD(wParam) == IDM_Copy) {MessageBox(NULL,"Copy","Copy",NULL);};
if (LOWORD(wParam) == IDM_Paste) {MessageBox(NULL,"Paste","Paste",NULL);};
break;
default:
return DefWindowProc (carciofi, tagliatelle, wParam, lParam);
}
return 0;
}
E, infine, i comandi per compilare i vari sorgenti.
cmd
g++ -c semplice.cpp
windres miaico.rc miaico.o
g++ --machine-windows semplice.o miaico.o -o semplice.exe
Негізгі бет finestra windows menu
Пікірлер