Ver Mensaje Individual
  #1 (permalink)  
Antiguo 29/08/2011, 10:52
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
Dudas de principiante en Win Api

Buenas. Postee esta duda en otro foro pero no he obtenido respuesta asi que la planteo aqui a ver si alguien sabe responderme.

Bien, he podido crear eventos a los menus/sub-menus como se puede ver en este ejemplo:

Código C++:
Ver original
  1. case WM_COMMAND:
  2.             switch(LOWORD(wParam))
  3.             {
  4.                 case item1_menu2:
  5.                     MessageBox(hwnd, "Mensaje de prueba", "Titulo del mensaje", MB_OK);
  6.                     break;
  7.                 case item2_menu2:
  8.                     PostQuitMessage(0);
  9.                     break;
  10.             }
  11.             break;

Como sigo algunas guías en inglés por falta de material en castellano no comprendí muy bien lo que hace switch(LOWORD(wParam)) pero puedo suponer razonando que es algo asi como "al darle clic a ...", corregídme si me equivoco.

Ahora, he intentado crear un evento a un botón y un edit pero no lo consigo; aqui os dejo el código (en rojo lo correspondiente al botón, lo demás es lo que te da como plantilla Code::Blocks):

Cita:
#include <windows.h>

/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/* Make the class name into a global variable */
char szClassName[ ] = "CodeBlocksWindowsApp";

HINSTANCE mi_instance;

int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
mi_instance = hThisInstance;

/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);

/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;

/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Code::Blocks Template Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);

/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);

/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}

/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}


/* This function is called by the Windows function DispatchMessage() */

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;
}
Para crear el evento he añadido debajo del WM_CREATE esto otro basándome en los ejemplos de los tutoriales (en rojo señalo dónde me da error):

Cita:
case WM_COMMAND:
{
if ((HWND)lParam==hwnd_boton1)
{
MessageBox(NULL, "Se ha presionado el boton", "Título", MB_OK | MB_ICONEXCLAMATION);
}
break;
}
De ahi me surge otra duda: ¿Por qué para crear eventos en los menus se usa (LOWORD(wParam)) y en botones ((HWND)lParam==hwnd_boton1) si en ambos casos se hace "clic" para activar el evento?.

Aparte de eso se me complica el hacer programas algo más currados por el hecho de no poder definir un "int main ()" ni funciones/clases (cuando lo hago el programa se salta la interfaz y vuelve a la linea de comandos); es mas, 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. Entonces, ¿dónde se declaran y definen las funciones/clases (en general cualquier acción que no sea WM_CREATE, WM_COMMAND...) en un programa creado con Win API?, ¿y se puede crear un "int main ()"? (ya he mencionado lo que me da como plantilla Code::Blocks).

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 11:06