0

I have spent half the day trying to get a bitmap to load. As far as I can tell, I'm doing everything right, but it's not working.

I am trying to build a static MFC library using Visual Studio 2022. Other projects in this solution use bitmaps with no problem. Before I began work, this project had no bitmaps.

I used the resource editor to import a bitmap. The .rc file contains this:

IDB_BITMAP1             BITMAP                  "C:\\ProgrammingProjects\\logix-designer\\RSLogix5000\\GUI\\DSI\\nodeExpanded.bmp"

The .rc file #includes a file named "PVGridResources.h". That file includes this:

#define IDB_BITMAP1                     16000

My class has two private CBitmap members. Attempts to call LoadBitmap() on them from the constructor return 0, which means that the resource failed to load, but there is no indication of a reason. So, I created a method named LoadTheBitmap() and call it after the object is constructed. For now, I'm just trying to load a bitmap into a locally declared CBitmap object, and I'm using the lowest-level bitmap methods there are:

void CPVTreeCtrlButton::LoadTheBitmap()
{
    HINSTANCE hResources = AfxGetResourceHandle();
    HBITMAP bitmapHandle = ::LoadBitmap(hResources,
        MAKEINTRESOURCE(IDB_BITMAP1));

}

hResources is non-null, which means the call to AfxGetResourceHandle() worked. But bitmapHandle comes back as 0, which means that once again the bitmap wasn't loaded.

The path of the bitmap file was automatically inserted into the resource file by Visual Studio, and I did not change it. Therefore, I know that the file exists.

What else can I try? What else would you like me to show you?

ROBERT RICHARDSON
  • 2,077
  • 4
  • 24
  • 57
  • 1
    Does the static library and the compiled resource script get linked into the same module? Is that module a DLL or an EXE? – IInspectable Aug 18 '22 at 20:40
  • 1
    if you open the resulting binary (exe or dll) in VS (using the resource editor), do you see the bitmap listed in the resources? – Ed Dore Aug 19 '22 at 04:25
  • Is the resource script part of the static library project? Linking with the lib won't include it in the executable (.exe or .dll) using it, the .res file has to be included in the executable. Check [this](https://stackoverflow.com/questions/531502/vc-resources-in-a-static-library) and [this](https://stackoverflow.com/questions/58306160/dialog-resources-in-a-static-library) post. Another possible cause, the .bmp file format, can it be used successfully in another project? If not, try a simpler, uncompressed .bmp file. – Constantine Georgiou Aug 19 '22 at 06:42
  • @ConstantineGeorgiou To answer your last question, yes. The bitmap was imported from another project in which it is successfully used. – ROBERT RICHARDSON Aug 19 '22 at 12:01

1 Answers1

0

Static libraries can use the resources of the DLL/EXE that use the library. Thus, all I have to do is pass the IDs of the bitmap resource from the DLL/EXE to the class in my static library through its constructor.

ROBERT RICHARDSON
  • 2,077
  • 4
  • 24
  • 57