Thread: Bitmap Program Help

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Question Bitmap Program Help

    Hello again,

    Trying to get back into windows programming after a brief 2 year hiatus; here is a bit o' code that is simply supposed to load a bitmap and display it on a window. My bitmap coding logic is based on this tutorial: http://www.winprog.org/tutorial/bitmaps.html

    I have commented out a lot of code (colored green) that was generating errors, but I am currently stuck with the following error(s):
    Code:
    
    C:\Users\Dsve\Desktop\blackjack\main.cpp|125|error: variable or field `command' declared void
    
    and more....
    My code may seem a bit hackish.. I am open to any suggestions.

    Code:
    
    #include <windows.h>
    #include "resource.h"
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "CodeBlocksWindowsApp";
    
    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 */
    
        /* 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) CreateSolidBrush(RGB(0,255,0));
    
        /* 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;
    }
    
    class Blackjack
    {
        public:
    
            //Constructor
            //Blackjack(HWND);
            //WinProc Functions
            void command(HWND hwnd, UINT uint, WPARAM wparam, LPARAM lparam);
            void create(HWND);
            int  destroy();
            //Destructor
            //~Blackjack();
    
        private:
    
        //HDC hdc;
        //PAINTSTRUCT ps;
        //HBITMAP hCard;
    
    };
    
    Blackjack myBlackjack;
    
    /*  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:   myBlackjack.create(hwnd);                              break;
            case WM_COMMAND:  myBlackjack.command(hwnd, message, wParam, lParam);    break;
            case WM_DESTROY:  myBlackjack.destroy();                                 break;
        }
    
        return 0;
    }
    /*
    Blackjack::Blackjack(hwnd)
    {
        //hdc = BeginPaint(hwnd, &ps);
        hCard = NULL;
    }
    
    ~Blackjack::Blackjack(hwnd);
    {
        //ReleaseDC(hwnd, hdc);
    }
    
    void Blackjack::create(HWND hwnd)
    {
         hCard = LoadBitmap(GetModuleHandle(NULL), MAKEINTORESOURCE(IDB_IMAGE1));
    }
    */
    void Blackjack::command(hwnd, uint, msg, wparam, lparam)
    {
    
        switch(msg)
        {
    
        case WM_PAINT:
    
            BITMAP bm;
            PAINTSTRUCT ps;
    
            HBITMAP hCard = LoadBitmap(GetModuleHandle(NULL), MAKEINTORESOURCE(IDB_IMAGE1));
    
            HDC hdc = BeginPaint(hwnd, &ps);
            HDC hdcMem = CreateCompatibleDC(hdc);
    
            HBITMAP hbmCard = (SelectObject(hdcMem, hCard));
    
            GetObject(hCard, sizeof(bm), &bm);
    
            BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
    
            SelectObject(hdcMem, hbmCard);
            DeleteDC(hdcMem);
    
            EndPaint(hwnd, &ps);
            break;
    
            //default:  return;
        }
    }
    
    int Blackjack::destroy()
    {
        PostQuitMessage(0);
        return 0;
    }



    using codeblocks IDE version 8.02
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    34
    You don't declare any types in the scope resolution for the command() function. Try this in stead:

    Code:
    void Blackjack::command(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
    Oh, and also, there is a difference in the parameter count between the declaration in the class:
    Code:
    void command(HWND hwnd, UINT uint, WPARAM wparam, LPARAM lparam);
    and the scope resolution:
    Code:
    void Blackjack::command(hwnd, uint, msg, wparam, lparam)
    You might want to sort that out.
    Last edited by Eirik; 03-13-2009 at 03:35 AM.

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Question

    thanks for the help...

    got everything to compile so far, but just have 1 error left:

    Code:
    C:\Users\Dsve\Desktop\blackjack\main.cpp|136|error: `MAKEINTORESOURCE' was not declared in this scope|
    Seems like this part of the code does not have access to the windows.h library for the makeintoresource macro... any suggestions?


    Also, I would like to point out that in this line:
    Code:
     HBITMAP hbmCard = (HBITMAP)(SelectObject(hdcMem, hCard));
    I had to cast the return value from SelectObject( ) to an HBITMAP.. the compiler originally gave me an, 'illegal conversion from void* to HBITMAP. Just wondering if this is acceptable since it wasn't in the tutorial.




    Code:
    /*  Window Creation and Registration Stuff......    */
    ...
    ...
    ...
    ...
    
    
    
    class Blackjack
    {
        public:
    
           //Constructor
            //Blackjack(HWND);
            //WinProc Functions
            void command(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
            void create(HWND);
            int  destroy();
            //Destructor
            //~Blackjack();
    
        private:
    
         //HDC hdc;
        //PAINTSTRUCT ps;
        //HBITMAP hCard;
    
    };
    
    Blackjack myBlackjack;
    
     /*  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:   myBlackjack.create(hwnd);                           break;
            case WM_COMMAND:  myBlackjack.command(hwnd, message, wParam, lParam);    break;
            case WM_DESTROY:  myBlackjack.destroy();                              break;
        }
    
        return 0;
    }
     /*
    Blackjack::Blackjack(hwnd)
    {
        //hdc = BeginPaint(hwnd, &ps);
        hCard = NULL;
    }
    
    ~Blackjack::Blackjack(hwnd);
    {
        //ReleaseDC(hwnd, hdc);
    }
    
    void Blackjack::create(HWND hwnd)
    {
         hCard = LoadBitmap(GetModuleHandle(NULL), MAKEINTORESOURCE(IDB_IMAGE1));
    }
    */
    void Blackjack::command(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
    {
    
        switch(msg)
        {
    
        case WM_PAINT:
    
            BITMAP bm;
            PAINTSTRUCT ps;
    
            HBITMAP hCard = LoadBitmap(GetModuleHandle(NULL), MAKEINTORESOURCE(IDB_IMAGE1));
    
            HDC hdc = BeginPaint(hwnd, &ps);
            HDC hdcMem = CreateCompatibleDC(hdc);
    
            HBITMAP hbmCard = (HBITMAP)(SelectObject(hdcMem, hCard));
    
            GetObject(hCard, sizeof(bm), &bm);
    
            BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
    
            SelectObject(hdcMem, hbmCard);
            DeleteDC(hdcMem);
    
            EndPaint(hwnd, &ps);
            break;
    
             //default:  return;
        }
    }
    
    int Blackjack::destroy()
    {
        PostQuitMessage(0);
        return 0;
    }
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    MAKEINTORESOURCE
    Seems to me like have a typO.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Ummm....

    WM_PAINT is not a subset of WM_COMMAND msgs. (you are switch'ing twice on the same UINT and expecting differing values)

    So the drawing code will not get called.

    Quote Originally Posted by The Brain View Post
    Also, I would like to point out that in this line:
    Code:
     HBITMAP hbmCard = (HBITMAP)(SelectObject(hdcMem, hCard));
    I had to cast the return value from SelectObject( ) to an HBITMAP.. the compiler originally gave me an, 'illegal conversion from void* to HBITMAP. Just wondering if this is acceptable since it wasn't in the tutorial.
    Is 'C" style cast and is required if using .c files (but not for .ccp)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM