Advertisement
eduensarceno

ventana simple en C

Oct 26th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.99 KB | None | 0 0
  1. #include "pak.h"
  2. #include <Windows.h>
  3. #include <tchar.h>
  4.  
  5. #define save_instance(ins)\
  6.         do {                    \
  7.                 instance = ins; \
  8.         } while(0)
  9.  
  10. /* prototypes */
  11. static int register_class (void);
  12. static int create_window (HWND *window);
  13. static LRESULT CALLBACK class_proc (HWND window, UINT msg, WPARAM wparam,
  14.                                     LPARAM lparam);
  15. /* global variables */
  16. static HINSTANCE instance = NULL;
  17.  
  18. int APIENTRY _tWinMain (HINSTANCE ins, HINSTANCE pins, LPTSTR args, int show)
  19. {
  20.         save_instance(ins);
  21.  
  22.         if (register_class()) {
  23.                 MessageBox(NULL, TEXT("Error registrando ventana"),
  24.                            NULL, MB_OK | MB_ICONEXCLAMATION);
  25.                 return -1;
  26.         }
  27.  
  28.         HWND mwindow;
  29.         if (create_window(&mwindow)) {
  30.                 MessageBox(NULL, TEXT("Error creando ventana"),
  31.                            NULL, MB_OK | MB_ICONEXCLAMATION);
  32.                 return -1;
  33.         }
  34.  
  35.         ShowWindow(mwindow, show);
  36.         UpdateWindow(mwindow);
  37.  
  38.         MSG msg;
  39.         while (GetMessage(&msg, NULL, 0, 0) > 0) {
  40.                 DispatchMessage(&msg);
  41.         }
  42.  
  43.         return msg.wParam;
  44. }
  45.  
  46. static int register_class (void)
  47. {
  48.         WNDCLASS wc = { .lpfnWndProc    =    class_proc,
  49.                         .hInstance      =    instance,
  50.                         .hbrBackground  =    (void *)(COLOR_WINDOW + 1),
  51.                         .lpszClassName  =    TEXT("MainWindow") };
  52.  
  53.         return (RegisterClass(&wc) == 0);
  54. }
  55.  
  56. static int create_window (HWND *window)
  57. {
  58.         HWND _window = CreateWindow(TEXT("MainWindow"),
  59.                                     TEXT("Prueba"),
  60.                                     WS_OVERLAPPED,
  61.                                     CW_USEDEFAULT,
  62.                                     CW_USEDEFAULT,
  63.                                     CW_USEDEFAULT,
  64.                                     CW_USEDEFAULT,
  65.                                     NULL,
  66.                                     NULL,
  67.                                     instance,
  68.                                     NULL);
  69.         if (_window == NULL) {
  70.                 return -1;
  71.         }
  72.  
  73.         *window = _window;
  74.         return 0;
  75. }
  76.  
  77. static LRESULT CALLBACK class_proc (HWND window, UINT msg, WPARAM wparam,
  78.                                     LPARAM lparam)
  79. {
  80.         switch (msg) {
  81.         case WM_CLOSE:
  82.                 if (MessageBox(NULL,
  83.                                TEXT("¿Quieres cerrar la ventana?"),
  84.                                TEXT("Cerrar"),
  85.                                MB_YESNO | MB_ICONEXCLAMATION) == IDYES) {
  86.                         DestroyWindow(window);
  87.                         return 0;
  88.                 } else {
  89.                         return -1;
  90.                 }
  91.         case WM_DESTROY:
  92.                 PostQuitMessage(0);
  93.                 return 0;
  94.         default:
  95.                 return DefWindowProc(window, msg, wparam, lparam);
  96.         }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement