Duda sobre manejo de mensajes Win32

Iniciado por Bob1098, 2 Agosto 2015, 22:05 PM

0 Miembros y 1 Visitante están viendo este tema.

Bob1098

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:
Código (cpp) [Seleccionar]
// 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
Código (cpp) [Seleccionar]
#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?

ivancea96

¿Llamar a una función en ambos lugares?

Bob1098

Cita de: ivancea96 en  2 Agosto 2015, 22:14 PM
¿Llamar a una función?

También lo pensé, (gracias de todas formas) pero no hay forma de hacer lo que me he planteado?

ivancea96

¿Quieres enviar un mensaje WM_CLOSE? Si es así, tienes la función SendMessage.

Bob1098

Cita de: ivancea96 en  2 Agosto 2015, 22:17 PM
¿Quieres enviar un mensaje WM_CLOSE? Si es así, tienes la función SendMessage.

Perfecto, justo eso era lo que quería muchas gracias ;)

karmany

Prueba a llamarla así:
Código (cpp) [Seleccionar]
PostMessage(hWnd, WM_CLOSE, 0, 0)
o
Código (cpp) [Seleccionar]
SendMessage(hWnd, WM_CLOSE, 0, 0)