Search code examples
delphivcl

is it possible to load a jpg/png image into a TbitBtn or a TSpeedButton?


I need to display an image in a button, so that it is visually easier for the user to know what the button is for instead of reading its text.

as far as I can tell TBitBtn and TSpeedButton are buttons that can show an image, but I do not know anything about it and I would like to know if it is possible to have a .JPG or .PNG file and load that image into the button so that it displays it.


Solution

  • TBitBtn and TSpeedButton only support BMP images, not JPG/PNG. You would have to convert the JPG/PNG images to BMP (which can be done in code by loading the images into TJPEGImage/TPNGImage first, and then Assign() them to the button's Glyph, which is a TBitmap).

    In modern Delphi versions, TButton has an Images property that you can assign any TCustomImageList to, and you can add PNG images to a standard TImageList, or use a 3rd party PNG ImageList.

    Otherwise, you can create your own owner-drawn button to draw JPG/PNG images directly. Derive from TButton and override its CreateParams() method to enable the BS_OWNERDRAW style, and then handle the WM_DRAWITEM message to draw the button however you want (this is what TBitBtn does).