next up previous contents
Next: 2.8 VRML Up: 2 Programiranje Previous: 2.6 PHIGS

2.7 Grafika v Windows

Programiranje grafike za okolje Windows je zahtevno, vendar je za osnovno risanje slikic dovolj ze enostaven program, ki ga lahko prevedemo s prevajalnikom za Windows. Obstaja tudi brezplacen prevajalnik Cygnus, ki zna prevajati win32 kodo. Prevajalnik je mozno dobiti tudi na nasem strezniku ftp.lecad.fs.uni-lj.si na podrocju /pub/vaje/install.

Program prevedemo z ukazom: gcc primer.c -o primer.exe -lgdi32 -luser32

#include <windows.h>

LRESULT FAR PASCAL AppWndProc(HWND hWnd, UINT msg, WPARAM wParam, 
                              LPARAM lParam);

HWND hAppWnd;

int WINAPI
 WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR
     lpCmdLine, int nCmdShow)
{
    MSG msg;
    static const char
     szWndClassName[] = {"MYWNDCLASS"};

    if (hPrevInstance == NULL) {
      WNDCLASS wc;
    wc.lpszClassName = szWndClassName;
    wc.hInstance = hInstance;
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = AppWndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hIcon = NULL;
    wc.hCursor = NULL;
    wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    if (!RegisterClass(&wc))
        return FALSE;
    }
    // Create our Main Application Window
    hAppWnd = CreateWindow(szWndClassName, "Test Application",
              WS_OVERLAPPEDWINDOW,  // Style of Window
              CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
              NULL, NULL, hInstance,
              NULL);

    ShowWindow(hAppWnd, nCmdShow);
    UpdateWindow(hAppWnd);

    while (GetMessage(&msg, NULL, 0, 0)) {
      TranslateMessage(&msg);   /* translates virtual key codes    */
      DispatchMessage(&msg);    /* dispatches message to window    */
    }

    return (int) msg.wParam;    /* return value of PostQuitMessage */
}


LRESULT FAR PASCAL
 AppWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    // this is where we receive all message concerning this window
    // we can either process a message or pass it on to the default
    // message handler of windows

    switch (msg) {
    case WM_CREATE:
    // Initialize Window
    // this message is received before the window is displayed
    break;
    case WM_PAINT:
    {
        static LOGPEN lpBlack =
        {PS_SOLID, 1, 1, RGB(0, 0, 0)}, lpWhite =
        {PS_SOLID, 1, 1, RGB(255, 255, 255)};

        PAINTSTRUCT ps;
        HDC hdc;
        RECT rect;
        HPEN hPenBlack, hPenWhite;
        int i;

        // Get the handle to the Windows's Display Context
        hdc = BeginPaint(hWnd, &ps);
        // Now Paint the Windows's contents
        // PaintAppWindow(hWnd,hdc,iPaintCount);
        // Call EndPaint to release the DC and validate

        hPenBlack = CreatePenIndirect(&lpBlack);
        hPenWhite = CreatePenIndirect(&lpWhite);

        // SetMapMode (hdc, MM_ANISOTROPIC);
        GetClientRect(hWnd, &rect);
        // SetViewportExtEx (hdc, rect.right, rect.bottom, NULL);
        // SetWindowExtEx(hdc, 10, 4, NULL);

        // Tu ri\v{s}emo \v{c}rto
        SelectObject(hdc, hPenBlack);
        MoveToEx(hdc, 1, 1, NULL);
        LineTo(hdc, 9, 1);

        DrawText(hdc, "Hello, Windows", -1, &rect,
             DT_SINGLELINE | DT_CENTER | DT_VCENTER);

        EndPaint(hWnd, &ps);
    }
    break;
    case WM_DESTROY:
    PostQuitMessage(0); // Terminate Application

    break;
    default:
    // We didn't process the message so let Windows do it
    return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    // We processed the message and there
    // is no processing by Windows necessary
    return 0L;
}



Leon Kos
Tue Mar 24 18:24:32 CET 1998