PlanetSquires Forums

Support Forums => Other Software and Code => Topic started by: Klaas Holland on August 22, 2018, 07:10:26 PM

Title: Connect an Image to the ImageButton
Post by: Klaas Holland on August 22, 2018, 07:10:26 PM
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?
Title: Re: Connect an Image to the ImageButton
Post by: José Roca on August 22, 2018, 07:26:24 PM
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.
Title: Re: Connect an Image to the ImageButton
Post by: Klaas Holland on August 23, 2018, 05:16:42 AM
Sorry Jose, I need an example.

I have not 1/100 of the knowledge you have.
Title: Re: Connect an Image to the ImageButton
Post by: Marc Pons on August 23, 2018, 01:37:05 PM

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;
}
Title: Re: Connect an Image to the ImageButton
Post by: Klaas Holland on August 23, 2018, 02:54:24 PM
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
Title: Re: Connect an Image to the ImageButton
Post by: Paul Squires on August 23, 2018, 04:29:01 PM
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)

Title: Re: Connect an Image to the ImageButton
Post by: Paul Squires on August 23, 2018, 04:31:15 PM
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.
Title: Re: Connect an Image to the ImageButton
Post by: Klaas Holland on August 24, 2018, 06:33:58 AM
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?
Title: Re: Connect an Image to the ImageButton
Post by: Paul Squires on August 24, 2018, 08:50:38 AM
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
Title: Re: Connect an Image to the ImageButton
Post by: Klaas Holland on August 24, 2018, 12:40:15 PM
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