Thread: Bitmaps

  1. #1
    cout << "Bye World!";
    Join Date
    Jun 2006
    Posts
    40

    Bitmaps

    I want to display a bitmap with a static control. MSDN says that I should use the SS_BITMAP style and for the text (which I assume is the lpWindowName parameter of CreateWindowEx, please correct me if I'm wrong), I should put the name of the bitmap defined in the resource file, not the filename. How do I figure out the name of the bitmap? I've tried using the following for that parameter:
    Code:
    TEXT("IDB_BITMAP1");
    TEXT("celebrate_bitmap");
    TEXT("bitmap1");
    MAKEINTRESOURCE(IDB_BITMAP1);  //fails even after loading the image, if that's even necessary
    (IDB_BITMAP1 is the ID defined in resource.h, my resource file is celebrate_bitmap.rc, and the bitmap is saved as bitmap1.bmp)
    The function always returns 0 and the bitmap is not displayed. Obviously these do not work (they were a last resort guess), but I can't think of anything else. I am using Visual C++ 2005.

  2. #2
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    1) Load your bitmap either using LoadBitmap or LoadImage.
    2) Use the above handle in SendMessage & STM_SETIMAGE
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  3. #3
    cout << "Bye World!";
    Join Date
    Jun 2006
    Posts
    40
    Quote Originally Posted by Joelito View Post
    1) Load your bitmap either using LoadBitmap or LoadImage.
    2) Use the above handle in SendMessage & STM_SETIMAGE
    I've tried that. The way I understand it, when CreateWindowEx fails and returns 0, the HWND stored for the static control is also 0, and when you send the message you are really sending it to an HWND of 0, which means that the message never gets to the nonexistant window anyway.

    Is there a way I can tell it not to load a bitmap when the control is created, but still create the control? This would allow me to use SendMessage to set the bitmap I believe.
    Last edited by ldb88; 05-28-2007 at 07:35 PM.

  4. #4
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    Can I see your code creation for the static control?
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  5. #5
    cout << "Bye World!";
    Join Date
    Jun 2006
    Posts
    40
    The commented out parts are things that I have tried, but it made no difference.
    Code:
    		//LoadImage(hInstance, MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP, 35, 37, LR_DEFAULTCOLOR);
    		static_celebrate.set_chCaption(MAKEINTRESOURCE(IDB_BITMAP1));  //this is the line that is messing me up I think
    		static_celebrate.set_chClassName(TEXT("STATIC"));		 
                    static_celebrate.nStyle|=SS_BITMAP;
    		static_celebrate.top=90;
    		static_celebrate.left=19;
    		static_celebrate.height=100;
    		static_celebrate.width=100;
    		static_celebrate.nID=0;
                    static_celebrate.Create(hAppWnd);
    		//HBITMAP hBmp=(HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
    		//SendMessage(static_celebrate.hCntrl, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBmp);
    This is the class that static_celebrate is an object of:
    Code:
    class CppCntrl:public CppProtoWnd
    {
    private:
    protected:
    public:
    	CppCntrl();
    	virtual ~CppCntrl();
    	virtual HWND Create(HWND hParent=NULL);
    	HWND	hCntrl;
    };
    And the implementation:
    Code:
    CppCntrl::CppCntrl(){
    	nStyle=WS_CHILD|WS_VISIBLE;
    	hCntrl=NULL;
    	width=100;
    	height=30;
    }
    
    CppCntrl::~CppCntrl(){
    	if (hCntrl)
    		DestroyWindow(hCntrl);
    }
    
    HWND CppCntrl::Create(HWND hParent){
    	if (!hInstance)
    		hInstance=(HINSTANCE)GetWindowLong(hParent, GWL_HINSTANCE);
    	hCntrl=CppProtoWnd::Create(hParent);
    	return hCntrl;
    }
    This is the class it is derived from:
    Code:
    class CppProtoWnd abstract{
    private:
    	TCHAR chClassName[100];
    	TCHAR chCaption[256];
    protected:
    	HINSTANCE hInstance;
    public:
    	CppProtoWnd();
    	~CppProtoWnd();
    	virtual HWND Create(HWND hParent = NULL);
    	void set_hInstance(HINSTANCE hInstance);
    	TCHAR* get_chClassName();
    	TCHAR* get_chCaption();
    	void set_chClassName(TCHAR* chClassName);
    	void set_chCaption(TCHAR* chCaption);
    	int left, top, height, width;
    	int nExStyle, nID, nStyle;
    };
    And the implementation:
    Code:
    CppProtoWnd::CppProtoWnd(){
    	hInstance=NULL;
    	nExStyle=0;
    	nID=0;
    }
    
    CppProtoWnd::~CppProtoWnd(){
    }
    
    HWND CppProtoWnd::Create(HWND hParent){
    	HWND hwnd;
    	hwnd=CreateWindowEx(nExStyle, chClassName, chCaption, nStyle, left, 
    		top, width, height, hParent, (HMENU)nID, hInstance, (VOID*)this);
    	return hwnd;
    }
    
    TCHAR* CppProtoWnd::get_chCaption(){
    	return chCaption;
    }
    
    TCHAR* CppProtoWnd::get_chClassName(){
    	return chClassName;
    }
    
    void CppProtoWnd::set_hInstance(HINSTANCE hInst){
    	hInstance=hInst;
    }
    
    void CppProtoWnd::set_chCaption(TCHAR* chCaption){
    	if((chCaption)&&(lstrlen(chCaption)<256))
    		lstrcpy(this->chCaption,chCaption);
    }
    
    void CppProtoWnd::set_chClassName(TCHAR* chClassName){
    	if ((chClassName)&&(lstrlen(chClassName)<100))
    		lstrcpy(this->chClassName,chClassName);
    }
    Last edited by ldb88; 05-29-2007 at 01:46 PM.

  6. #6
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    Oh god...MFC style :\

    Use GetLastError to see what went wrong... I have this done tons of time without problems, of course in plain old C API
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  7. #7
    cout << "Bye World!";
    Join Date
    Jun 2006
    Posts
    40
    I get "The handle is invalid" for the first one, and "The specified resource type cannot be found in the image file" for the second one.
    Code:
    		SetLastError(0);
    		TCHAR text[256];
    		HBITMAP hBmp = (HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP, 35, 37, LR_DEFAULTCOLOR);
    		DWORD dw=GetLastError();
    		FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, dw, 0, text, 256, 0);
    		MessageBox(hAppWnd, (LPCWSTR)text, TEXT("Error"), MB_OK|MB_ICONINFORMATION);
    		static_celebrate.set_chCaption(MAKEINTRESOURCE(IDB_BITMAP1));
    		static_celebrate.set_chClassName(TEXT("STATIC"));
    		static_celebrate.nStyle|=SS_BITMAP;
    		static_celebrate.top=90;
    		static_celebrate.left=19;
    		static_celebrate.height=100;
    		static_celebrate.width=100;
    		static_celebrate.nID=0;
    		static_celebrate.Create(hAppWnd);
    		dw=GetLastError();
    		FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, dw, 0, text, 256, 0);
    		MessageBox(hAppWnd, (LPCWSTR)text, TEXT("Error"), MB_OK|MB_ICONINFORMATION);
    Last edited by ldb88; 05-29-2007 at 01:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing bitmaps efficiently
    By scwizzo in forum Windows Programming
    Replies: 28
    Last Post: 06-30-2009, 08:25 PM
  2. DX9 Not Displaying Bitmaps
    By Sentral in forum Game Programming
    Replies: 9
    Last Post: 01-31-2006, 05:35 AM
  3. MFC: Multiple clicks shows multiple bitmaps - how?
    By BrianK in forum Windows Programming
    Replies: 0
    Last Post: 06-20-2004, 07:25 PM
  4. Loading bitmaps with createfile() +CreateDIBSection
    By Stevo in forum Windows Programming
    Replies: 7
    Last Post: 05-13-2004, 09:22 PM
  5. using standard bitmaps in the toolbar editor
    By Kibble in forum Windows Programming
    Replies: 0
    Last Post: 12-23-2002, 08:43 PM