View previous topic :: View next topic |
Author |
Message |
TraxMate Master Cheater Reputation: 0
Joined: 01 Mar 2008 Posts: 363
|
Posted: Sat Apr 11, 2009 5:11 pm Post subject: [C++] Loading image |
|
|
I want to load an image from a resource so I did this:
Resource.rc:
Code: | #include "Resource.h"
IDB_HP_BAR BITMAP "HP_Bar.bmp" |
And Resource.h: Code: | #define IDB_HP_BAR 201 |
and to load it I did this:
Code: | CreateWindowEx(0, WC_STATIC, 0, WS_VISIBLE | WS_CHILD | SS_BITMAP, 0, 0, 0, 0, hWnd, (HMENU)ID_HP, hInstance, NULL);
SendDlgItemMessage(hWnd, ID_HP, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)LoadImage(hInstance, MAKEINTRESOURCE(IDB_HP_BAR), IMAGE_BITMAP, 0, 0, 0)); |
But the image won't show. It works if I use LR_LOADFROMFILE and put it in C:\ drive, but I want to load it from a resource so is there anything I've done wrong?
|
|
Back to top |
|
|
lurc Grandmaster Cheater Supreme Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Sat Apr 11, 2009 5:24 pm Post subject: |
|
|
Don't you have to give the width/height of the static control a value?
_________________
|
|
Back to top |
|
|
TraxMate Master Cheater Reputation: 0
Joined: 01 Mar 2008 Posts: 363
|
Posted: Sat Apr 11, 2009 5:49 pm Post subject: |
|
|
Nope. MSDN wrote: | The style ignores the nWidth and nHeight parameters; the control automatically sizes itself to accommodate the bitmap. | But I tried just in case but the results was the same.
|
|
Back to top |
|
|
lurc Grandmaster Cheater Supreme Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Sat Apr 11, 2009 6:07 pm Post subject: |
|
|
Try this:
Code: | BOOL CreateStaticImage(__in HINSTANCE hInstance, __in HWND hWnd)
{
BOOL bRET = FALSE;
HWND hStatic;
HANDLE hBitmap;
hStatic = CreateWindowEx(0, WC_STATIC, 0, WS_VISIBLE | WS_CHILD | SS_BITMAP, 0, 0, 0, 0, hWnd, (HMENU)ID_HP, hInstance, 0);
if (hStatic != NULL)
{
MessageBox(hWnd, _T("CreateWindowEx success."), _T("Step 1"), MB_OK);
hBitmap = LoadImage(hInstance, MAKEINTRESOURCE(IDB_HP_BAR), IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE | LR_LOADTRANSPARENT);
if (hBitmap != NULL)
{
MessageBox(hWnd, _T("LoadImage success."), _T("Step 2"), MB_OK);
if (SendMessage(hStatic, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap) != NULL)
{
MessageBox(hWnd, _T("SendMessage success."), _T("Step 3"), MB_OK);
bRET = TRUE;
}
}
}
return bRET;
} |
See where the Message Box's stop.
If you do not get all 3 then use GetLastError to find out what's going on.
_________________
|
|
Back to top |
|
|
TraxMate Master Cheater Reputation: 0
Joined: 01 Mar 2008 Posts: 363
|
Posted: Sat Apr 11, 2009 6:46 pm Post subject: |
|
|
I get an error at LoadImage() Error wrote: | Can not find the specified resource type in the memory image file. | What does that mean? I have the bitmap in the same folder as the source :O.
|
|
Back to top |
|
|
lurc Grandmaster Cheater Supreme Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Sat Apr 11, 2009 6:50 pm Post subject: |
|
|
What does GetLastError return?
_________________
|
|
Back to top |
|
|
TraxMate Master Cheater Reputation: 0
Joined: 01 Mar 2008 Posts: 363
|
Posted: Sat Apr 11, 2009 6:57 pm Post subject: |
|
|
the eror was that in the quote. MSDN doesn't show what errors that might be returned so I had to use an alternative function I found on the internet to show the last error.
|
|
Back to top |
|
|
hcavolsdsadgadsg I'm a spammer Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sat Apr 11, 2009 7:49 pm Post subject: |
|
|
TraxMate wrote: | the eror was that in the quote. MSDN doesn't show what errors that might be returned so I had to use an alternative function I found on the internet to show the last error. |
it does...
|
|
Back to top |
|
|
lurc Grandmaster Cheater Supreme Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Sat Apr 11, 2009 8:00 pm Post subject: |
|
|
Are you sure you added the bitmap correctly...?
_________________
|
|
Back to top |
|
|
TraxMate Master Cheater Reputation: 0
Joined: 01 Mar 2008 Posts: 363
|
Posted: Sat Apr 11, 2009 8:20 pm Post subject: |
|
|
@slovach: Didn't find it when I looked. Will look again then..
@lurc: I think so cuz I've done it before. But this time I'm doing it in a dll, does it matter if it's in a dll or not?
|
|
Back to top |
|
|
lurc Grandmaster Cheater Supreme Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Sat Apr 11, 2009 9:19 pm Post subject: |
|
|
If it's in a different dll other then the module itself then you must LoadLibrary that dll before you are able to use its resources.
_________________
|
|
Back to top |
|
|
TraxMate Master Cheater Reputation: 0
Joined: 01 Mar 2008 Posts: 363
|
Posted: Sat Apr 11, 2009 9:30 pm Post subject: |
|
|
It is in the same module. I have embedded a GUI into a dll file.
|
|
Back to top |
|
|
Bizarro I post too much Reputation: 0
Joined: 01 May 2007 Posts: 2648
|
Posted: Sat Apr 11, 2009 10:59 pm Post subject: |
|
|
use GetModuleHandle(NULL) instead of hInstance when u loadimage
_________________
w8 baby.com Banner contest, Come join NOW!!
Check us out for Prize deatils
Last edited by Bizarro on Sat Apr 11, 2009 11:00 pm; edited 1 time in total |
|
Back to top |
|
|
TraxMate Master Cheater Reputation: 0
Joined: 01 Mar 2008 Posts: 363
|
Posted: Sat Apr 11, 2009 11:06 pm Post subject: |
|
|
@Bizarro: Tried that same thing, no image.
@Moose: w00t? o.O Edit: Searched on that and found a link to msdn. I'm going to look at that.
This works perfectly when it's a normal win32 application...
Last edited by TraxMate on Sat Apr 11, 2009 11:20 pm; edited 1 time in total |
|
Back to top |
|
|
Bizarro I post too much Reputation: 0
Joined: 01 May 2007 Posts: 2648
|
Posted: Sat Apr 11, 2009 11:18 pm Post subject: |
|
|
o just read u are doing it in a dll.
in the case of injecting dll, ur hInstance is messed up and returns the main app not the dll
so instead of GetModuleHandle(NULL)/hInstance , use GetModuleHandle("project name"). replace it with your project/dll name
_________________
w8 baby.com Banner contest, Come join NOW!!
Check us out for Prize deatils |
|
Back to top |
|
|
|