Lo que pretendo hacer es que el botón del menu "Salir", haga la misma función que si intento cerrar la aplicación, sin tener que repetir el código. Quiero saltar directamente al mensaje WM_CLOSE.
MainWindow.cpp:
// MainWindow
// Build Date: 02-08-2015
#ifndef UNICODE
#define UNICODE
#endif
#include <Windows.h>
#include "Resources.h"
#define SCREEN_HEIGHT 600
#define SCREEN_WIDTH 800
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
PCWSTR CLASS_NAME = L"MainWindow";
HWND hwnd;
MSG msg;
WNDCLASS wc = {};
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.hInstance = hInstance;
wc.lpfnWndProc = WindowProc;
wc.lpszClassName = CLASS_NAME;
wc.lpszMenuName = MAKEINTRESOURCE(ID_MENU);
RegisterClass(&wc);
hwnd = CreateWindowEx(0, CLASS_NAME, L"Pruebas Win32", WS_OVERLAPPEDWINDOW, 200, 200, SCREEN_WIDTH, SCREEN_HEIGHT,
NULL, NULL, hInstance, NULL);
if (!hwnd) return 0;
ShowWindow(hwnd, nCmdShow);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) {
case WM_CLOSE:
if (MessageBox(hwnd, L"¿Realmente desea salir?", L"Salir", MB_OKCANCEL) == IDOK) DestroyWindow(hwnd);
return 0;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_EXIT:
// Aqui quiero que salte a WM_CLOSE
return 0;
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
Resources.h
#define ID_MENU 100
//File
#define ID_NEW 101
#define ID_OPEN 102
#define ID_SAVE 103
#define ID_CLOSE 104
#define ID_EXIT 105
Resources.rc
#include "Resources.h"
ID_MENU MENU
BEGIN
POPUP L"Archivo"
BEGIN
MENUITEM L"Nuevo" ID_NEW
MENUITEM L"Abrir" ID_OPEN
MENUITEM L"Guardar" ID_SAVE
MENUITEM L"Cerrar" ID_CLOSE
MENUITEM SEPARATOR
MENUITEM L"Salir" ID_EXIT
END
END
Se me ocurre la solcion de aplicar una sentencia goto dentro de WM_CLOSE, pero me parece un poco perrenco. Alguna otra idea?