Results 1 to 7 of 7

Thread: Displaying bitmaps with button click

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    5

    Displaying bitmaps with button click

    My first request for help here. I'm fairly new to c++.
    I'm trying to display a bitmap with a button click. I can get the bitmap to
    display when the code is put under the WM_PAINT section of WndProc.
    (you'll see where I have that code commented out)

    Everything compiles that I've included below. I use Code::Blocks. (Window XP Pro SP3) Some of those includes aren't needed here but it compiles with it anyway. This is just part of a larger program I'm working on with PRNG's. Those code sections work great. This part is giving me fits.

    Code:
    #include "resource.h"
    #include <windows.h>
    #include <windef.h>
    #include <stdafx.h>
    #include <stdio.h>
    #include <string.h>
    
    HBITMAP g_hbmDieThree = NULL;
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, 
    WPARAM wParam, LPARAM lParam);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow)
    {
    char szClassName[] = "myWindowClass";
    HWND hwnd;
    MSG msg;
    WNDCLASSEX wc;
            wc.cbSize  = sizeof(WNDCLASSEX);
            wc.style  = 0;
            wc.lpfnWndProc = WndProc;  
            wc.cbClsExtra  = 0;
            wc.cbWndExtra  = 0;
            wc.hInstance  = hInstance;
            wc.hIcon  = LoadIcon(NULL, IDI_APPLICATION);
            wc.hCursor  = LoadCursor(NULL, IDC_ARROW);
            wc.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
            wc.lpszMenuName  = NULL;
            wc.lpszClassName  = szClassName;
            wc.hIconSm  = LoadIcon(NULL, IDI_APPLICATION);
    
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        hwnd = CreateWindowEx(
    		WS_EX_CLIENTEDGE,
    		szClassName,
    		"My Window", 
    		WS_OVERLAPPEDWINDOW,
    		100, 100, 400, 400,
    		NULL, NULL, hInstance, NULL);
    
            if(hwnd == NULL)
            {
            MessageBox(NULL, "Window Creation Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
            }
    
    	ShowWindow(hwnd, nCmdShow);
    	UpdateWindow(hwnd);
    
    	while(GetMessage(&msg, NULL, 0, 0) > 0)
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	return msg.wParam;
    
    }  // end of WinMain
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, 
    WPARAM wParam, LPARAM lParam)
    {
    
        // HDC hdc;
        HWND hCtl;
        PAINTSTRUCT ps;
        HBITMAP  g_hbmDieThree = NULL;
    
        switch(msg)
        {
         case WM_CREATE: {
             hCtl = CreateWindowEx (
             0, "BUTTON", "ROLL",
             WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
             70, 300, 100, 35, hwnd,
             (HMENU) IDC_BTN_ROLL,
             GetModuleHandle(0),
             NULL);
    
             hCtl = CreateWindowEx (
             0, "BUTTON", "Exit",
             WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
             220, 300, 100, 35, hwnd,
             (HMENU) IDC_BTN_QUIT,
             GetModuleHandle(0),
             NULL);
    
    // LOAD THE BITMAP IN MEMORY
             g_hbmDieThree = LoadBitmap(GetModuleHandle(NULL), 
                         MAKEINTRESOURCE(IDB_DieThree));
              if
              (g_hbmDieThree == NULL)
              MessageBox(hwnd, "Could not find IDB_DieThree.", "Error", 
                   MB_OK | MB_ICONEXCLAMATION);
              }
              break;
              
              case WM_CLOSE:
              DestroyWindow(hwnd);
              break;
    
              case WM_DESTROY:
                PostQuitMessage(0);
              break;
    
    
        case WM_PAINT:
            {
             
             return TRUE;
             }
             break;
    
        case WM_COMMAND:
    
        switch(LOWORD(wParam)) {
    
        case IDC_BTN_QUIT:
        {
        DestroyWindow(hwnd);
        return TRUE;
        }
        break;
    
        case IDC_BTN_ROLL:
        {
    
            BITMAP bm;
            HBITMAP g_hbmDieThree = (HBITMAP) LoadImage(
                         NULL, "C:\\temp\\DieThree.bmp", IMAGE_BITMAP, 0, 0,
                         LR_CREATEDIBSECTION | LR_DEFAULTSIZE | 
                         LR_LOADFROMFILE);
            HDC hdc = BeginPaint(hwnd, &ps);
            HDC hdcMem = CreateCompatibleDC(hdc);
            HBITMAP hbmOld = (HBITMAP)SelectObject (hdcMem, g_hbmDieThree);
    
            GetObject(g_hbmDieThree, sizeof(bm), &bm);
            BitBlt(hdc, 50, 20, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
    
            SelectObject(hdcMem, hbmOld);
            // DeleteDC(hdcMem);
            DeleteObject(g_hbmDieThree);
            EndPaint(hwnd, &ps);
    
            return TRUE;
        }
        break;
        }
        break;  // END OF SWITCH(LOWORD)wParam))
        default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;  // END OF SWITCH(msg)
    
    }  // end of WndProc
    
    // I also have a RC and H file that goes with this.  If needed I'll post it too.
    I hope I didn't remove any important code when I removed all my
    multitudes of comments I use to keep track of my thought processes.

    I've searched MSDN and many other sites and haven't found any answers that work yet.

    Any help would be appreciated.
    (I tried following the post rules but if I need to change how I did this let me know and I'll do better next time)
    Last edited by codie; Jul 13th, 2010 at 02:02 PM.

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