Results 1 to 11 of 11

Thread: this code does not show a bitmap!

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    this code does not show a bitmap!

    Please look at the following source code. I have a bimap in my resource file and I want to first load that bitmap into the memory DC and then I want to copy it on to the screen
    But the following code just shows me an error message:
    The message is my own. I call it whenver i cannot load the image from the resource file:
    Code:
    #include <windows.h>
    #include "resource.h"
    #define TheClass "myclass"
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
    {
    	switch(msg)
    	{
    	case WM_CREATE:
    	HBITMAP hbball;
    	hbball = LoadBitmap(NULL,  "BALLBMP");
    	if(!hbball)
    	{
         MessageBox(hwnd, "Load of resource BALLBMP failed.", "Error",
             MB_OK | MB_ICONEXCLAMATION);
    	}
    	 return 0;
    	case WM_PAINT:
    	if(hbball)
    		{
    		HDC winhdc, memhdc;
    		PAINTSTRUCT ps;
    		BITMAP bm;
    		winhdc =  BeginPaint(hwnd, &ps);
    		memhdc = CreateCompatibleDC(winhdc);
    		SelectObject(memhdc, hbball);
    		GetObject(hbball, sizeof(bm), &bm);
    		BitBlt(winhdc,0,0,bm.bmWidth, bm.bmHeight, memhdc, 0,0,SRCCOPY);
    		DeleteDC(memhdc);
    		EndPaint(hwnd, &ps);
    		}
    		return 0;
    	case WM_CLOSE:
    		DestroyWindow(hwnd);
    		return 0;
    	case WM_DESTROY:
    		DeleteObject(hbball);
    		PostQuitMessage(0);
    		return 0;
    	}
    
    	return DefWindowProc(hwnd, msg, wparam, lparam);
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE HPrevInstance, LPSTR lpCmdLine, int ShowCmd)
    {
    	WNDCLASSEX wc;
    	HWND hwnd;
    	MSG msg;
    
    	//Creates the window class
    
    	wc.cbClsExtra = 0;
    	wc.cbSize = sizeof(wc);
    	wc.cbWndExtra = 0;
    	wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
    	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hInstance = hInstance;
    	wc.lpfnWndProc = WndProc;
    	wc.lpszClassName = TheClass;
    	wc.lpszMenuName = MAKEINTRESOURCE(MYMENU);
    	wc.style = CS_VREDRAW | CS_HREDRAW;
    
    	//Registers the Window Class -- if it does not succeed then it
    	//shows an error message.
    
    	if(!RegisterClassEx(&wc))
    	{
    		MessageBox(NULL, "Error occurred while \
    		Registering the Main Class", "Error!", MB_OK);
    		return 0;
    	}
    
    	//Creates the main window hwnd
    
    	hwnd = CreateWindow(TheClass,
    					"The Ball Example",
    					WS_OVERLAPPEDWINDOW,
    					0,
    					0,
    					500,
    					500,
    					NULL,
    					NULL,
    					hInstance,
    					NULL);
    
    	//Check if there is any error when we make our windows' Hwnd
    
    	if(hwnd == NULL)
    	{
    		MessageBox(NULL, "Error making the Hwnd for our window","Error",MB_OK);
    	return 0;
    	}
    
    	ShowWindow(hwnd, SW_SHOW);
    	UpdateWindow(hwnd);
    
    	while(GetMessage(&msg, NULL, 0,0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	return msg.wParam;
    }


    Do you know what is rong with that?
    Baaaaaaaaah

  2. #2
    denniswrenn
    Guest
    Try this if you are using Visual C++:

    Code:
    LoadBitmap(NULL,  MAKEINTRESOURCE(BALLBMP));

  3. #3

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    i HAVE DONE THAT

    I have tried that but it still does not load the bitmap
    Baaaaaaaaah

  4. #4
    denniswrenn
    Guest
    Try this then:

    Code:
    //global(at the top)
    HINSTANCE hInst; 
    
    //somewhere in winmain
    hInst = hInstance;
    
    //WM_PAINT
    LoadBitmap(hInstance,  MAKEINTRESOURCE(BALLBMP));

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    LoadBitmap(GetModuleHandle(NULL),  MAKEINTRESOURCE(BALLBMP));
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    Now it does not show anything

    Now it does does not show the error message but now it does show anything on my winodw.
    Baaaaaaaaah

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    SelectObject(memhdc, hbball);
    Change that memhdc to winhdc and see what happens.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Here:

    PHP Code:
    #include <windows.h>
    #include "resource.h"
    #define TheClass "myclass"

    HBITMAP hbball;      //<-----HERE
    HINSTANCE hInst;   //<-----HERE

    LRESULT CALLBACK WndProc(HWND hwndUINT msgWPARAM wparamLPARAM lparam)
    {
        switch(
    msg)
        {
        case 
    WM_CREATE:
        
    hbball LoadBitmap(hInst,  MAKEINTRESOURCE(BALLBMP));   //<-----HERE
        
    if(!hbball)
        {
         
    MessageBox(hwnd"Load of resource BALLBMP failed.""Error",
             
    MB_OK MB_ICONEXCLAMATION);
        }
         return 
    0;
        case 
    WM_PAINT:
        if(
    hbball)
            {
            
    HDC winhdcmemhdc;
            
    PAINTSTRUCT ps;
            
    BITMAP bm;
            
    winhdc =  BeginPaint(hwnd, &ps);
            
    memhdc CreateCompatibleDC(winhdc);
            
    SelectObject(memhdchbball);
            
    GetObject(hbballsizeof(bm), &bm);
            
    BitBlt(winhdc,0,0,bm.bmWidthbm.bmHeightmemhdc0,0,SRCCOPY);
            
    DeleteDC(memhdc);
            
    EndPaint(hwnd, &ps);
            }
            return 
    0;
        case 
    WM_CLOSE:
            
    DestroyWindow(hwnd);
            return 
    0;
        case 
    WM_DESTROY:
            
    DeleteObject(hbball);
            
    PostQuitMessage(0);
            return 
    0;
        }

        return 
    DefWindowProc(hwndmsgwparamlparam);
    }

    int WINAPI WinMain(HINSTANCE hInstanceHINSTANCE HPrevInstanceLPSTR lpCmdLineint ShowCmd)
    {
        
    WNDCLASSEX wc;
        
    HWND hwnd;
        
    MSG msg;

        
    //Creates the window class

        
    wc.cbClsExtra 0;
        
    wc.cbSize sizeof(wc);
        
    wc.cbWndExtra 0;
        
    wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
        
    wc.hCursor LoadCursor(NULLIDC_ARROW);
        
    wc.hIcon LoadIcon(NULLIDI_APPLICATION);
        
    wc.hIconSm LoadIcon(NULLIDI_APPLICATION);
        
    wc.hInstance hInstance;
        
    wc.lpfnWndProc WndProc;
        
    wc.lpszClassName TheClass;
        
    wc.lpszMenuName MAKEINTRESOURCE(MYMENU);
        
    wc.style CS_VREDRAW CS_HREDRAW;

        
    hInst hInstance;  //<-----HERE

        //Registers the Window Class -- if it does not succeed then it
        //shows an error message.

        
    if(!RegisterClassEx(&wc))
        {
            
    MessageBox(NULL"Error occurred while \
            Registering the Main Class"
    "Error!"MB_OK);
            return 
    0;
        }

        
    //Creates the main window hwnd

        
    hwnd CreateWindow(TheClass,
                        
    "The Ball Example",
                        
    WS_OVERLAPPEDWINDOW,
                        
    0,
                        
    0,
                        
    500,
                        
    500,
                        
    NULL,
                        
    NULL,
                        
    hInstance,
                        
    NULL);

        
    //Check if there is any error when we make our windows' Hwnd

        
    if(hwnd == NULL)
        {
            
    MessageBox(NULL"Error making the Hwnd for our window","Error",MB_OK);
        return 
    0;
        }

        
    ShowWindow(hwndSW_SHOW);
        
    UpdateWindow(hwnd);

        while(
    GetMessage(&msgNULL0,0))
        {
            
    TranslateMessage(&msg);
            
    DispatchMessage(&msg);
        }

        return 
    msg.wParam;

    Since you had your Bitmap as a local it was getting destroyed everytime it left create
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  9. #9
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Oh and you got 2 memory leaks, you need to add:

    PHP Code:
    DeleteDC(memhdc);

    //And

    WM_CLOSE:
    DeleteObject hbball;
    return 
    DefWindowProc(hwndmsgwparamlparam);
    break; 
    Plus if when you get farther along your graphics seem a bit slow on the refresh, make your HDCs global and use select object only once to get them. That way it will not have to recreate them everytime you BitBlt.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  10. #10

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    Great guys!

    It is working now.
    So the only problem was that I was not declaring my "HBITMAP hbball"as global?
    Baaaaaaaaah

  11. #11
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    If you got it to work using a global then yeah, except for those pesky memory leaks you got
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width