Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: Bob1098 en 2 Agosto 2015, 22:05 pm



Título: Duda sobre manejo de mensajes Win32
Publicado por: Bob1098 en 2 Agosto 2015, 22:05 pm
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
  1. // MainWindow
  2. // Build Date: 02-08-2015
  3.  
  4. #ifndef UNICODE
  5. #define UNICODE
  6. #endif
  7.  
  8. #include <Windows.h>
  9. #include "Resources.h"
  10.  
  11. #define SCREEN_HEIGHT 600
  12. #define SCREEN_WIDTH 800
  13.  
  14. LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
  15.  
  16. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
  17. {
  18. PCWSTR CLASS_NAME = L"MainWindow";
  19.  
  20. HWND hwnd;
  21. MSG msg;
  22. WNDCLASS wc = {};
  23.  
  24. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  25. wc.hInstance = hInstance;
  26. wc.lpfnWndProc = WindowProc;
  27. wc.lpszClassName = CLASS_NAME;
  28. wc.lpszMenuName = MAKEINTRESOURCE(ID_MENU);
  29. RegisterClass(&wc);
  30.  
  31. hwnd = CreateWindowEx(0, CLASS_NAME, L"Pruebas Win32", WS_OVERLAPPEDWINDOW, 200, 200, SCREEN_WIDTH, SCREEN_HEIGHT,
  32. NULL, NULL, hInstance, NULL);
  33. if (!hwnd) return 0;
  34.  
  35. ShowWindow(hwnd, nCmdShow);
  36.  
  37. while (GetMessage(&msg, NULL, 0, 0))
  38. {
  39. TranslateMessage(&msg);
  40. DispatchMessage(&msg);
  41. }
  42.  
  43. return 0;
  44. }
  45.  
  46. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  47. {
  48. switch (uMsg) {
  49. case WM_CLOSE:
  50. if (MessageBox(hwnd, L"¿Realmente desea salir?", L"Salir", MB_OKCANCEL) == IDOK) DestroyWindow(hwnd);
  51.  
  52. return 0;
  53.  
  54. case WM_COMMAND:
  55. switch (LOWORD(wParam))
  56. {
  57. case ID_EXIT:
  58. // Aqui quiero que salte a WM_CLOSE
  59.  
  60. return 0;
  61. }
  62.  
  63. return 0;
  64. case WM_DESTROY:
  65. PostQuitMessage(0);
  66.  
  67. return 0;
  68. }
  69.  
  70. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  71. }

Resources.h
Código
  1. #define ID_MENU 100
  2.  
  3. //File
  4. #define ID_NEW 101
  5. #define ID_OPEN 102
  6. #define ID_SAVE 103
  7. #define ID_CLOSE 104
  8. #define ID_EXIT 105
  9.  

Resources.rc
Código:
#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?


Título: Re: Duda sobre manejo de mensajes Win32
Publicado por: ivancea96 en 2 Agosto 2015, 22:14 pm
¿Llamar a una función en ambos lugares?


Título: Re: Duda sobre manejo de mensajes Win32
Publicado por: Bob1098 en 2 Agosto 2015, 22:15 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?


Título: Re: Duda sobre manejo de mensajes Win32
Publicado por: ivancea96 en 2 Agosto 2015, 22:17 pm
¿Quieres enviar un mensaje WM_CLOSE? Si es así, tienes la función SendMessage.


Título: Re: Duda sobre manejo de mensajes Win32
Publicado por: Bob1098 en 2 Agosto 2015, 22:19 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 ;)


Título: Re: Duda sobre manejo de mensajes Win32
Publicado por: karmany en 2 Agosto 2015, 22:19 pm
Prueba a llamarla así:
Código
  1. PostMessage(hWnd, WM_CLOSE, 0, 0)
o
Código
  1. SendMessage(hWnd, WM_CLOSE, 0, 0)