Cómo escribir una interfaz gráfica en lenguaje C
¿Este es el programa de interfaz más simple?:?//?c .cpp?:?Define el punto de entrada de la aplicación.
//
#include?"stdafx.h"
#include?"c .h"
#define?MAX_LOADSTRING ?100
//?Variable global:
HINSTANCE?hInst; //Instancia actual
TCHAR?szTitle[MAX_LOADSTRING] //Barra de título; Texto
TCHAR?szWindowClass[MAX_LOADSTRING]; //Nombre de clase de ventana principal
//?Declaración directa de funciones contenidas en este módulo de código:
ATOM MyRegisterClass(HINSTANCE?hInstance);
BOOL InitInstance(HINSTANCE,?int);
LRESULT?CALLBACK WndProc(HWND,?UINT,?WPARAM,?LPARAM);
INT_PTR?CALLBACK About(HWND,?UINT,?WPARAM,?LPARAM);
int?APIENTRY?_tWinMain(HINSTANCE?hInstance, HINSTANCE?hPrevInstance, LPTSTRlpCmdLine, intnCmdShow) p>
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine); //?TODO: ?Coloque el código aquí.
MSG?msg;
HACCEL?hAccelTable;
//Inicializar cadena global
LoadString(hInstance,?IDS_APP_TITLE,? szTitle,?MAX_LOADSTRING);
LoadString(hInstance,?IDC_C,?szWindowClass,?MAX_LOADSTRING);
MyRegisterClass(hInstance);
// inicialización de la aplicación:
if?(!InitInstance?(hInstance, ?nCmdShow))
{
return?FALSE;
}
hAccelTable?=?LoadAccelerators(hInstance,?MAKEINTRESOURCE(IDC_C));
///?Bucle de mensajes principal:
while?(GetMessage( amp; msg,?NULL,?0,?0))
{
if?(!TranslateAccelerator(msg.hwnd,?hAccelTable,?amp;msg))
p>{
TranslateMessage(amp; msg);
DispatchMessage(amp; msg);
}
}
return?(int)?msg.wParam;
}
//
//?Función:?MyRegisterClass()
//
//?Propósito:?Registrar clase de ventana.
//
//?Nota:
//
//Solo si lo desea
/ /Esta función y su uso sólo son necesarios por compatibilidad con sistemas ?Win32? anteriores cuando este código se agregó a "RegisterClassEx"
// la función se agregó a ?Windows?95? Es importante llamar a esta función
// para que la aplicación pueda obtener el pequeño icono
// "formateado correctamente" asociado.
//
ATOM?MyRegisterClass(HINSTANCE?hInstance)
{
WNDCLASSEX?wcex;
wcex.cbSize?=?sizeof(WNDCLASSEX);
wcex.style =?CS_HREDRAW?|?CS_VREDRAW;
wcex.lpfnWndProc =?WndProc;
wcex.cbClsExtra =?0;
wcex.cbWndExtra =?0;
wcex.hInstance =?hInstance;
wcex.hIcon =?LoadIcon(hInstance ,?MAKEINTRESOURCE(IDI_C));
wcex.hCursor =?LoadCursor(NULL,?IDC_ARROW);
wcex.hbrBackground =?(HBRUSH)(COLOR_WINDOW 1); p>
p>
wcex.lpszMenuName =?MAKEINTRESOURCE(IDC_C);
wcex.lpszClassName =?szWindowClass;
wcex.hIconSm =?LoadIcon(wcex. hInstance,?MAKEINTRESOURCE( IDI_SMALL));
return?RegisterClassEx(amp;wcex);
}
//
/ /Función:?InitInstance (HINSTANCE,?int)
//
//Propósito:?Guardar el identificador de instancia y crear la ventana principal
//
//Nota:
//
//En esta función, guardamos el identificador de instancia en la variable global y
//Crea y muestra la ventana principal del programa.
//
BOOL?InitInstance(HINSTANCE?hInstance, ?int?nCmdShow)
{
HWND?hWnd; p>
p>
hInst?=?hInstance;?//?Almacenar el identificador de instancia en una variable global
hWnd?=?CreateWindow(szWindowClass,?szTitle,?WS_OVERLAPPEDWINDOW, CW_USEDEFAULT ,?0,? CW_USEDEFAULT,?0,?NULL,?NULL,?hInstance,?NULL);
if?(!hWnd)
{ return?FALSE; p>
}
ShowWindow(hWnd,?nCmdShow);
UpdateWindow(hWnd);
return?TRUE;
}
//
//?Función:?WndProc(HWND,?UINT,?WPARAM,?LPARAM)
//
// ?Propósito:?Manejar mensajes desde la ventana principal.
//
//?WM_COMMAND -?Manejar el menú de la aplicación
//?WM_PAINT -?Pintar la ventana principal
/ /?WM_DESTROY -?Enviar mensaje de salida y regresar
//
//
LRESULT?CALLBACK?WndProc(HWND?hWnd,?UINT?message , ?WPARAM?wParam,?LPARAM?lParam)
{
int?wmId,?wmEvent;
PAINTSTRUCT?ps;
HDC?hdc;
cambiar?(mensaje)
{
caso?WM_COMMAND:
wmId=?LOWORD(wParam)
wmEvent?=?HIWORD(wParam);
//Selección del menú de análisis:
switch?(wmId)
{
case?IDM_ABOUT:
DialogBox(hInst, ?MAKEINTRESOURCE(IDD_ABOUTBOX), ?hWnd, ?About); p > case?IDM_EXIT:
DestroyWindow(hWnd);
break;
predeterminado:
return?DefWindowProc(hWnd,? mensaje ,?wParam,?lParam);
}
descanso;
caso?WM_PAINT:
hdc?=?BeginPaint( hWnd ,?amp;ps);
//TODO:?Agregue cualquier código de dibujo aquí...
EndPaint(hWnd,?amp;ps);
romper;
caso?WM_DESTROY:
PostQuitMessage(0);
romper
predeterminado:
return?DefWindowProc(hWnd,?message,?wParam,?lParam);
}
return?0;
}
//?Manejador de mensajes para el cuadro "Acerca de".
INT_PTR?CALLBACK?About(HWND?hDlg, ?UINT?message, ?WPARAM?wParam, ?LPARAM?lParam)
{
UNREFERENCED_PARAMETER(lParam );
cambiar?(mensaje)
{
caso?WM_INITDIALOG:
retorno?(INT_PTR)TRUE;
case?WM_COMMAND:
if?(LOWORD(wParam)?==?IDOK?||?LOWORD(wParam)?==?IDCANCEL)
{
EndDialog(hDlg,?LOWORD(wParam));
retorno?(INT_PTR)TRUE;
}
descanso; p>
p>
}
¿devolver?(INT_PTR)FALSE;
}