Board index » cppbuilder » Loading resource file containing bitmap

Loading resource file containing bitmap

I am creating a package, ThePack.bpl, with several components. One of the
components, Buttons.cpp, has 10 speed buttons. I an trying to load a 160 x
16 bitmap from a resource file.

I created Buttons.rc and Buttons.rb with notepad and compiled them with
brcc.exe. ThePack.bpl install correctly.

When I try to drop the Buttons component on a form I get the mesage:

Access Violation at address 4005C40C in module VCL50.bpl read of adress
0000030

When I block out the code to handle the resource and TimageList I can drop
the Buttons component correctly.

Any help would be appreciated.

Bob Jones

// ----------------- ThePack.cpp

#include <vcl.h>
#pragma hdrstop
USERES("ThePack.res");
USERES("Buttons.res");
USEUNIT("Buttons.cpp");
USERES("TButtons.dcr");

. . .

// ----------------- file buttons.rh

#ifndef BUTTONS_RH
#define BUTTONS_RH

#define ID_BUTTONS 1000

#endif

// ----------------- file buttons.rc

#include "resource.rh"

ID_BUTTONS BITMAP "Buttons.bmp"

// ----------------- file buttons.h

TSpeedButton *Buttons[buttonCount];

// ----------------- file buttons.cpp

#include <vcl.h>
#pragma hdrstop
#include "Buttons.rh"
#include "Buttons.h"

#pragma package(smart_init)
// #pragma resource "Buttons.res"

// ----------------- constructor

__fastcall TButtons::TButtons(TComponent* Owner)

. . .

TImageList *pImageList = new TImageList(this);

TLoadResources lr;
bool rc = pImageList->GetInstRes(ID_Nav, rtBitmap, "Navigator", 160, lr,
clBtnFace);

for (int i = 0; i < buttonCount; i++){
  imageList->GetBitmap(i, Buttons[i]->Glyph);

Quote
}

. . .

// -----------------

void __fastcall TButtons::createButtons(){
  for (int i = 0; i < buttonCount; i++){
    TSpeedButton *p = new TSpeedButton(this);
    p->Parent = this;
    p->Name = "B_" + String(i);
    p->ShowHint = true;
    p->Tag = i;
    p->OnClick = buttonClick;
    p->Height = buttonHeight;
    p->Width = buttonWidth;
    p->Enabled = true;
    Buttons[i] = p;
  }

Quote
}

// -------------------------------------------------------------------------
---

--
Bob Jones

 

Re:Loading resource file containing bitmap


Correction

I am creating a package, ThePack.bpl,  with several components.  One of the
components, Buttons.cpp,  has 10 speed buttons.  I an trying to load a 160 x
16 bitmap from a resource file.

I created Buttons.rc and Buttons.rb with notepad and compiled them with
brcc.exe.  ThePack.bpl install correctly.

When I try to drop the Buttons component on a form I get the mesage:
Access Violation at address 4005C40C in module VCL50.bpl read of adress
0000030

When I block out the code to handle the resource and TimageList I can drop
the Buttons component correctly.

Any help would be appreciated.

Bob Jones

// ----------------- ThePack.cpp
#include <vcl.h>
#pragma hdrstop
USERES("ThePack.res");
USERES("Buttons.res");

USEUNIT("Buttons.cpp");
USERES("TButtons.dcr");
  . . .

// -----------------  file buttons.rh

#ifndef BUTTONS_RH
#define BUTTONS_RH

#define ID_BUTTONS 1000

#endif

// -----------------  file  buttons.rc

#include "resource.rh"

ID_BUTTONS     BITMAP   "Buttons.bmp"

// -----------------  file buttons.h

  TSpeedButton *Buttons[buttonCount];

// -----------------  file buttons.cpp
#include <vcl.h>
#pragma hdrstop

#include "Buttons.rh"

#include "Buttons.h"
#pragma package(smart_init)
// #pragma resource "Buttons.res"

// ----------------- constructor
__fastcall TButtons::TButtons(TComponent* Owner)
  . . .

  TImageList *pImageList = new TImageList(this);

  TLoadResources lr;
  bool rc = pImageList->GetInstRes(ID_BUTTONS, rtBitmap, "Buttons", 160, lr,
clBtnFace);

  for (int i = 0; i < buttonCount; i++){
    imageList->GetBitmap(i, Buttons[i]->Glyph);
  }
  . . .

