Programmation de menus en API Win 32 C/C++ sous Visual Studio 2017
Pascalito
-
Pascalito -
Pascalito -
Bonjour,
Je voudrais faire, en API Win 32 C/C++ sous Visual Studio Community 2017, un programme affichant un menu que cela soit par programmation ou avec un fichier de ressource(.RC) sans MFC. Quelle est l'astuce que Microsoft ne donne pas dans son aide pour réaliser cela car aucun affichage n'apparait alors qu'en Borland il n'y a pas de problème ????
Ci-dessous mes lignes de programmes. Je part d'un projet vide et application windows(.exe).
Merci de votre aide.
#include <Windows.h>
#include "strsafe.h" // pour StringCbPrintf
#define IDC_ABOUT 10
#define IDC_EXIT 11
#define IDC_CALCUL 12
#define IDC_ABOUTBOX 13
#define IDC_CALCULBOX 14
HINSTANCE hinst;
// The main window class name.
static TCHAR szWindowClass[] = TEXT("win32app"); //ne s'affiche pas
// The string that appears in the application's title bar.
static TCHAR szTitle[] = TEXT("Win32 Application ProjetEssai");
// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND dlg, UINT message, WPARAM wparam, LPARAM lparam);
INT_PTR CALLBACK About(HWND dlg, UINT message, WPARAM wparam, LPARAM lparam);
INT_PTR CALLBACK Calcul(HWND dlg, UINT message, WPARAM wparam, LPARAM lparam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
UNREFERENCED_PARAMETER(lpCmdLine);
UNREFERENCED_PARAMETER(hPrevInstance);
HMENU menu, sousmenu;
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = NULL;// (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL, TEXT("Call to RegisterClassEx failed!"), TEXT("Win32 API"), NULL);
return 1;
}
//création de menus par programmation
sousmenu = CreateMenu();
AppendMenuA(sousmenu, MF_STRING, IDC_CALCUL,"Calcul");
AppendMenuA(sousmenu, MF_SEPARATOR,0,NULL);
AppendMenuA(sousmenu, MF_STRING, IDC_EXIT,"Quitter");
menu = CreateMenu();
AppendMenuA(menu, MF_POPUP,(UINT)sousmenu,(LPCSTR)"Fichier");
AppendMenuA(menu, MF_POPUP, (UINT)sousmenu,"About");
HWND hwnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 1000, 800, NULL, NULL, hinst, NULL);
if (!hwnd)
{
MessageBox(NULL,TEXT("Call to CreateWindow failed!"), TEXT("Win32 API"), NULL);
return 1;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Main message loop:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND dlg, UINT message, WPARAM wparam, LPARAM lparam)
{
int select;
switch (message)
{
case WM_COMMAND:
select = LOWORD(wparam);
switch (select)
{
case IDC_ABOUT:
DialogBox(hinst, MAKEINTRESOURCE(IDC_ABOUTBOX), dlg, About);
break;
case IDC_EXIT:
DestroyWindow(dlg);
break;
case IDC_CALCUL:
DialogBox(hinst, MAKEINTRESOURCE(IDC_CALCULBOX), dlg, Calcul);
break;
default:
return DefWindowProc(dlg, message, wparam, lparam);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(dlg, message, wparam, lparam);
}
return 0;
}
Je voudrais faire, en API Win 32 C/C++ sous Visual Studio Community 2017, un programme affichant un menu que cela soit par programmation ou avec un fichier de ressource(.RC) sans MFC. Quelle est l'astuce que Microsoft ne donne pas dans son aide pour réaliser cela car aucun affichage n'apparait alors qu'en Borland il n'y a pas de problème ????
Ci-dessous mes lignes de programmes. Je part d'un projet vide et application windows(.exe).
Merci de votre aide.
#include <Windows.h>
#include "strsafe.h" // pour StringCbPrintf
#define IDC_ABOUT 10
#define IDC_EXIT 11
#define IDC_CALCUL 12
#define IDC_ABOUTBOX 13
#define IDC_CALCULBOX 14
HINSTANCE hinst;
// The main window class name.
static TCHAR szWindowClass[] = TEXT("win32app"); //ne s'affiche pas
// The string that appears in the application's title bar.
static TCHAR szTitle[] = TEXT("Win32 Application ProjetEssai");
// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND dlg, UINT message, WPARAM wparam, LPARAM lparam);
INT_PTR CALLBACK About(HWND dlg, UINT message, WPARAM wparam, LPARAM lparam);
INT_PTR CALLBACK Calcul(HWND dlg, UINT message, WPARAM wparam, LPARAM lparam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
UNREFERENCED_PARAMETER(lpCmdLine);
UNREFERENCED_PARAMETER(hPrevInstance);
HMENU menu, sousmenu;
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = NULL;// (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL, TEXT("Call to RegisterClassEx failed!"), TEXT("Win32 API"), NULL);
return 1;
}
//création de menus par programmation
sousmenu = CreateMenu();
AppendMenuA(sousmenu, MF_STRING, IDC_CALCUL,"Calcul");
AppendMenuA(sousmenu, MF_SEPARATOR,0,NULL);
AppendMenuA(sousmenu, MF_STRING, IDC_EXIT,"Quitter");
menu = CreateMenu();
AppendMenuA(menu, MF_POPUP,(UINT)sousmenu,(LPCSTR)"Fichier");
AppendMenuA(menu, MF_POPUP, (UINT)sousmenu,"About");
HWND hwnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 1000, 800, NULL, NULL, hinst, NULL);
if (!hwnd)
{
MessageBox(NULL,TEXT("Call to CreateWindow failed!"), TEXT("Win32 API"), NULL);
return 1;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Main message loop:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND dlg, UINT message, WPARAM wparam, LPARAM lparam)
{
int select;
switch (message)
{
case WM_COMMAND:
select = LOWORD(wparam);
switch (select)
{
case IDC_ABOUT:
DialogBox(hinst, MAKEINTRESOURCE(IDC_ABOUTBOX), dlg, About);
break;
case IDC_EXIT:
DestroyWindow(dlg);
break;
case IDC_CALCUL:
DialogBox(hinst, MAKEINTRESOURCE(IDC_CALCULBOX), dlg, Calcul);
break;
default:
return DefWindowProc(dlg, message, wparam, lparam);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(dlg, message, wparam, lparam);
}
return 0;
}
A voir également:
- Programmation de menus en API Win 32 C/C++ sous Visual Studio 2017
- Microsoft visual c++ 2019 - Guide
- Power iso 32 bit - Télécharger - Gravure
- 32 bits - Guide
- Telecharger fl studio 20 pour pc gratuit complet - Télécharger - Édition & Montage
- Visual petanque - Télécharger - Sport
2 réponses
Bonjour,
Ligne ???? (si le code avait été posté en utilisant le bouton [<>] d'insertion code, j'aurais pu indiquer la ligne), tu crées un
Il faut passer ce
Ligne ???? (si le code avait été posté en utilisant le bouton [<>] d'insertion code, j'aurais pu indiquer la ligne), tu crées un
menuqui reste dans sa variable locale.
Il faut passer ce
menuà la fenêtre au moment de la création, effectué juste après ligne ????, c'est un des paramètres de la fonction
CreateWindow().