Thread: Drawing a bitmap when a button is pressed

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Hallo,

    I am trying to display a .bmp image on the screen when I press a button. This is probably simple, but I cant get it work and I must admit that I find the msdn very confusing (although I am starting to learn my way around).

    I was able to load and display the image on the screen using WM_PAINT, but then it was under my other media object (I am streaming from a web camera) and it was visible all the time.

    So, is there a way to display a bmp image over all the other elements when a button is pressed?

    Here is the code I am using(which do not work)
    Code:
    case WM_COMMAND:
    {
    // Check if the mouse button has been clicked
    if(HIWORD(wParam) == BN_CLICKED)
    {
    	// See if it clicked any of the buttons
    	if((wParam) == BUTTON_PLAY)
    	{
    		PAINTSTRUCT ps;
    
    		HBITMAP img = (HBITMAP) LoadImage(NULL, TEXT("C:\\Users\\Ole\\Documents\\frames\\animasjonsFrame_0.bmp"), 
    						          IMAGE_BITMAP,640,480,LR_LOADFROMFILE );
    
    		if(img == NULL)
    			MessageBox(NULL, TEXT("ERROR loading"), TEXT("Error"), MB_ICONERROR|MB_OK);
    
    		BITMAP bm;
    		HDC hdc = BeginPaint(hWnd, &ps);;
    
    		HDC hdcMem = CreateCompatibleDC(hdc);
    		HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, img);
    		GetObject(img, sizeof(bm), &bm);
    
    		BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
    
    		SelectObject(hdcMem, hbmOld);
    		DeleteDC(hdcMem);
    
    		EndPaint(hWnd, &ps);
    
    		int currentTime = GetTickCount();
    		while(currentTime + 500 > GetTickCount());
    
    		ShowWindow(hWnd, SW_SHOW); 
    	} 
    	//.....
    }
    Last edited by h3ro; 08-05-2008 at 09:22 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    		int currentTime = GetTickCount();
    		while(currentTime + 500 > GetTickCount());
    What is wrong with "Sleep(500)"?

    --
    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.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Try loading the image to a static window, and hiding the static window until the button is pressed. That's assuming you have the image loaded at compile time, and doesn't come from the clipboard or something.

    Code:
    hwndCalc = CreateWindow(WC_STATIC,"",
                    WS_CHILD|SS_BITMAP|WS_VISIBLE,
                    rect.left+3,rect.top+23,rect.right-7,rect.bottom-27,hwndTab,NULL,
                    (HINSTANCE)GetWindowLong(hwndTab,GWL_HINSTANCE),NULL);
    
    SendMessage(hwndCalc,STM_SETIMAGE,IMAGE_BITMAP,
        (LPARAM)LoadImage(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_CALC),
        IMAGE_BITMAP,rect.right-rect.left,rect.bottom-rect.top,LR_SHARED));

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    I am going to put something in the while loop later on that I only want to happen for a certain time. I added that code there just to see if the problem was that the window got updated to fast and removed my -bmp image

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Try loading the image to a static window, and hiding the static window until the button is pressed. That's assuming you have the image loaded at compile time, and doesn't come from the clipboard or something.
    There is no way of knowing how many pictures I need to load when the program is compiled. It could be from 1 to 'a lot'

    It would also be great if there was a fast way to do this as I need to load them fairly fast. (about 12 per second)

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    67
    BeginPaint() and EndPaint() is only used in WM_PAINT !!!

    Use instead GetDC().


    Greetz

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. Replace button with a bitmap
    By Swarvy in forum Windows Programming
    Replies: 10
    Last Post: 09-12-2008, 11:43 AM
  3. Replies: 2
    Last Post: 06-28-2008, 08:30 PM
  4. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM
  5. Displaying a seperate bitmap for each menu button
    By Magriep in forum Windows Programming
    Replies: 1
    Last Post: 03-26-2002, 11:40 PM