htm=articulo/progc.htm ok articulo/progc.htm Programa Windows usando solo C.

Programa Windows usando solo C.

Programa Windows realizado en C que crea una ventana con barras de scroll.


//---------------------------------------------------------
// Ejemplo de manejo de scrollbars en C (Windows)
//---------------------------------------------------------
#include "windows.h"
//---------------------------------------------------------
//define maximos scroll
#define HMAX 10000
#define VMAX 10000
//define pagina scroll
#define HPAG  100
#define VPAG  100
//define linea scroll
#define HUNO   10
#define VUNO   10  
//---------------------------------------------------------
// Global Variables:
HINSTANCE hInst;
TCHAR szTitle[]="Prueba de barras de scroll.";
TCHAR szWindowClass[]="MyClass";
//---------------------------------------------------------
// Foward declarations of functions
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
VOID                Draw(HWND hWnd, HDC hdc);
//---------------------------------------------------------
// Entrada de la aplicación
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	MSG msg;
	hInst = hInstance; 
	MyRegisterClass(hInstance);
	if (!InitInstance (hInstance, nCmdShow)) 
		return FALSE;
	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)){
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}
//---------------------------------------------------------
//Registrar clase de ventana
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;
	wcex.cbSize = sizeof(WNDCLASSEX); 
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(NULL, IDI_APPLICATION);
	return RegisterClassEx(&wcex);
}
//---------------------------------------------------------
//inicializar instancia (crear ventana)
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;
   hWnd = CreateWindow(szWindowClass, szTitle, 
	  WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,
      CW_USEDEFAULT, 
	  CW_USEDEFAULT, 
	  CW_USEDEFAULT, 
	  CW_USEDEFAULT, 
	  NULL, NULL, hInstance, NULL);
   if (!hWnd) return FALSE;
   SetScrollRange(hWnd, SB_HORZ, 0, HMAX, FALSE);
   SetScrollRange(hWnd, SB_VERT, 0, VMAX, FALSE);
   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);
   return TRUE;
}
//---------------------------------------------------------
// procedimiento de ventana
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
	int vpos, hpos;
	switch (message) {
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		Draw(hWnd, hdc);
		EndPaint(hWnd, &ps);
		return 0L;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
    //------------------------------------------------------
    // se ha movido la barra vertical
	case WM_VSCROLL:
		vpos = GetScrollPos(hWnd, SB_VERT);
		switch (LOWORD(wParam)){
		case SB_TOP:       
			vpos  = 0;       
			break;
		case SB_BOTTOM:    
			vpos  = VMAX;	
			break;
		case SB_LINEUP:    
			vpos -= VUNO;	
			break;
		case SB_LINEDOWN:  
			vpos += VUNO;	
			break;
		case SB_PAGEUP:    
			vpos -= VPAG;	
			break;
		case SB_PAGEDOWN:  
			vpos += VPAG;	
			break;
		case SB_THUMBTRACK:
		case SB_THUMBPOSITION:
			vpos = (short int) HIWORD(wParam);
			break;
		}
		if(vpos <0) vpos="0;" if(vpos> VMAX) vpos = VMAX;
		SetScrollPos(hWnd, SB_VERT, vpos, TRUE);
		InvalidateRect(hWnd, NULL, TRUE);
		break;
    //------------------------------------------------------
    // se ha movido la barra horizontal
	case WM_HSCROLL:
		hpos = GetScrollPos(hWnd, SB_HORZ);
		switch (LOWORD(wParam)){
		case SB_TOP:       
			hpos  = 0;       
			break;
		case SB_BOTTOM:    
			hpos  = HMAX;	
			break;
		case SB_LINEUP:    
			hpos -= HUNO;	
			break;
		case SB_LINEDOWN:  
			hpos += HUNO;	
			break;
		case SB_PAGEUP:    
			hpos -= HPAG;	
			break;
		case SB_PAGEDOWN:  
			hpos += HPAG;	
			break;
		case SB_THUMBTRACK:
		case SB_THUMBPOSITION:
			hpos = (short int) HIWORD(wParam);
			break;
		}
		if(hpos <0) hpos="0;" if(hpos> VMAX) hpos = HMAX;
		SetScrollPos(hWnd, SB_HORZ, hpos, TRUE);
		InvalidateRect(hWnd, NULL, TRUE);
		return 0L;
    //------------------------------------------------------
    // manejo de las barras con el teclado
	case  WM_KEYDOWN:
		switch (wParam) {
	    case VK_HOME:
			PostMessage(hWnd, WM_VSCROLL, SB_TOP, 0L);
			break;
	    case VK_END:
			PostMessage(hWnd, WM_VSCROLL, SB_BOTTOM, 0L);
			break;
	    case VK_PRIOR:
			PostMessage(hWnd, WM_VSCROLL, SB_PAGEUP, 0L);
			break;
	    case VK_NEXT:
			PostMessage(hWnd, WM_VSCROLL, SB_PAGEDOWN, 0L);
			break;
	    case VK_UP:
			PostMessage(hWnd, WM_VSCROLL, SB_LINEUP, 0L);
			break;
	    case VK_DOWN:
			PostMessage(hWnd, WM_VSCROLL, SB_LINEDOWN, 0L);
			break;
	    case VK_LEFT:
			PostMessage(hWnd, WM_HSCROLL, SB_LINEUP, 0L);
			break;
	    case VK_RIGHT:
			PostMessage(hWnd, WM_HSCROLL, SB_LINEDOWN, 0L);
			break;
	    }
		break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}
//---------------------------------------------------------
//pintar ventana
VOID Draw(HWND hWnd, HDC hdc)
{
	TCHAR szText[256];
	RECT rt;
	GetClientRect(hWnd, &rt);
	wsprintf(szText, " %d X %d", 
		GetScrollPos(hWnd, SB_HORZ), 
		GetScrollPos(hWnd, SB_VERT));
	DrawText(hdc, szText, -1, &rt, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
}

© info3@maicas.net