cards.dll

cow 17.11.05 16:37

Kuinka käyttää cards.dll-tiedostoa ohjelmassa

 Tekstiversio  Arvo: 1 (1 ääntä)  Äänestä: +  -
/*
Esimerkki cards.dll:n käytöstä. Tiedosto ladataan ajon aikana dynaamisesti
LoadLibrary-funktiolla ja kuvat haetaan LoadBitmap-funktiolla. Täällä kun ei
näy liikaa win32-esimerkkejä olevan..

HBITMAP getCard(int number, int suit)
    number = kortin numero väliltä 1-13
    suit = maa väliltä 0-4. Järjestys: risti, ruutu, hertta, pata, kuvapuolet

void drawCard(HDC h_dc, int x, int y, int number, int suit)
    h_dc = DC, johon kortti piirretään
    x = kohdeDC:n x-koordinaatti
    y = arvaa.

Kääntynee mingw:llä seuraavasti: g++ cards.cpp -mwindows -o cards.exe

Screenshot: http://img434.imageshack.us/img434/5953/cardsdll1eg.png
*/


#include <windows.h>

LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
char szClassName[] = "Cards";
HINSTANCE h_instance;

class Cards {
public:
    bool init()
    {
        h_dll = LoadLibrary("cards.dll");
        if(!h_dll)
            return false;
        return true;
    }

    HINSTANCE getHandle(){ return h_dll; }

    HBITMAP getCard(int number, int suit)
    {
        HBITMAP h_bitmap;
        if(suit>4) suit = 4;
        if(suit<0) suit = 0;
        if(number>13) number = 13;
        if(number<1) number = 1;

        int card = number+(suit*13);
        h_bitmap = LoadBitmap(h_dll, MAKEINTRESOURCE(card));
        return h_bitmap;
    }

    void drawCard(HDC h_dc, int x, int y, int number, int suit)
    {
        HBITMAP card = getCard(number, suit);

        BITMAP bmp;
        GetObject(card, sizeof(bmp), &bmp);
        int w = bmp.bmWidth;
        int h = bmp.bmHeight;

        HDC h_srcdc = CreateCompatibleDC(h_dc);
        SelectObject(h_srcdc, card);
        // copy from hsrcdc to hdc
        BitBlt(h_dc, x, y, w, h, h_srcdc, 0, 0, SRCCOPY);

        // clean up
        DeleteObject(card);
        DeleteDC(h_dc);
        DeleteDC(h_srcdc);
    }

    ~Cards()
    {
        if(h_dll)
            FreeLibrary(h_dll);
    }
private:
    HINSTANCE h_dll;
};

int WINAPI WinMain(HINSTANCE h_instance, HINSTANCE h_pinstance, LPSTR arg, int cs)
{
    WNDCLASSEX wc;
    wc.hInstance  = h_instance;
    wc.lpszClassName = szClassName;
    wc.lpfnWndProc = WindowProcedure;
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.lpszMenuName = NULL;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;

    RegisterClassEx(&wc);

    HWND hwnd = CreateWindowEx( WS_EX_TOOLWINDOW, szClassName,
               szClassName, WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU,
               CW_USEDEFAULT, CW_USEDEFAULT, /* position on the screen */
               350, 250,                     /* width and height */
               HWND_DESKTOP, NULL, h_instance, NULL);

    ShowWindow(hwnd, cs);

    MSG messages;

    // the message loop
    while(GetMessage (&messages, NULL, 0, 0))
        TranslateMessage(&messages) | DispatchMessage(&messages);

    return messages.wParam;
}

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static HWND h_number, h_suit, updatebtn;
    static HFONT font;
    static char buffer[4] = {0,0,0,0};
    static Cards d;
    const int ID_UPDATEBTN = 10000;

    switch (msg)
    {
        case WM_CREATE:
            // get default font
            font = (HFONT)GetStockObject(DEFAULT_GUI_FONT);

            // create controls
            CreateWindow("static", "Number:", WS_CHILD|WS_VISIBLE,
             50, 25, 50, 20, hwnd, (HMENU) 0, h_instance, NULL);

            CreateWindow("static", "Suit:", WS_CHILD|WS_VISIBLE,
             150, 25, 50, 20, hwnd, (HMENU) 0, h_instance, NULL);

            h_number = CreateWindowEx (WS_EX_CLIENTEDGE, "edit", "11",
                WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL,
                50, 50, 50, 20, hwnd, (HMENU) 4, h_instance, NULL) ;

            h_suit = CreateWindowEx (WS_EX_CLIENTEDGE, "edit", "0",
                WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL,
                150, 50, 50, 20, hwnd, (HMENU) 8, h_instance, NULL) ;
           
            updatebtn = CreateWindow ("button", "Update image",
                WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON  ,
                230, 50, 80, 20, hwnd, (HMENU) ID_UPDATEBTN, h_instance, NULL);

            // set fonts
            SendMessage(h_number, WM_SETFONT, (WPARAM)font, MAKELPARAM(TRUE,0));
            SendMessage(h_suit,   WM_SETFONT, (WPARAM)font, MAKELPARAM(TRUE,0));
            SendMessage(updatebtn,WM_SETFONT, (WPARAM)font, MAKELPARAM(TRUE,0));

            if(!d.init()) {
                MessageBox (NULL, "Error loading cards.dll!\nQuitting..",
                            szClassName, MB_OK|MB_ICONERROR);
                DestroyWindow(hwnd);
                return 0;
            }
            break;
        case WM_COMMAND:
            switch( wParam )
            {
                case ID_UPDATEBTN:
                {
                    GetWindowText(h_number, buffer, 4);
                    int number = atoi(buffer);
                    memset(buffer, 0, 4);

                    GetWindowText(h_suit, buffer, 4);
                    int suit = atoi(buffer);
                    memset(buffer, 0, 4);

                    d.drawCard(GetDC(hwnd), 100, 100, number, suit);
                }
                break;
            } // wparam
            break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
            return 0;
       case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
       default:
           return DefWindowProc(hwnd, msg, wParam, lParam);
    } // msg
    return 0;
}