Results 1 to 14 of 14

Thread: bitmap display

  1. #1

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330

    bitmap display

    Does anyone have a piece of code that shows how to display a resource bitmap on your window? Thanks
    Matt

  2. #2
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Here is a little code that I wrote some weeks ago:

    -The bitmap in the resource file is named "MYBMP"
    -The resource header file is called "resource.h"

    Code:
    #include <windows.h>
    #include "resource.h"
    static char g_szClassName[] = "MyWindowClass";
    static HINSTANCE hinst = NULL;
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    HWND hwnd;
    PAINTSTRUCT ps;
    HDC hdc, memhdc;
    HBITMAP mybmp;
    BITMAP bm;
    
    //MYBMP is a bitmap in the resource file
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
       LPSTR lpCmdLine, int nCmdShow)
    {
       WNDCLASSEX WndClass;
       MSG Msg;
    
       hinst = hInstance;
    
       mybmp = LoadBitmap(hinst, MAKEINTRESOURCE(MYBMP));
    
       WndClass.cbSize        = sizeof(WNDCLASSEX);
       WndClass.style         = CS_HREDRAW | CS_VREDRAW;
       WndClass.lpfnWndProc   = WndProc;
       WndClass.cbClsExtra    = 0;
       WndClass.cbWndExtra    = 0;
       WndClass.hInstance     = hinst;
       WndClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
       WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
       WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
       WndClass.lpszMenuName  = NULL;
       WndClass.lpszClassName = g_szClassName;
       WndClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
       if(!RegisterClassEx(&WndClass))
       {
          MessageBox(0, "Window Registration Failed! ", "Error!",
             MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
          return 0;
       }
    
       hwnd = CreateWindowEx(
          NULL,
          g_szClassName,
          "Coloured Shapes Generator",
          WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, CW_USEDEFAULT, 320, 240,
          NULL, NULL, hinst, NULL);
    
       if(hwnd == NULL)
       {
          MessageBox(0, "Window Creation Failed! ", "Error!",
             MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
          return 0;
       }
    
    
       ShowWindow(hwnd, nCmdShow);
       UpdateWindow(hwnd);
    
       while(GetMessage(&Msg, NULL, 0, 0))
       {
          TranslateMessage(&Msg);
          DispatchMessage(&Msg);
       }
       return Msg.wParam;
    }
    
    
    
    
    
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
    {
    	
       switch(Message)
       {
       case WM_PAINT:
    	   hdc = BeginPaint(hwnd, &ps);
    	   memhdc = CreateCompatibleDC(hdc);
    	   SelectObject(memhdc, mybmp);
    	   //Draws the bitmap on the memory DC
    
    	   GetObject(mybmp, sizeof(bm), &bm);
    
    	   //Gets the info about the bitmap and stores that in "bm"
    	   BitBlt(hdc, 0,0, bm.bmWidth, bm.bmHeight, memhdc, 0,0, SRCCOPY);
    
    	   //Draws the bitmap on the window from the memory DC
    
    	   DeleteDC(memhdc);
    	   EndPaint(hwnd, &ps);
    
    	   return 0;
       
          case WM_CLOSE:
    		  DeleteObject(mybmp);
             DestroyWindow(hwnd);
          break;
          case WM_DESTROY:
             PostQuitMessage(0);
          break;
          default:
             return DefWindowProc(hwnd, Message, wparam, lparam);
       }
       return 0;
    }
    Baaaaaaaaah

  3. #3

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    if you don't mind could you send me your project? Thanks alot

    my email adress is [email protected]
    Matt

  4. #4
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    i cannot find the project because it is on one of the cds that I created after reinstalling the os.

    YOu can get project from the link below (it is from winprog.org examples):

    http://www.winprog.org/tutorial-old/files/image_one.zip
    Baaaaaaaaah

  5. #5

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330

    Smile

    just as good! Thanks
    Matt

  6. #6

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    one more thing, when I use your example code I have MYBMP.bmp under the resource folder in my project, which contains the drawn picture you can create. When I compile I get
    the error:
    error C2065: 'MYBMP' : undeclared identifier
    what am I doing wrong?
    Matt

  7. #7
    denniswrenn
    Guest
    What is the resource identifier of MYBMP.bmp? Is it IDB_BITMAP1 (or similar) or MYBMP?

  8. #8

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    where would I find that info? I don't have a header file created. Do I need one?
    Matt

  9. #9

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    actually for some reason its not in the project explorer but I found a resource.h file that goes with my project. It is
    #define IDB_BITMAP1
    Matt

  10. #10
    denniswrenn
    Guest
    Well, look in the resource tab thingy in VC++ and look at the bitmap you added.... it should have an identifier(such as IDB_BITMAP1) change that, and change your resource.h (I think VC++ does this automatically though).

  11. #11
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    MYBMP.bmp is the name of the bitmap that you have on your computer (the actuall file) but as dennis said, you need you change the id of the bitmap in the resource file to MYBMP, not the bitmap file name
    Baaaaaaaaah

  12. #12

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    ok, under Resource Files I have the bitmap "MYBMP.bmp". In that I changed the Identifier to IDB_MYBMP, then the actual bitmap is called bitmap1.bmp. I set the file name to that. I still get the error

    'MYBMP' : undeclared identifier

    anyway heres the code I am using, this does not include the callback although I have it.

    Code:
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    HWND hwnd;
    PAINTSTRUCT ps;
    HDC hdc, memhdc;
    HBITMAP mybmp;
    BITMAP bm;
    
    //MYBMP is a bitmap in the resource file
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
       LPSTR lpCmdLine, int nCmdShow)
    {
       WNDCLASSEX WndClass;
       MSG Msg;
    
       hinst = hInstance;
    
       mybmp = LoadBitmap(hinst, MAKEINTRESOURCE(MYBMP)); //error here-
    
       WndClass.cbSize        = sizeof(WNDCLASSEX);
       WndClass.style         = CS_HREDRAW | CS_VREDRAW;
       WndClass.lpfnWndProc   = WndProc;
       WndClass.cbClsExtra    = 0;
       WndClass.cbWndExtra    = 0;
       WndClass.hInstance     = hinst;
       WndClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
       WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
       WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
       WndClass.lpszMenuName  = NULL;
       WndClass.lpszClassName = g_szClassName;
       WndClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
       if(!RegisterClassEx(&WndClass))
       {
          MessageBox(0, "Window Registration Failed! ", "Error!",
             MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
          return 0;
       }
    
       hwnd = CreateWindowEx(
          NULL,
          g_szClassName,
          "Coloured Shapes Generator",
          WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, CW_USEDEFAULT, 320, 240,
          NULL, NULL, hinst, NULL);
    
       if(hwnd == NULL)
       {
          MessageBox(0, "Window Creation Failed! ", "Error!",
             MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
          return 0;
       }
    
    
       ShowWindow(hwnd, nCmdShow);
       UpdateWindow(hwnd);
    
       while(GetMessage(&Msg, NULL, 0, 0))
       {
          TranslateMessage(&Msg);
          DispatchMessage(&Msg);
       }
       return Msg.wParam;
    }
    Matt

  13. #13
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    In that I changed the Identifier to IDB_MYBMP
    Change the indentifier to MYBMP not IDB_MYBMP
    Baaaaaaaaah

  14. #14

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    Well, now Im getting no errors but nothing is being displayed. Same code as above, changed the identifier to MYBMP. Any idea what could be wrong?
    Matt

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