Hello Robin. Congratulations on your purchase of PureBasic, and great to hear that you're getting on well with it.
RobKiwi wrote:The following lines work but I don't know why:
Code: Select all
If LoadImage(0, picture.jpg) ; this does return an image
SetGadgetState(1, ImageID(0))
EndIf
That is for an image box at the top of the enumeration list , #1. But
why is the SetGadgetState needed? It doesn't work without it.
I can't really comment on PureVision simply because I haven't personally used it, but I'm assuming that you'd find something similar to the following line in your code, where the code for the ImageGadget() was generated with a null value for the picture:
Code: Select all
ImageGadget(1, 10, 10, 50, 50, 0) ;<-- zero value sets no picture!
The sixth parameter of the ImageGadget() function should contain the ImageID() of the picture to be displayed. Since no picture was selected in the above line, the following code uses the SetGadgetState() function to place a picture into the ImageGadget():
Code: Select all
If LoadImage(0, "picture.jpg") ;<-- loads picture.jpg and assigns it the number 0
SetGadgetState(1, ImageID(0)) ;<-- places image number 0 (which is picture.jpg) into gadget number 1
EndIf
However, this approach of using SetGadgetState() to change the picture will only work with ImageGadget(). For ButtonImageGadget() you'd have to use the SetGadgetAttribute() function with the #PB_Button_Image flag. Any one of these formats is valid:
An image "c:\picture1.bmp" is loaded and assigned the number 123, and used in two gadgets:
Code: Select all
wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
If OpenWindow(0, 0, 0, 200, 200, "Load Image:", wFlags)
LoadImage(123, "c:\picture1.bmp")
ImageGadget(1, 10, 70, 50, 50, ImageID(123))
ButtonImageGadget(2, 10, 10, 50, 50, ImageID(123))
EndIf
While WaitWindowEvent() ! #PB_Event_CloseWindow : CloseWindow : Wend
Two images "c:\picture1.bmp" & "C:\picture2.bmp" are loaded and assigned the numbers 123 & 456 respectively, and used interchangeably between two gadgets:
Code: Select all
wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
If OpenWindow(0, 0, 0, 200, 200, "Load Image:", wFlags)
LoadImage(123, "c:\picture1.bmp")
LoadImage(456, "c:\picture2.bmp")
ImageGadget(1, 10, 70, 50, 50, ImageID(123))
ButtonImageGadget(2, 10, 10, 50, 50, ImageID(456))
MessageRequester("Changing Gadget Images:", "Click OK to switch the pictures...")
SetGadgetState(1, ImageID(456))
SetGadgetAttribute(2, #PB_Button_Image, ImageID(123))
While WaitWindowEvent() ! #PB_Event_CloseWindow : CloseWindow : Wend
EndIf
The image "c:\apicture.bmp" is loaded directly into the ImageGadget() by using the #PB_Any constant:
Code: Select all
ImageGadget(1, 10, 70, 50, 50, ImageID(LoadImage(#PB_Any, "c:\picture1.bmp")))
RobKiwi wrote:...all the examples deal with MAKING the image box, not using an existing one. I do not understand what the "0", "1" and again "0" refer to...
Basically, even PureVision is
making the image box, but the automation leads it to create an empty one first, then setting the picture using the SetGadgetState() function when one is selected. That way, if no picture is selected, the generated code will not raise any errors.
As far as the numbers are concerned, they are referring to gadget and image numbers. PureBasic uses a numbering system to handle the windows, gadgets and other resources. So, in this example of yours:
Code: Select all
If LoadImage(0, picture.jpg) ;should be in quotes - "picture.jpg"
SetGadgetState(1, ImageID(0))
EndIf
the function LoadImage() loads picture.jpg into memory and assigns it the
resource number 0. Then, the SetGadgetState() function places this newly loaded resource into a previously created
gadget number 1, which is the ImageGadget(). But, as you can see, the resource has to be passed to the gadget using its ID, so it is passed as ImageID(0). The same system and syntax is adopted throughout PureBasic, with every created gadget and resource assigned a unique number, and used in conjunction with their corresponding functions, WindowID(), GadgetID(), FontID(), etc.
(you can assign same numbers to different resources, which means that you can, for example, have a window, a gadget, and an image all with the number 0)
I hope it answers your questions.