Ver Mensaje Individual
  #6 (permalink)  
Antiguo 30/08/2011, 18:27
Anonimo12
 
Fecha de Ingreso: abril-2009
Ubicación: En foros del web, normalmente en Web general, HTML y CSS.
Mensajes: 258
Antigüedad: 15 años
Puntos: 3
Respuesta: Dudas de principiante en Win Api

Gracias ya me va a la perfección. Aprovecho el mismo tema para preguntar otra cosa y asi no spammeo:

Cómo puedo hacer para guardar un valor introducido en un edit, por ejemplo:

Tengo un edit--> el usuario introduce un texto o número ---> al darle a enter o a un botón ese texto/número se guarda en una variable. Creo y solo creo que lo he conseguido de la siguiente forma, pero al intentar imprimir en pantalla el valor no puedo:

Código C++:
Ver original
  1. #include <windows.h>
  2. #include <tchar.h>
  3.  
  4. enum {ID_boton1 = 1000, ID_edit1};
  5.  
  6. /*  Declare Windows procedure  */
  7. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  8.  
  9. /*  Make the class name into a global variable  */
  10. char szClassName[ ] = "CodeBlocksWindowsApp";
  11.  
  12.  
  13. HINSTANCE estancia;
  14. HWND boton1;
  15. HWND edit1;
  16.  
  17.  
  18. int WINAPI WinMain (HINSTANCE hThisInstance,
  19.                      HINSTANCE hPrevInstance,
  20.                      LPSTR lpszArgument,
  21.                      int nCmdShow)
  22. {
  23.     HWND hwnd;               /* This is the handle for our window */
  24.     MSG messages;            /* Here messages to the application are saved */
  25.     WNDCLASSEX wincl;        /* Data structure for the windowclass */
  26.  
  27.     estancia = hThisInstance;
  28.  
  29.     /* The Window structure */
  30.     wincl.hInstance = hThisInstance;
  31.     wincl.lpszClassName = szClassName;
  32.     wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
  33.     wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
  34.     wincl.cbSize = sizeof (WNDCLASSEX);
  35.  
  36.     /* Use default icon and mouse-pointer */
  37.     wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  38.     wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  39.     wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  40.     wincl.lpszMenuName = NULL;                 /* No menu */
  41.     wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
  42.     wincl.cbWndExtra = 0;                      /* structure or the window instance */
  43.     /* Use Windows's default colour as the background of the window */
  44.     wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  45.  
  46.     /* Register the window class, and if it fails quit the program */
  47.     if (!RegisterClassEx (&wincl))
  48.         return 0;
  49.  
  50.     /* The class is registered, let's create the program*/
  51.     hwnd = CreateWindowEx (
  52.            0,                   /* Extended possibilites for variation */
  53.            szClassName,         /* Classname */
  54.            "Code::Blocks Template Windows App",       /* Title Text */
  55.            WS_OVERLAPPEDWINDOW, /* default window */
  56.            CW_USEDEFAULT,       /* Windows decides the position */
  57.            CW_USEDEFAULT,       /* where the window ends up on the screen */
  58.            544,                 /* The programs width */
  59.            375,                 /* and height in pixels */
  60.            HWND_DESKTOP,        /* The window is a child-window to desktop */
  61.            NULL,                /* No menu */
  62.            hThisInstance,       /* Program Instance handler */
  63.            NULL                 /* No Window Creation data */
  64.            );
  65.  
  66.     /* Make the window visible on the screen */
  67.     ShowWindow (hwnd, nCmdShow);
  68.  
  69.     /* Run the message loop. It will run until GetMessage() returns 0 */
  70.     while (GetMessage (&messages, NULL, 0, 0))
  71.     {
  72.         /* Translate virtual-key messages into character messages */
  73.         TranslateMessage(&messages);
  74.         /* Send message to WindowProcedure */
  75.         DispatchMessage(&messages);
  76.     }
  77.  
  78.     /* The program return-value is 0 - The value that PostQuitMessage() gave */
  79.     return messages.wParam;
  80. }
  81.  
  82.  
  83.  
  84.  
  85. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  86. {
  87.  
  88.  
  89.  
  90.     PAINTSTRUCT ps;
  91.     HDC hdc;
  92.     TCHAR texto [256];   //no sé si esto está bien o tengo que decalararlo como "static char texto [256]"
  93.  
  94.     switch (message)
  95.     {
  96.         case WM_CREATE:
  97.         {
  98.  
  99.             //Botón y edit
  100.  
  101.             boton1 = CreateWindow (_T("Button"), _T("Texto"), BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE, 80, 100, 70, 25, hwnd, (HMENU) ID_boton1, estancia, 0);
  102.             edit1 = CreateWindow (_T("Edit"), 0, WS_CHILD | WS_VISIBLE, 160, 100, 100, 35, hwnd, (HMENU) ID_edit1, estancia, 0);
  103.             break;
  104.         }
  105.  
  106.         case WM_COMMAND:
  107.         switch (wParam)
  108.         {
  109.             // Al presionar el boton1 se obtiene lo que hay en el edit1 y se guarda en texto
  110.  
  111.             case ID_boton1:
  112.             GetWindowText(edit1, texto, 255);
  113.         }
  114.  
  115.         break;
  116.  
  117.  
  118.         case WM_PAINT:
  119.         hdc = BeginPaint(hwnd, &ps);
  120.  
  121.         TextOut(hdc, 200, 200, texto, _tcslen(texto));   //Imprime en pantalla lo que se ha guardado en la variable "texto"
  122.  
  123.         EndPaint(hwnd, &ps);
  124.         break;
  125.  
  126.         case WM_DESTROY:
  127.             PostQuitMessage (0);
  128.             break;
  129.         default:
  130.             return DefWindowProc (hwnd, message, wParam, lParam);
  131.     }
  132.  
  133.     return 0;
  134. }

He puesto comentarios después del "WindowProcedure" explicando lo que he hecho, de primeras puedo suponer que "TextOut" sirve solo para imprimir textos declarados anteriormente en el código y no los que se introducen en el edit y guardan en variables, por tanto no sé como se hace.

Saludos.
__________________
¿Por qué Anónimo?, porque como está el mundo no podemos considerarnos humanos...