Ver Mensaje Individual
  #3 (permalink)  
Antiguo 29/08/2011, 13:52
Avatar de Munire
Munire
 
Fecha de Ingreso: julio-2010
Ubicación: en el ciberdespacio (España)
Mensajes: 150
Antigüedad: 13 años, 9 meses
Puntos: 4
Respuesta: Dudas de principiante en Win Api

Cita:
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
{
HWND hwnd_boton1 = CreateWindowEx (NULL, "BUTTON", "Texto", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 100, 100, 90, 25, hwnd, NULL, mi_instance, NULL);
break;
}

case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}

return 0;
}
la funcion createwindowEx() va antes del switch. y dentro del case WM_CREATE se pone la funcion SendMessage para mostrar lo que has creado


Cita:
si imprimo en pantalla un mensaje con "cout" en vez de salir en la ventana del programa me sale en una línea de comandos.
es que cout sirve solo para mostrar por consola.

te dejo un ejemplo para que veas como se crea un boton y como se pone texto



Código C++:
Ver original
  1. #include <windows.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <tchar.h>
  5.  
  6. const int MN_MENSAJE=1025;
  7. const int MN_SALIR=1026;
  8. const int MN_SAL=1027;
  9. // Global variables
  10.  
  11. // The main window class name.
  12. static TCHAR szWindowClass[] = _T("win32app");
  13.  
  14. // The string that appears in the application's title bar.
  15. static TCHAR szTitle[] = _T("Win32 Application");
  16.  
  17. HINSTANCE hInst, miinstance;
  18.  
  19. // Forward declarations of functions included in this code module:
  20. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  21.  
  22.  
  23. int WINAPI WinMain(HINSTANCE hInstance,
  24.                    HINSTANCE hPrevInstance,
  25.                    LPSTR lpCmdLine,
  26.                    int nCmdShow)
  27. {
  28.     WNDCLASSEX wcex;
  29.     miinstance = hInstance;
  30.     wcex.cbSize = sizeof(WNDCLASSEX);
  31.     wcex.style          = CS_HREDRAW | CS_VREDRAW;
  32.     wcex.lpfnWndProc    = WndProc;
  33.     wcex.cbClsExtra     = 0;
  34.     wcex.cbWndExtra     = 0;
  35.     wcex.hInstance      = hInstance;
  36.     wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
  37.     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
  38.     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
  39.     wcex.lpszMenuName   = NULL;
  40.     wcex.lpszClassName  = szWindowClass;
  41.     wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
  42.  
  43.     if (!RegisterClassEx(&wcex))
  44.     {
  45.         MessageBox(NULL,
  46.             _T("Call to RegisterClassEx failed!"),
  47.             _T("Win32 Guided Tour"),
  48.             NULL);
  49.        
  50.         return 1;
  51.     }
  52.  
  53.     hInst = hInstance; // Store instance handle in our global variable
  54.  
  55.     // The parameters to CreateWindow explained:
  56.     // szWindowClass: the name of the application
  57.     // szTitle: the text that appears in the title bar
  58.     // WS_OVERLAPPEDWINDOW: the type of window to create
  59.     // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
  60.     // 500, 100: initial size (width, length)
  61.     // NULL: the parent of this window
  62.     // NULL: this application dows not have a menu bar
  63.     // hInstance: the first parameter from WinMain
  64.     // NULL: not used in this application
  65.     HWND hWnd = CreateWindow(
  66.         szWindowClass,
  67.         szTitle,
  68.         WS_OVERLAPPEDWINDOW,
  69.         CW_USEDEFAULT, CW_USEDEFAULT,
  70.         500, 500,
  71.         NULL,
  72.         NULL,
  73.         hInstance,
  74.         NULL
  75.     );
  76.    
  77.     if (!hWnd)
  78.     {
  79.         MessageBox(NULL,
  80.             _T("Call to CreateWindow failed!"),
  81.             _T("Win32 Guided Tour"),
  82.             NULL);
  83.  
  84.         return 1;
  85.     }
  86.  
  87.     // The parameters to ShowWindow explained:
  88.     // hWnd: the value returned from CreateWindow
  89.     // nCmdShow: the fourth parameter from WinMain
  90.     ShowWindow(hWnd,
  91.         nCmdShow);
  92.     UpdateWindow(hWnd);
  93.  
  94.     // Main message loop:
  95.     MSG msg;
  96.     while (GetMessage(&msg, NULL, 0, 0))
  97.     {
  98.         TranslateMessage(&msg);
  99.         DispatchMessage(&msg);
  100.     }
  101.  
  102.     return (int) msg.wParam;
  103. }
  104.  
  105.  
  106. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  107. {
  108.     PAINTSTRUCT ps;
  109.     HDC hdc;
  110.     TCHAR greeting[] = _T("Hello, World!");
  111.     HMENU menu1 = CreateMenu();
  112.     HMENU menu2 = CreateMenu();
  113.     HMENU menu3 = CreateMenu();
  114.    
  115.         static HFONT hFont = CreateFont(14, 0, 0, 0, 100, 0, 0, 0,0, 0, 0, 0, VARIABLE_PITCH | FF_SWISS, _T("Helv"));
  116.         static HWND hwndButton1 = CreateWindowEx(0,_T("BUTTON"), _T("Nuestro boton"), WS_CHILD|WS_VISIBLE|WS_TABSTOP, 300, 300, 100, 20, hWnd, 0, miinstance, NULL);
  117.         static HWND hwndEdit1 = CreateWindowEx(0,_T("Edit"), _T("asd"), WS_CHILD|WS_VISIBLE|WS_TABSTOP, 100, 100, 100, 20, hWnd, 0, miinstance, NULL);
  118.  
  119.     switch (message)
  120.     {
  121.  
  122.     case WM_CREATE:
  123.         AppendMenu(menu2, MF_STRING, MN_MENSAJE, _T("Mensaje"));
  124.         AppendMenu(menu2, MF_STRING, MN_SALIR, _T("Salir"));
  125.         AppendMenu(menu3, MF_STRING, MN_SAL, _T("opc1"));
  126.         AppendMenu(menu3, MF_STRING, MN_SAL, _T("opc2"));
  127.         AppendMenu(menu1, MF_STRING | MF_POPUP, (UINT)menu2, _T("MiMenu"));
  128.         AppendMenu(menu1, MF_STRING | MF_POPUP, (UINT)menu3, _T("Opciones"));
  129.         SetMenu (hWnd, menu1);
  130.         SendMessage(hwndButton1, WM_SETFONT, (WPARAM) hFont, true);
  131.         SendMessage(hwndEdit1, WM_SETFONT, (WPARAM) hFont, true);
  132.  
  133.         break;
  134.      
  135.     case WM_COMMAND:
  136.         switch(LOWORD(wParam))
  137.         {
  138.         case MN_MENSAJE:
  139.             MessageBox(hWnd, _T("Hola Mundo"), _T("Title"), MB_OK);
  140.             break;
  141.         case MN_SALIR:
  142.             PostQuitMessage(0);
  143.             break;
  144.         }
  145.         break;
  146.  
  147.     case WM_PAINT:
  148.         hdc = BeginPaint(hWnd, &ps);
  149.  
  150.         // Here your application is laid out.   LPCWSTR
  151.         // For this introduction, we just print out "Hello, World!"
  152.         // in the top left corner.
  153.         TextOut(hdc,
  154.             200, 200,
  155.             greeting, _tcslen(greeting));
  156.        
  157.         // End application specific layout section.
  158.        
  159.         EndPaint(hWnd, &ps);
  160.         break;
  161.     case WM_DESTROY:
  162.         PostQuitMessage(0);
  163.         break;
  164.     default:
  165.         return DefWindowProc(hWnd, message, wParam, lParam);
  166.         break;
  167.     }
  168.  
  169.     return 0;
  170. }