Razraal

Button with image

9 posts in this topic

Hey there.

I want to create a button with an image. So far I've been unable to do it.

Here's the code that creates the button and sets the image:

$button = GUICtrlCreateButton ("", 10, 10, 40, 40)
GUICtrlSetImage ($button, "my_image.jpg")

Is there any other step I need to do?

Share this post


Link to post
Share on other sites



Additional info:

The "my_image.jpg" file is a 1024x1024 picture.

Share this post


Link to post
Share on other sites

Additional info:

Added the $BS_BITMAP style to the GUICtrlCreateButton.

$button = GUICtrlCreateButton ("", 10, 10, 40, 40, $BS_BITMAP)
GUICtrlSetImage ($button, "my_image.jpg")

Still fails to show the image.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Razraal,

Welcome to the AutoIt forums.

From the Help file for GUICtrlCreateButton:

Quote

A Button control can display an icon or image by using the $BS_ICON or $BS_BITMAP style. Use GUICtrlSetImage() to specify the picture to use.

So you need to add the style when creating the button:

$button = GUICtrlCreateButton ("", 10, 10, 40, 40, $BS_BITMAP)
GUICtrlSetImage ($button, "my_image.jpg")

And add the #include <ButtonConstants.au3> line at the top of the script.

M23

Edit 1: Our posts crossed. Is the image file in the same folder as the script?

Edit 2: It seems that it does not work with jpg files - you need to convert it to a BMP. Sorry about that - I have never tried it with a jpg file.

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
11 minutes ago, Melba23 said:

Edit 2: It seems that it does not work with jpg files - you need to convert it to a BMP. Sorry about that - I have never tried it with a jpg file.

Hey there @Melba23, thanks for the welcoming!

I've tried with a bmp image, also does not work.

$button = GUICtrlCreateButton ("", 10, 10, 40, 40, $BS_BITMAP)
GUICtrlSetImage ($button, "my_image.bmp")

Converted the original jpg image through GIMP to a bmp image. Same resolution. Same result.

Share this post


Link to post
Share on other sites

Razraal,

I have just tried with a large bmp file and I note that the image is displayed full-size, so only the very centre of the image is shown - which might lead you to think that there is nothing there. Obviously scaling is not done automatically as in the case of a picture control - you learn something new every day.

You will need to rescale the image to the button size - putting a 1000x1000 image on a 40x40 button means you are not going to see much detail anyway.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
3 minutes ago, Melba23 said:

Razraal,

I have just tried with a large bmp file and I note that the image is displayed full-size, so only the very centre of the image is shown - which might lead you to think that there is nothing there. Obviously scaling is not done automatically as in the case of a picture control - you learn something new every day.

You will need to rescale the image to the button size - putting a 1000x1000 image on a 40x40 button means you are not going to see much detail anyway.

M23

Thanks for your help mate, I appreciate it!

I've decided to create an image control with GUICtrlCreateImage, instead of a button with an image, since the overal result is almost the same.

Share this post


Link to post
Share on other sites

Razraal,

Delighted I could help.

But when you reply in future, could you please use the "Reply to this topic" button at the top of the thread or the "Reply to this topic" editor at the bottom rather than the "Quote" button - responders know what they wrote and it just pads the thread unnecessarily.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Sure thing Melba!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now