Thread: program ends immediately

  1. #1
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378

    program ends immediately

    hey, i'm bored and writing a hangman program as my first experience in dealing with images. as of now, the program compiles just fine but when i try to run it, it opens and then immediately closes. i'm using a wm_paint case almost identical to the one used in the Forger's Win32 tutorial over at winprog.net and the program would run just fine until i added the wm_paint case. any ideas why? thanks


    wow...today is not my day.
    i have:
    Code:
     case wm_paint:
    {
        ....
    }
    case wm_close:
        ...
    break;
    ..wowthatsucks. lol break's usually help in switches....
    Last edited by willc0de4food; 09-02-2005 at 10:04 PM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    you've placed a break or return at the end of your wm_paint right?

  3. #3
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    lol nope. *my bad*

    but for a new question, how do i make the picture go away so theres blank space? and then on a wrong answer, i want to display a new image. do i display the image but accessing the wm_paint case and then have like, a variable that tells which image to use? or is there a better way? thanks
    Registered Linux User #380033. Be counted: http://counter.li.org

  4. #4
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    I'm assuming you are using bitmaps? If you are a good way to decide which bitmap to use is an array. Like this one for example:
    Code:
    HBITMAP bitmapArray[4];  //assuming you are using 5 bitmaps
    ...    //load each bitmap and store the handle into the array
    ...    //and declare a variable to determine which one to use
    ...    //name the variable bitToUse for the example
    
    case WM_PAINT:
    HDC hdc=BeginPaint(hwnd,&ps);
    HDC hdcMem=CreateCompatibleDC(hdc);
    SelectObject(hdcMem,bitmapArray[bitToUse];
    BitBlt(hdc,x,y,bitWidth,bitHeight,hdcMem,0,0,SRCCOPY);
    DeleteDC(hdcMem);
    EndPaint(hwnd,&ps);
    return 0;
    Some of those parameters to BitBlt are of course variables(x,y,bitWidth,bitHeight).
    Hope that helps
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  5. #5
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    but then when i want to change the bitmap, how do i re-call the WM_PAINT case to make it change the bitmap?
    //load each bitmap and store the handle into the array
    what do you mean? or how? lol

    thanks
    Registered Linux User #380033. Be counted: http://counter.li.org

  6. #6
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Depends on how you load the bitmap, I usually do it from file so that's the way I will show you. In the resource file I add:
    Code:
    IDB_BITMAP1  BITMAP  "bitmap1.bmp"
    IDB_BITMAP2  BITMAP  "bitmap2.bmp"
    and load them into the array like this:
    Code:
    bitmapArray[0]=LoadBitmap(hInstance,TEXT("IDB_BITMAP1"));
    bitmapArray[1]=LoadBitmap(hInstance,TEXT("IDB_BITMAP2"));
    //load the rest of the bitmaps like this
    And finally for re-calling WM_PAINT do this:
    Code:
    SendMessage(hwnd,WM_PAINT,0,0);
    Any more questions just ask.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  7. #7
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    ok, thanks thats how i was loading bitmaps but i'd make an HBITMAP var for all of them, lol


    mm...with a character array, you have to have one extra spot for the NULL char. is there anything similar to this with a bmp array?
    Registered Linux User #380033. Be counted: http://counter.li.org

  8. #8
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Not to my knowledge but if you want to be safe, there's no hurt in declaring one extra space in the array. I'm pretty sure you don't NEED one but it's up to you.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  9. #9
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    that way of loading bitmaps didn't work. (the LoadBitmap(hInstance, TEXT("ID_PICWHATEVER"));

    but this is how i've been doing it:
    Code:
    bitmapArray[0] = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_PICWHATEVER));
      if (bitmapArray[0] == NULL)
        MessageBox(NULL, "Could not load Picwhatever.", "Error", MB_OK | MB_ICONERROR);
    but the pics still aren't changing this is my WM_PAINT case:
    Code:
    case WM_PAINT:
            {
                BITMAP bm;
                PAINTSTRUCT ps;
                
                HDC hdc = BeginPaint(hwnd, &ps);
                
                HDC hdcMem = CreateCompatibleDC(hdc);
                HBITMAP hbmOld = SelectObject(hdcMem, bitmapArray[showBmp]);
                GetObject(bitmapArray[showBmp], sizeof(bm), &bm);
                
                BitBlt(hdc, 14, 13, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
                
                SelectObject(hdcMem, hbmOld);
                DeleteDC(hdcMem);
                
                EndPaint(hwnd, &ps);
            }
            break;
    i have showBmp declared as an int and initialized to 7; then in an if statement in the check() function, if the guess was incorrect, it does --showBmp; and then SendMessage(hwnd, WM_PAINT, 0, 0); any ideas on what wrong?
    thanks.
    Last edited by willc0de4food; 09-04-2005 at 05:09 PM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  10. #10
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    hmm...after the --showBmp, if i do
    Code:
    InvalidateRect(hwnd, 0, TRUE);
    then it updates the picture.
    thanks for all your help.
    Registered Linux User #380033. Be counted: http://counter.li.org

  11. #11
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    No problem, and yes InvalidateRect works too because when you call that function it sends a WM_PAINT message because you are telling it the "rect" is invalid and needs to be repainted. Maybe my way wasn't working because hInstance wasn't declared in that scope? GetModuleHandle returns a HINSTANCE which is hInstance. MAKEINTRESOURCE is only if you define an ID for the bitmap like :
    Code:
     #define IDB_IMAGE4 3000
    I don't know the proper range of integers that bitmaps use I forgot so don't quote me on that part.
    Last edited by jmd15; 09-04-2005 at 06:08 PM.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  12. #12
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    MSDN
    "The WM_PAINT message is generated by the system and should not be sent by an application. "

    Use InvalidateRect() (followed by an UpdateWindow() to bypass the OS que and post directly to the windows callback).
    "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. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM