|
Summary
With CreateObject functions you can create MMB objects directly from script! Previously, you had to design everything in designer and without an option to prepare some dynamic actions, like drawing lines, creating dynamic forms based on selected options, etc.
Sure, there was always an option to pre-made all needed objects, hide them from users and then show them at proper time. But it was not very useful solution, especially for games or heavily scripted projects. So here we are. From MMB 4.9.8 you can dynamically create most of basics MMB objects and even delete them (to save memory) using a DeleteObject function. To change the appearance of newly created objects use SetObjectParam function.
Here is the list of available commands, allowing you to create objects in a runtime:
| • | Text Button - CreateTextButton("inlabel","outlabel$,x,y,w,h,text") |
| • | Text Label - CreateText("inlabel","outlabel$,x,y,text") |
| • | Paragraph - CreateParagraph("inlabel","outlabel$,x,y,w,h,text") |
| • | Circle - CreateCircle("inlabel","outlabel$,x,y,w,h,r,g,b") |
| • | Rectangle - CreateRectangle("inlabel","outlabel$,x,y,w,h,r,g,b") |
| • | Line (relative positioning) - CreateLine("inlabel","outlabel$,x,y,w,h,r,g,b") |
| • | Line (absolute positioning) - CreateLineAB("inlabel","outlabel$,x1,y1,x2,y2,r,g,b") |
| • | HotSpot - CreateHotSpot("inlabel","outlabel$,x,y,w,h") |
| • | Script object - CreateScript("inlabel","outlabel$") |
Where:
x is object's x position
y is object's y position
w is object's width
h is object's height
r,g,b is object's Red, Green, Blue color (in range 0..255)
text is object's text string
inlabel is a user-defined objet's name, let's say Text. If there already is an object with such name in the project, there will be added a number after the suggested name and this new name will be passed to outlabel$ variable (or whatever this variable will be called in your project). As you may know, you can't have two objects of the same name on the same page.
In short, if the object already exists (let's say Text), there will be used first available name based of suggested inlabel string (let's say Text13). This name will be then passed to outlabel$ variable and used by MMB. If you would like to delete or hide this object, you will have to use the outlabel$ name in the appropriate function.
All parameters (except outlabel$) are IN parameters and can be defined as a numerical or string variable.
All parameters are optional, but it's recommended to define at least the text/position ;)
If there is no inlabel defined, the Create function uses default object names (like TextBTN, Circle,...). If there is no position/size defined, the object is created at 0,0 and with default (hardcoded) size.
You can skip some parameters, but you still have to use the appropriate number of comas, as a parameters separator. So let's say, you want to create Text Button with defined position and Text, but leave on MMB to use the default size. The code should appear like this..
CreateTextButton("inlabel","outlabel$,x,y,,,text")
CreateTextButton("inlabel","outlabel$,x,y,w,h,text")
|
|
Description
|
|
With this function you can create Text Button object. This will probably be the most used Create Object function. Available parameters:
in/out label
x,y - position
w,h - width/height
text - default button's text
To define other Text Button parameters (like BG color, text color, font, etc..) use SetObjectParam function right after CreateTextButton.
|
|
Code Examples
|
|
** this creates Text Button object called "BTN"
CreateTextButton("BTN","outlabel$,10,10,50,25,OK")
** or like this via script variables..
inlabel$='BTN'
x=10
y=10
w=50
h=25
text$='OK'
CreateTextButton("inlabel$","outlabel$,x,y,w,h,text$")
** and to change the button color use this..
rgbcol$='255,0,255'
SetObjectParam("outlabel$","BGCOLOR=rgbcol$")
** you can also use expressions in Create Object functions..
n=n+10
CreateTextButton("inlabel$","outlabel$,x,y,w+n,h+n,text$")
|
|
CreateText("inlabel","outlabel$,x,y,text")
|
|
Description
|
|
With this function you can create Text object. Available parameters:
in/out label
x,y - position
text - default text
To define other Text parameters (like text color, font, etc..) use SetObjectParam function right after CreateText function.
|
|
Code Examples
|
|
** this creates Text object called "TXT"..
CreateText("TXT","outlabel$,10,10,Short Text")
** or like this via script variables..
inlabel$='TXT'
x=10
y=10
text$='Short Text'
CreateText("inlabel$","outlabel$,x,y,text$")
** and to change the text color use this..
rgbcol$='255,0,255'
SetObjectParam("outlabel$","TEXTCOLOR=rgbcol$")
|
|
CreateParagraph("inlabel","outlabel$,x,y,w,h,text")
|
|
Description
|
|
With this function you can create Paragraph object. Available parameters:
in/out label
x,y - position
w,h - width/height
text - default paragraph text
To define other Paragraph parameters (like text color, font, etc..) use SetObjectParam function right after CreateParagraph function.
|
|
Code Examples
|
|
** this creates Paragraph object called "PARA"..
CreateParagraph("PARA","outlabel$,10,10,320,200,Long text in Paragraph")
** or like this via script variables..
inlabel$='PARA'
x=10
y=10
w=320
h=200
text$='Lorem ipsum dolor sit amet et condimentum lectus sodales litora parturient.'
CreateParagraph("inlabel$","outlabel$,x,y,w,h,text$")
** and to change the paragraph text color use this..
rgbcol$='255,0,255'
SetObjectParam("outlabel$","TEXTCOLOR=rgbcol$")
|
|
CreateCircle("inlabel","outlabel$,x,y,w,h,r,g,b")
CreateRectangle("inlabel","outlabel$,x,y,w,h,r,g,b")
|
|
Description
|
|
With these two functions you can create Circle or Rectangle object. Available parameters:
in/out label
x,y - position
w,h - width/height
r,g,b - default fill color
To define (or change) Circle/Rectangle parameters (like fill color, border type or color) use SetObjectParam function right after CreateCircle or CreateRectangle function.
|
|
Code Examples
|
|
** this creates Circle object called "CIRC"..
CreateCircle("CIRC","outlabel$,10,10,50,50,128,0,128")
** or like this via script variables..
inlabel$='CIRC'
x=10
y=10
w=50
h=50
** generate random color..
r=RND(254)+1
g=RND(254)+1
b=RND(254)+1
CreateCircle("inlabel$","outlabel$,x,y,w,h,r,g,b")
** and to change the circle fill color and border color use this code..
r=RND(254)+1
g=RND(254)+1
b=RND(254)+1
SetObjectParam("outlabel$","BORDERCOLOR=r,g,b")
|
|
CreateLine("inlabel","outlabel$,x,y,w,h,r,g,b")
CreateLineAB("inlabel","outlabel$,x1,y1,x2,y2,r,g,b")
|
|
Description
|
|
With CreateLine and CreateLineAB function you can create Line object. The difference between these two functions is in entering the position of the second (ending) point of the line.
Available parameters for CreateLine:
in/out label
x,y - position
w,h - width/height
r,g,b - default fill color
Available parameters for CreateLineAB:
in/out label
x1,y1 - position
x2,y2 - width/height
r,g,b - default fill color
While the Line created with CreateLineAB is defined by two points with absolute position on the project window, the line created with CreateLine is defined by the width/height of the line bounding box. In other words, both functions with exactly the same "position" values may produce different results, because of different meaning of the second pair of position values.
Please see the following image:

Here is the code used in the above screenshot:
CreateLine("LINE","outlabel$,20,20,280,200,255,0,0")
CreateLineAB("LINE","outlabel$,20,20,280,200,0,0,255")
As you may know, most of MMB objects are defined by x/y position of their left-top corner and width/height, which in fact represents the bottom-right corner of the object. This is also the case of CreateLine function. Although it may sound useless (to define the line with width/height) it's very useful in cases when you need to create multiple lines and you want to use relative positioning.
|
|
Code Examples
|
|
** this creates Line object called "LINE" and using the absolute position values..
CreateLineAB("LINE","outlabel$,20,20,280,200,0,0,255")
** this creates Line object called "LINE" but this time using the width/height values..
CreateLine("LINE","outlabel$,20,20,280,200,255,0,0")
|
|
CreateHotSpot("inlabel","outlabel$,x,y,w,h")
|
|
Description
|
|
With CreateHotSpot function you can create hotspot object. It's useful for creating clickable active areas in your project. But as you may (or may not?;) know, the HotSpot can also be used as an image container! You can load an image into the HotSpot using the ReplaceImage function. And then the HotSpot object can be used and manipulated as Bitmap object (using the available Bitmap related commands).
Available parameters for CreateHotSpot:
in/out label
x,y - position
w,h - width/height
|
|
Code Examples
|
|
In this example you can learn how fill the project window with hotspots containing images and how to calculate the maximum number of images for project width/height.
pw=PubWidth()
ph=PubHeight()
** size of space between the images
coef=10
w=50
h=50
** count the number of images for current width..
n_imgs_w=pw/(w+coef)
RoundNum1=INT(n_imgs_w)
RoundNum2=n_imgs_w-RoundNum1
If (RoundNum2>=0.5) Then
n_imgs_w=RoundNum1 + 1
End
** and the number of images (image columns) is..
n_imgs_w=n_imgs_w-1
** n of images for current height
n_imgs_h=ph/(h+coef)
RoundNum1=INT(n_imgs_h)
RoundNum2=n_imgs_h-RoundNum1
If (RoundNum2>=0.5) Then
n_imgs_h=RoundNum1 + 1
End
** and the number of images (image rows) is..
n_imgs_h=n_imgs_h-1
********************
x=coef
y=coef
For j=1 To n_imgs_h
For i=1 To n_imgs_w
Pause("50")
CreateHotSpot("HSpot","object[i]$,x,y,w,h")
** set new x (column) postition
x=x+w+coef
** set script to HotSpot
script$='CurObj$=CurrentObject()'+CHR(13)+CHR(10)
script$= script$ + 'Message("Selected Object","CurObj$")' +CHR(13)+CHR(10)
SetObjectParam("object[i]$","MOUSEUPSCRIPT:1=script$")
** set repeating of images. If you for example have only 3 images in the folder
** and the number of added hotspots is higher, you will have to repeat the images.
** In this sample we have only 3 images in Images folder, so if the actual image count is
** higher or equal 3, reset the image count..
If (ni>=3) Then
ni=1
Else
** else add 1 to the image count..
ni=ni+1
End
imgpath$='<SrcDir>\Images\\'+CHAR(ni)+'.jpg'
** set image to hotspot object
ReplaceImage("object[i]$","imgpath$")
** move object to back (change object Z-order)
ReorderObject("object[i]$","BACK")
Refresh("")
Next i
** reset x (column) position
x=coef
** new y (line) position
y=y+h+coef
Next j
|
|
CreateScript("inlabel","outlabel$")
|
|
Description
|
|
With this function you can create Script object. Available parameters:
in/out label
The Script object is the small yellow rectangle holding the scripts, which can be called via RunScript or ScriptTimer. It's some kind of procedure, which can be called from another, processed (via RunScript) or run simultaneously (using ScriptTimer) with another part of your project. Usually, Script objects are created at a design time. But thanks to CreateScript you can now create new Script object also in a runtime and fill it with script using the SetObjectParam, which now allows (from 4.9.8) to replace the actual object's script with new content. The script can be generated via script, loaded from predefined file, or simply typed in EditBox ;)
|
|
Code Examples
|
|
** this creates Script object called "SCRPT"..
CreateScript("SCRPT","outlabel$")
** set new script..
ScriptParam$='SCRIPT:1=' + 'Message("Hello World","")'
SetObjectParam("outlabel$","ScriptParam$")
** this runs the newly created script object
RunScript("outlabel$")
** create new Script object, this time with multiline script..
CreateScript("SCRPT","outlabel$")
** set new script..
script$='SCRIPT:1=' + 'For i=1 To 10'+CHR(13)+CHR(10)
script$= script$ + 'loop$=CHAR(i)'+CHR(13)+CHR(10)
script$= script$ + 'loop$=CHAR(i)'+CHR(13)+CHR(10)
script$= script$ + 'Message("Loop:","loop$")'+CHR(13)+CHR(10)
script$= script$ + 'Next i'+CHR(13)+CHR(10)
SetObjectParam("outlabel$","script$")
** this runs the newly created script object
RunScript("outlabel$")
|
|
DeleteObject("ObjectLabel")
|
|
Description
|
|
DeleteObject("ObjectLabel")
With this function you can delete both objects created in a designer or dynamically in a runtime. All you need to do is to apply the correct (existing) object label into the function parameter. Be careful with using this function! Once you delete the object, the only way how to get it back is to restart the project or create the object with runtime object creation functions.
|
|
Code Examples
|
|
** this deletes the "TextBTN"
DeleteObject("TextBTN")
** this deletes all text buttons from TextBTN1 to TextBTN10
For i=1 To 10
DeleteObject("TextBTN[i]")
Next i
|
|
|