// -----------------
void __fastcall TButtons::createButtons(){
  for (int i = 0; i < buttonCount; i++){
    TSpeedButton *p = new TSpeedButton(this);
    p->Parent   = this;
    p->Name     = "B_" + String(i);
    p->ShowHint = true;
    p->Tag      = i;
    p->OnClick  = buttonClick;
    p->Height   = buttonHeight;
    p->Width    = buttonWidth;
    p->Enabled  = true;
    Buttons[i]  = p;
  }

Quote
}

// -------------------------------------------------------------------------
---

Re:Loading resource file containing bitmap


Where exactly are you calling your createButtons() function from?  You're
not showing that.  If you're not calling it before calling GetBitmap(), then
you're going to get an error due to trying to access invalid memory, since
you didn't allocate the actual button objects before trying to then assign
the images from the TImageList:

    __fastcall TButtons::TButtons(TComponent* Owner)
    {
        TImageList *pImageList = new TImageList(this);

        TLoadResources lr;
        bool rc = pImageList->GetInstRes(ID_Nav, rtBitmap, "Navigator", 160,
lr, clBtnFace);

        if(rc)
        {
            buttonCount = pImageList->Count;
            createButtons();

            for (int i = 0; i < buttonCount; i++)
                imageList->GetBitmap(i, Buttons[i]->Glyph);
        }
    }

Gambit

Quote
"Bob Jones" <bobjo...@AbcTools.com> wrote in message

news:3d2e3537$1_1@dnews...
Quote
> When I try to drop the Buttons component on a form I get the mesage:

> Access Violation at address 4005C40C in module VCL50.bpl read of adress
> 0000030

Re:Loading resource file containing bitmap


"Remy Lebeau [TeamB]" <gambi...@yahoo.com> wrote in message
news:3d2e9214$1_2@dnews...
: Where exactly are you calling your createButtons() function from?
. . .
const int buttonCount = 10;

 __fastcall TButtons::TButtons(TComponent* Owner)
 {
     TImageList *pImageList = new TImageList(this);

     TLoadResources lr;
     bool rc = pImageList->GetInstRes(ID_BUTTONS, rtBitmap, "Buttons", 160,
lr, clBtnFace);

     createButtons();

     if(rc)
       for (int i = 0; i < buttonCount; i++)
           imageList->GetBitmap(i, Buttons[i]->Glyph);
 }

Hi Gambit,

You had my code almost exactly. It was my very poor pasting to the message
form that caused the omission. The constructor isn't called when I drop the
component on a form. This makes me suspect the resource file or my method of
declaring the resource file is the problem area.
Bob Jones

: "Bob Jones" <bobjo...@AbcTools.com> wrote in message
: news:3d2e3537$1_1@dnews...
:
: > When I try to drop the Buttons component on a form I get the mesage:
: >
: > Access Violation at address 4005C40C in module VCL50.bpl read of adress
: > 0000030

Re:Loading resource file containing bitmap


Eventually I gave up on  GetInstRes and used the API.  My solution was:

createButtons();
HIMAGELIST himl = ImageList_LoadBitmap(GetModuleHandle(0),
MAKEINTRESOURCE(ID_BUTTONS_Nav), 16, 1, CLR_NONE);
for (int i = 0; i < buttonCount; i++){
  bool rc = ImageList_Draw(himl, i, Buttons[i]->Glyph->Canvas->Handle, 0, 0,
0);
  Buttons[i]->Glyph->Height = 16;
  Buttons[i]->Glyph->Width = 16;

Quote
}

ImageList_Destroy(himl);

Len Jones

Re:Loading resource file containing bitmap


hi Bob

Not knowing and never tryed it but looking at the code
You might try changing the width value to 16 insted
of 160 otherwise there wouldn't be more then one
imge in the list with a width of 160

bool rc = pImageList->GetInstRes(ID_BUTTONS, rtBitmap, "Buttons", 160,
 lr, clBtnFace);

Best regards
Asger

Other Threads