Ver Mensaje Individual
  #4 (permalink)  
Antiguo 29/08/2011, 16:48
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

Instru: Gracias por tomarte las molestias con esa explicación, ya lo veo más claro :)

Munire: Asi que la declaración y definición de los botones/edit va fuera del Switch, mientras que la declaración de los menus va fuera y la definición en WM_CREATE; ya veo como se organiza.

Sobre poner un texto me funciona a la perfección, basándome en tu ejemplo he hecho uno sencillo:

Código C++:
Ver original
  1. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  2. {
  3.  
  4.     PAINTSTRUCT ps;
  5.     HDC hdc;
  6.     TCHAR texto[] = _T("Hello, World!");
  7.  
  8.  
  9.  
  10.     switch (message)                  /* handle the messages */
  11.     {
  12.         case WM_PAINT:
  13.         hdc = BeginPaint(hwnd, &ps);
  14.         TextOut(hdc, 200, 200, texto, _tcslen(texto));
  15.         EndPaint(hwnd, &ps);
  16.         break;
  17.  
  18.  
  19.         case WM_DESTROY:
  20.             PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
  21.             break;
  22.         default:                      /* for messages that we don't deal with */
  23.             return DefWindowProc (hwnd, message, wParam, lParam);
  24.     }
  25.  
  26.     return 0;
  27. }

El PAINTSTRUCT ps puedo suponer que es para crear un objeto (ps) de la estructura "PAINTSTRUCT", el HDC hdc será lo mismo pero no tengo ni idea de para qué sirve y el TCHAR está claro que es el texto en si.

Luego abajo hdc = BeginPaint(hwnd, &ps); me hago una idea por encima pero no lo tengo claro aunque he comprobado que puedo hacer lo siguiente:

Cita:
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

PAINTSTRUCT ps;
HDC hdc;
TCHAR texto[] = _T("Hello, World!");
TCHAR texto2[] = _T("Otro texto");



switch (message) /* handle the messages */
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
TextOut(hdc, 200, 200, texto, _tcslen(texto));
TextOut(hdc, 300, 300, texto2, _tcslen(texto2));
EndPaint(hwnd, &ps);

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;
}
Puedo crear varios textos sin necesidad de crear más "hdc" ni "paintstruct", es mas, he eliminido el EndPaint (hwnd, &ps) y no he encontrado ningún problema (¿por qué se debe finalizar la impresión?).

Ahora, aun poniendo la declaracón/definición del botón antes del switch no me deja crearle un evento, he probado 4583 cosas pero nada, puedo crear un evento a los menus y me va perfectamente pero no a los botones/edit. Cuando le doy clic al botón no consigo que realice ninguna acción ni cuando escribo un texto en los edit no me lo guarda en una variable. Aqui te dejo el código del mejor resultado que he conseguido creandole un evento a un botón:

Código C++:
Ver original
  1. #include <windows.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <tchar.h>
  5.  
  6.  
  7. HINSTANCE miinstance;
  8.  
  9.  
  10. /*  Declare Windows procedure  */
  11. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  12.  
  13. /*  Make the class name into a global variable  */
  14. char szClassName[ ] = "CodeBlocksWindowsApp";
  15.  
  16. int WINAPI WinMain (HINSTANCE hThisInstance,
  17.                      HINSTANCE hPrevInstance,
  18.                      LPSTR lpszArgument,
  19.                      int nCmdShow)
  20. {
  21.     HWND hwnd;               /* This is the handle for our window */
  22.     MSG messages;            /* Here messages to the application are saved */
  23.     WNDCLASSEX wincl;        /* Data structure for the windowclass */
  24.  
  25.     miinstance = hThisInstance;
  26.  
  27.  
  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. /*  This function is called by the Windows function DispatchMessage()  */
  84.  
  85. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  86. {
  87.  
  88.      HWND boton1 = CreateWindowEx (0,"BUTTON", "Texto", WS_CHILD|WS_VISIBLE|WS_TABSTOP, 100, 100, 100, 20, hwnd, 0, miinstance, NULL);
  89.  
  90.     switch (message)                  /* handle the messages */
  91.     {
  92.        case WM_COMMAND:
  93.             if((HWND)lParam==boton1)
  94.             {
  95.                 MessageBox (NULL, "Esto es un texto", "Título", MB_OK | MB_ICONEXCLAMATION);
  96.             }
  97.             break;
  98.  
  99.         case WM_DESTROY:
  100.             PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
  101.             break;
  102.         default:                      /* for messages that we don't deal with */
  103.             return DefWindowProc (hwnd, message, wParam, lParam);
  104.     }
  105.  
  106.     return 0;
  107. }

Se me queda pillada la ventana completamente vacia y al rato aparece el botón pero aunque le doy no hace nada. En fin no se me ocurren más cosas que probar y me he recorrido medio internet asi que vuelvo a recurrir a vosotros a ver si podéis decime cómo se hace.

Sientos las molestias, gracias por adelantado. Saludos.
__________________
¿Por qué Anónimo?, porque como está el mundo no podemos considerarnos humanos...

Última edición por Anonimo12; 29/08/2011 a las 17:00