Jump to content

Placing an image on a button [SOLVED]


mr-es335
 Share

Go to solution Solved by mr-es335,

Recommended Posts

Good day,

After some 2 hours, I could not locate a simple and direct response to my question, "How do I place an image on a button in AutoIt?" Rather frustrating indeed!

So, I am providing what I hope will be a simple "response-and-guide" in answer to my query.
• Both the script and the image are being provided.
• Other hopefully relevant information is provided below the actual script

; -----------------------------------------------
#include <ButtonConstants.au3>
#include <FontConstants.au3>
#include <GUIConstantsEx.au3>
; -----------------------------------------------
Local $hGUI = GUICreate("Button Image Test", 345, 65) ; The width and height of the dialog box.[1]
GUISetFont(12, $FW_BOLD, $GUI_FONTNORMAL, "Calibri") ; The size and the type of font being employed. [2]
Local $MyButton = GUICtrlCreateButton("", 20, 20, 150, 25, $BS_BITMAP) ; The postion, size and the GUI Control Styles of the button. [3]
GUICtrlSetImage($MyButton, "my_image.bmp") ; The actual image [4]
Local $Exit = GUICtrlCreateButton("Exit Me Please!", 175, 20, 150, 25) ; As $MyButton, but with no image.
; -----------------------------------------------
GUISetState(@SW_SHOW, $hGUI) ; Display the dialog box.
; -----------------------------------------------
While 1 ; To "catch" any-and-all of the button events.
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $Exit
            ExitLoop ; What occurs when the Exit button is selected.
        Case $MyButton
            MyFunc() ; I simply wanted something to occur when $MyButton is selected.
    EndSwitch
WEnd
; -----------------------------------------------
Func MyFunc() ; In response to the selection of $MyButton.
    MsgBox(0, 'MyButton', 'Inside MyFunc()')
EndFunc   ;==>MyFunc
; -----------------------------------------------
; Observations
; [1] This dialog displays what I refer to as a 1x2 button dialog...that is, 1 button high by two buttons wide!
; [2] I do tend to prefer the Calibri font for all of my button texts. See GUISetFont in the HelpFile.
; [3] Go to the HelpFile and locate GUICtrlCreateButton. For "style" select "GUI Control Styles Appendix".
; [4] As the image is stored within the folder where the script is located,...no "path" is required.

1x2.au3

my_image.bmp

I sincerely hope that the above information will be of some practicable use to others?

"A teacher takes a hand, opens a mind, and touches a heart."

Edited by mr-es335
Link to comment
Share on other sites

On 11/18/2023 at 10:26 AM, mr-es335 said:

After some 2 hours, I could not locate a simple and direct response to my question, "How do I place an image on a button in AutoIt?" Rather frustrating indeed!

https://www.google.com/search?q=autoit+%24BS_BITMAP

This post is good but is not like there was no examples in the forums. Nonetheless thanks for a well documented example.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • Moderators
2 hours ago, mr-es335 said:

Any comments...anyone?

Yes, please do not post simple scripts such as this when there is an essentially similar example in the help file and there is a direct link between GUICtrlCreateButton and GUICtrlSetImage which should lead you straight to the solution. Thanks in advance for your cooperation.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Melba23,

I very much respectfully disagree...I personally tend to find many of the example in the HelpFile - at least to me anyhow...confusing!

Please note: The use of italics and underlines are intentional and theretofore, purposeful! It is imperative that I do not come across as "crass" or, in any way  "disrespectful".

Link to comment
Share on other sites

  • Moderators

mr-es335,

The Help File example:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>

Example()

Func Example()
        GUICreate("My GUI") ; will create a dialog box that when displayed is centered

        GUICtrlCreateButton("my picture button", 10, 20, 40, 40, $BS_ICON)
        GUICtrlSetImage(-1, "shell32.dll", 22)

        GUISetState(@SW_SHOW)

        ; Loop until the user exits.
        While 1
                Switch GUIGetMsg()
                        Case $GUI_EVENT_CLOSE
                                ExitLoop

                EndSwitch
        WEnd
EndFunc   ;==>Example

Please explain what is confusing about that code taken directly from the GUICtrlSetImage page. Concerning the "placing an image on a button" problem, I can see little difference between it and the code you posted in the OP. Your "something to occur" when the button is pressed code is already covered on the GUICtrlCreateButton page - which includes the following text: 

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.

Which, as you can see, has a direct link to the function.

This is a serious question - we want the examples in the Help file to be understandable to even complete beginners.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Melba,

I did read both texts...and I personally, simply do not see the provided content being as "simple" as you suggest! Many times, what I read is exactly the obvious.

All that I wanted to do was to insert an image on a button. I did not want to delve into the differences between an image and an icon. If there are various options, it might be preferable to provide more than one example...as is done with other examples in the HelpFile.

I do hope that this makes sense?

Link to comment
Share on other sites

  • Moderators

mr-es335,

Thank you for responding.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...