• Welcome to PlanetSquires Forums.
 

Connect an Image to the ImageButton

Started by Klaas Holland, August 22, 2018, 06:40:26 PM

Previous topic - Next topic

Klaas Holland

I tried to connect an Image to the ImageButton in the Program but it doesn't work.

1. In AppStart :      Common Shared gLogo        as String
2. In WinMain :       gLogo = "Logo-K3H.bmp"
3. In Form1 :           SendMessage (HWND_FORM1_IMAGEBUTTON1, BM_SetImage, Image_Bitmap, gLogo)           

4. Error :   \CODEGEN_KCREDITEURFB_FORM1_FORM.inc(1187) error 57: Type mismatch, at parameter 4 of SENDMESSAGE() in 'SendMessage       (HWND_FORM1_IMAGEBUTTON1, BM_SetImage, Image_Bitmap, gLogo)         

What am I doing wrong?

José Roca

#1
The last parameter of SendMessge is not the name of a file but the handle of a bitmap. Therefore, you have to load the image usind LoadImage or LoadBitmap to get the handle and pass it to SendMessage.

Klaas Holland

Sorry Jose, I need an example.

I have not 1/100 of the knowledge you have.

Marc Pons


c++ example, quite simple to translate
int main() {
 
    HANDLE hBitMap = LoadImage(0, "img.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
   
    BITMAP bitmap;
    GetObject(hBitMap,sizeof(BITMAP),&bitmap);
    cout << &bitmap;   
    int size = bitmap.bmHeight*bitmap.bmWidth*bitmap.bmBitsPixel/8;
   
    cout << size;
    BYTE *lpBits = new BYTE[ size ];
   
    GetBitmapBits((HBITMAP)hBitMap,size,lpBits );
   
    delete []lpBits;
   
    system("PAUSE");
    return 0;
}

Klaas Holland

Sorry Marc, but this does not make sence to me either.

Paul,

Can you make a Function for me that works like:

FF_CONTROL_SETIMAGE ( HWND_FORM1_IMAGEBUTTON1, gLogo )

Thanks in advance

Paul Squires

I am not at my development computer but based on the info you have provided, it should look something like this:

dim hImage, nWidth, nHeight as dword
nWidth = 32: nHeight = 32
hImage = LoadImage(App.Instance, gLogo, %IMAGE_BITMAP, nWidth, nHeight, %LR_LOADFROMFILE)
SendMessage (HWND_FORM1_IMAGEBUTTON1, BM_SetImage, Image_Bitmap, hImage)

Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Paul Squires

At some point you will have to DeleteObject(hImage) for the image you load, or if you decide to change the image to a different image.
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Klaas Holland

Thanks Paul,

I made a Function of the code and it works Oke.

But the quality of the image is half as good as when I use the Image Library Manager.

Is there a way to improve that?

Paul Squires

Depending on how you used the ImageButton from the Toolbox, you may have used additional window style flags that affected how the image was loaded from the Image Library. Here are some LoadImage flags that could be affecting your image quality:

LR_DEFAULTSIZE
LR_LOADMAP3DCOLORS
LR_LOADTRANSPARENT

Just OR them to your existing flags. For example:
%LR_LOADMAP3DCOLORS Or %LR_LOADFROMFILE

https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-loadimagea
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Klaas Holland

Thanks all,

The nWidth and nHeight must both be set to 0 then the picture shows natural.

Here's the Function:

Function FF_SetImage ( ByVal hControl  as HWnd,  ByRef hLogo  as String ) as String

    Dim hImage   as Long  =  LoadImage (0, hLogo, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE)

    SendMessage (hControl, BM_SetImage, Image_Bitmap, hImage)

    DeleteObject (hImage)
   
End Function