NO

Author Topic: Updating Picturebox Control  (Read 4042 times)

Offline WiiLF23

  • Member
  • *
  • Posts: 89
Updating Picturebox Control
« on: August 19, 2023, 11:41:27 PM »
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: [Select]
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"

And in main.h

Code: [Select]
#define IDR_ICO_MAIN  8001

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

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

Code: [Select]
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;
    }
}

And I call like so

Code: [Select]
SetVendorLogo(hWnd, 8001, VENDOR_LOGO_INTEL);

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

« Last Edit: August 20, 2023, 01:13:09 AM by WiiLF23 »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Updating Picturebox Control
« Reply #1 on: August 20, 2023, 02:04:57 AM »
Post a minimal example project.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline MrBcx

  • Global Moderator
  • Member
  • *****
  • Posts: 189
    • Bcx Basic to C/C++ Translator
Re: Updating Picturebox Control
« Reply #2 on: August 20, 2023, 03:49:00 AM »
Here is a BCX runtime function that you should be able to glean some insight from.

Code: [Select]

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;
}



ref:  https://bcxbasiccoders.com/webhelp/html/set_bcx_bitmap2statement.htm
« Last Edit: August 20, 2023, 04:00:24 AM by MrBcx »
Bcx Basic to C/C++ Translator
https://www.BcxBasicCoders.com

Offline John Z

  • Member
  • *
  • Posts: 860
Re: Updating Picturebox Control
« Reply #3 on: August 20, 2023, 12:10:13 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.

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: [Select]
// 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);

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: [Select]
TB_Pict2 BITMAP "..\\Toolbar\\New_ToolBar2.bmp"
note the leading ..

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

I also see you use:
Code: [Select]
CONTROL "", 8001, "Static", SS_ICON | SS_CENTERIMAGE, 4, 192, 32, 24ICON 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: [Select]
CONTROL "", Logo, "Static", SS_BITMAP|SS_CENTERIMAGE|0x00000100, 476, 104, 56, 56, 0, 235
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.
« Last Edit: August 20, 2023, 12:58:25 PM by John Z »

Offline WiiLF23

  • Member
  • *
  • Posts: 89
Re: Updating Picturebox Control
« Reply #4 on: August 21, 2023, 06:50:36 PM »
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.
« Last Edit: August 21, 2023, 09:28:39 PM by WiiLF23 »