C language > Beginner questions

Updating Picturebox Control

(1/1)

WiiLF23:
Hey everyone.

I can not figure this out. I have a picturebox control (ID 8001) on my main dialog. I want to replace and updating this control's Bitmap with a function, allowing me to select different Bitmap (logos) to effectively replace/update the picturebox control.

But nothing takes effect. And I just cant figure it out.

I'll break down what I did to try to accomplish this.

I added 3 Bitmaps using the resource editor Import > select BMP etc

In resource tree I have

main.rc > Bitmap >
   #8002
   #8003
   #8004

And this reflects in the main.rc file


--- Code: ---CONTROL "", 8001, "Static", SS_ICON|SS_CENTERIMAGE, 4, 192, 32, 24

8002 BITMAP "assets\\vendors\\vendor_logo_amd.bmp"
8003 BITMAP "assets\\vendors\\vendor_logo_intel.bmp"
8004 BITMAP "assets\\vendors\\vendor_logo_nvidia.bmp"

--- End code ---

And in main.h


--- Code: ---#define IDR_ICO_MAIN  8001

#define VENDOR_LOGO_AMD 8002
#define VENDOR_LOGO_NVIDIA 8003
#define VENDOR_LOGO_INTEL 8004

--- End code ---

It is ID 8001 (the picturebox ID) I want to update with this function


--- Code: ---static HWND vendorLogo;

void SetVendorLogo(HWND hDlg, int controlId, int imageId) {
    HBITMAP hBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(imageId));
    if (hBitmap) {
        HBITMAP hOldBitmap = (HBITMAP)SendMessage(vendorLogo, STM_GETIMAGE, (WPARAM)IMAGE_BITMAP, 0);
        SendMessage(vendorLogo, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBitmap);
        InvalidateRect(vendorLogo, NULL, TRUE);
       
        if (hOldBitmap) {
            DeleteObject(hOldBitmap);
        }
    } else {
        MessageBox(hDlg, L"Bitmap loading failed", L"Error", MB_ICONERROR);
        return;
    }
}

--- End code ---

And I call like so


--- Code: ---SetVendorLogo(hWnd, 8001, VENDOR_LOGO_INTEL);

--- End code ---

But the image never updates and remains blank (empty) at all times. Can anyone tell me why this is happening, and how I can resolve this?

The picturebox coordinates are perfect as was used with the main application icon all along, until we wish to update/replace picturebox, in case this raises question.

Thank you

frankie:
Post a minimal example project.

MrBcx:
Here is a BCX runtime function that you should be able to glean some insight from.


--- Code: ---
HBITMAP Set_BCX_Bitmap2 (HWND hWnd, HBITMAP Bmp, int DeleteBM)
{
  HBITMAP bm=(HBITMAP)SendMessage(hWnd,STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)Bmp);
  if (DeleteBM)
  {
    DeleteObject(bm);
    return 0;
  }
  return bm;
}


--- End code ---


ref:  https://bcxbasiccoders.com/webhelp/html/set_bcx_bitmap2statement.htm

John Z:

--- Quote from: WiiLF23 on August 19, 2023, 11:41:27 PM ---I can not figure this out. I have a picturebox control (ID 8001) on my main dialog. I want to replace and updating this control's Bitmap with a function, allowing me to select different Bitmap (logos) to effectively replace/update the picturebox control.

--- End quote ---

Well as frankie suggests it is hard to determine what is what without a full code snippet - but here are some thoughts.

I see InvalidateRect(vendorLogo, NULL, TRUE);
but I don't see UpdateWindow(....), You might add

Here is what I use to essentially do exactly as you are trying to do

--- Code: ---// load the Add Logo bitmap
hBitmap = (HBITMAP) (LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(AddLogo), IMAGE_BITMAP, 0, 0, 0));

SendDlgItemMessage(gHWND, Logo, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap);

--- End code ---

Have you use BeginPaint and EndPaint?
Does your main process WM_PAINT ?
Do you see the outline of the control? if not you might need to use ShowWindow((HWND)GetDlgItem(gHWND, Logo), SW_SHOW);

Is the image the right type?

For your image path is it really right?  Here is how one of mine looks:

--- Code: ---TB_Pict2 BITMAP "..\\Toolbar\\New_ToolBar2.bmp"
--- End code ---

note the leading ..

I'm sure there are more things but all is guessing....
Using Level 2 warnings ?

I also see you use:

--- Code: ---CONTROL "", 8001, "Static", SS_ICON | SS_CENTERIMAGE, 4, 192, 32, 24
--- End code ---
ICON requires very specific sizes and color complications. does the new logo conform to the parameters of the old one, size X and Y, color palette size and colors? 

I used:

--- Code: ---CONTROL "", Logo, "Static", SS_BITMAP|SS_CENTERIMAGE|0x00000100, 476, 104, 56, 56, 0, 235
--- End code ---

John Z

From MS
SS_ICON
The style ignores the CreateWindow parameters nWidth and nHeight; the control automatically sizes itself to accommodate the icon. As it uses the LoadIcon function, the SS_ICON style can load only icons of dimensions SM_CXICON and SM_CYICON. This restriction can be bypassed by using the SS_REALSIZEIMAGE style in addition to SS_ICON.
If an icon cannot be loaded through LoadIcon, an attempt is made to load the specified resource as a cursor using LoadCursor. If that too fails, an attempt is made to load from the device driver using LoadImage.

WiiLF23:
I appreciate the feedback. As it turns out, my image format needs to be set to 24 BPP, RGB-565. The bitmap format I was having trouble with was the problem all along - too many colors as reported by Irfanview. The code was working all along as it turned out.

Images successfully update according to my function.

This is a lesson for me going forward. Thank you everyone.

Navigation

[0] Message Index

Go to full version