Thread: Passing HBITMAP to a function

  1. #1
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483

    Passing HBITMAP to a function

    I'm trying to pass an HBITMAP to a function which is suppose to put the HBITMAP on a static control. But it doesnt work :-\


    Code:
    void SetImage(HBITMAP bitmap)
    {
        //hwnd is the handle to the static control
        SendMessage(hwnd, STM_SETIMAGE, IMAGE_BITMAP, bitmap);
    }
    this doesnt work, but if i send the message from the Windows Procedure (which is where i load the bitmap, in the WM_CREATE message) it works just fine.

    What am i doing wrong?
    My Website
    010000110010101100101011
    Add Color To Your Code!

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Are you sure that hwnd is correct? It could be that the hwnd variable is different in your window procedure than it is in this function.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    Also make sure that your HBITMAP declaration is global or static. If it's local to your windows proceedure perhaps it's going out of scope on you.

    Regards,
    Brian

  4. #4
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    well the function is inside a class and the class is really responsible for handling the entire static control, so yeah, the hwnd is correct.

    But I'm passing the HBITMAP by value... so it doesn't need to be global variable.
    My Website
    010000110010101100101011
    Add Color To Your Code!

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Well there is not explanation for your problem with the information you have given. I suggest you post your window procedure. Maybe there is something wrong there we can spot.

  6. #6
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Windows Procedrue
    Code:
    LRESULT CALLBACK MyApp::WndProc(HWND hwnd, UINT msg,
    								WPARAM wParam, LPARAM lParam)
    {
    	
    	switch(msg)
    	{
    	case WM_CREATE:
    		{
    			BitMap = LoadBitmap(HInstance, MAKEINTRESOURCE(IDB_BITMAP1));
    			if(BitMap == NULL)
    			{
    				MessageBox(hwnd, "Loading Failure", "Failure", MB_OK);
    			}
                            //stc is of type StaticControl.  Which is suppose to handle the static control.
    			if(!stc.Create(WS_EX_CLIENTEDGE, "", WS_CHILD | WS_VISIBLE | SS_BITMAP, 10, 10, 10, 10, hwnd, HInstance))
    			{
    				MessageBox(NULL, "Creating Static Box failed", "Failure", MB_OK);
    			}
    			stc.SetImage(IMAGE_BITMAP, BitMap); // Problem
    		}
    		break;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    
    	case WM_QUIT:
    		DestroyWindow(hwnd);
    		break;
    	}
    	return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    SetImage function in class StaticControl:

    Code:
    void SetImage(int Style, HBITMAP Bitmap)
    	{
    		if(SendMessage(hwnd, STM_SETIMAGE, Style, (LPARAM)Bitmap) == NULL)
    		{
    			MessageBox(NULL, "Massive Failure", "Failure", MB_OK);
    		}
    	}

    EDIT:

    Nvm it works just fine now, idk why it didn't before

    Thanks guys.
    Last edited by mrafcho001; 03-04-2006 at 02:54 PM.
    My Website
    010000110010101100101011
    Add Color To Your Code!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Passing a byte and its bits to a function
    By rtarbell in forum C Programming
    Replies: 9
    Last Post: 12-04-2008, 09:24 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Passing a function to a function
    By lend0g in forum C++ Programming
    Replies: 1
    Last Post: 03-18-2003, 10:16 PM