When you use a statictext to display images, you can locate them anywhere on a window, with no need for graphicboxes! You can display as many images as you want, and they can be scattered amongst other controls. They don't even need to be flushed to make them stick.
You can use or icons. If you choose to display an icon on a statictext, any transparent areas remain transparent.
Bitmap on statictext.
Icon on statictext.
Icon on statictext, BackgroundColor$ = "blue"
Stylebits
Using STYLEBITS to alter the style of a statictext control.
When we create the control with the STATICTEXT command, we needn't be concerned with the caption, since it won't show. We can also put in any values for width and height, because they are ignored when the style is changed to display an image. The control is automatically sized to fit the image. The X and Y location are important for placement, though.
statictext#1.s,"",10,10,2,2
We need a single line of code to change a statictext control so that it will display a bitmap. We add the _SS_BITMAP style to the control like this:
stylebits#1.s, _SS_BITMAP,0,0,0
We can also display an ICON on the statictext control. To do this, we add the _SS_ICON style to the control like this:
stylebits#1.s, _SS_ICON,0,0,0
Setting the Image
We must use an API call to cause an image to be displayed on a statictext control. The function is SendMessageA. It requires the handle of the statictext, which we retrieve with the HWND() function.
hStatic =hwnd(#1.s)
The message to send is _STM_SETIMAGE. We need to tell it what type of image is being used. We use either _IMAGE_BITMAP or _IMAGE_ICON. We also need to tell it the handle of the image. For a bitmap, we use the Liberty BASIC command LOADBMP, then get the handle with HBMP(). Using an icon is a bit more complicated. See the section later in this article for details.
loadbmp"image",file$
hImage=hbmp("image")calldll#user32,"SendMessageA",_
hStatic asulong,_ 'handle of statictext
_STM_SETIMAGE aslong,_ 'message to set image
_IMAGE_BITMAP aslong,_ 'type of image
hImage asulong,_ 'handle of bitmap
re aslong
API calls must be placed on one line. If the line continuation character is used, the API call can be written on mulitple lines, allowing each argument to be documented. To create shorter code, if you are comfortable with using API calls, write it like this:
You can use the method outlined above to change the image on the statictext as many times as you'd like during a program's execution. It will always cause the statictext to be resized to match the size of the image.
StaticText with Bitmap
The following demo allows the user to select a bitmap file on disk. This demonstrates that the control is automatically sized to fit the image. In a program, you would hard-code a path to the bitmap on disk. If the bitmap is in the same folder as the program, no path information is needed.
Here is the demo program to place a bitmap on a statictext control:
nomainwin'can use hard-coded path such as:'loadbmp "image", "mybmp.bmp"filedialog"Open Bmp","*.bmp",file$
if file$=""thenendstylebits#1.s, _SS_BITMAP,0,0,0statictext#1.s,"",10,10,2,2open"StaticText Image"forWINDOWas#1#1"trapclose [quit]"loadbmp"image",file$
hImage=hbmp("image")
hStatic=hwnd(#1.s)calldll#user32,"SendMessageA",_
hStatic asulong,_ 'handle of statictext
_STM_SETIMAGE aslong,_ 'message to set image
_IMAGE_BITMAP aslong,_ 'type of image
hImage asulong,_ 'handle of bitmap
re aslongwait[quit]unloadbmp"image"close#1:end
StaticText with Icon
There is only one difference in the above technique when an icon is used instead of a bitmap. It is in the way the image is loaded. There is no native Liberty BASIC command to load an icon. To use the ExtractIconA API call, we need to know the instance handle of the window. We get this easily with GetWindowLongA.
The function also reqires the disk filename of the icon. If it is in the program's directory, there is no need for path information in the filename. The function returns the handle of the icon, which is then used to set the image for the statictext control.
CallDLL#shell32,"ExtractIconA",_
hInstance Aslong,_ 'instance handle of window
file$ Asptr,_ 'filename of ico,dll,exe
index Aslong,_ '0-based icon index
hIcon Aslong'returns handle of icon
When the program ends, we need to remove the icon from memory with a call to DestroyIcon.
calldll#user32,"DestroyIcon",_
hIcon aslong,_ 'handle of icon
result aslong
It is also possible to load an icon using LoadImageA. It will allow you to load an icon from disk. The ExtractIconA function has more flexibility, since it allows you to extract the icon from a file on disk, from an executable, from a DLL, or from an icon library (*.icl). This allows you to place all of your icons in a single icon library file, or to extract an icon from the program executable to display on the window.
Demo of Icon on Statictext
The icon API functions in the demo have been wrapped in Liberty BASIC functions to make their use very easy. Here is the demo to place an icon on a statictext control:
nomainwinBackgroundColor$="blue"'can use hard-coded path such as:'icofile$="myicon.ico"filedialog"Open Icon","*.ico;*.exe",icofile$
if icofile$=""thenendstylebits#1.s, _SS_ICON,0,0,0statictext#1.s,"",10,10,2,2open"StaticText Image"forWINDOWas#1#1"trapclose [quit]"
hImage=ExtractIcon(hwnd(#1),icofile$,0)
hStatic=hwnd(#1.s)calldll#user32,"SendMessageA",_
hStatic asulong,_ 'handle of statictext
_STM_SETIMAGE aslong,_ 'message to set image
_IMAGE_ICON aslong,_ 'type of image
hImage asulong,_ 'handle of icon
re aslongwait[quit]
re=DestroyIcon(hImage)close#1:endFunction ExtractIcon(hW, file$, index)
hInst=GetWindowLong(hW, _GWL_HINSTANCE)CallDLL#shell32,"ExtractIconA",_
hInst Aslong,_ 'instance handle of window
file$ Asptr,_ 'filename of ico,dll,exe
index Aslong,_ '0-based icon index
ExtractIcon AslongEndFunctionFunction DestroyIcon(hIcon)calldll#user32,"DestroyIcon",_
hIcon aslong,_ 'handle of icon
DestroyIcon aslongEndFunctionFunction GetWindowLong(hW, type)CallDLL#user32,"GetWindowLongA",_
hW Aslong,_ 'window handle
type Aslong,_ 'flag for type
GetWindowLong AslongEndFunction
Images On Statictext
-Images On Statictext | Why Use Statictext? | Stylebits | Setting the Image | Changing the Image | StaticText with Bitmap | Demo of BMP on Statictext | StaticText with Icon | Demo of Icon on Statictext
Why Use Statictext?
Why use a statictext control to display images?When you use a statictext to display images, you can locate them anywhere on a window, with no need for graphicboxes! You can display as many images as you want, and they can be scattered amongst other controls. They don't even need to be flushed to make them stick.
You can use or icons. If you choose to display an icon on a statictext, any transparent areas remain transparent.
Stylebits
Using STYLEBITS to alter the style of a statictext control.When we create the control with the STATICTEXT command, we needn't be concerned with the caption, since it won't show. We can also put in any values for width and height, because they are ignored when the style is changed to display an image. The control is automatically sized to fit the image. The X and Y location are important for placement, though.
We need a single line of code to change a statictext control so that it will display a bitmap. We add the _SS_BITMAP style to the control like this:
We can also display an ICON on the statictext control. To do this, we add the _SS_ICON style to the control like this:
Setting the Image
We must use an API call to cause an image to be displayed on a statictext control. The function is SendMessageA. It requires the handle of the statictext, which we retrieve with the HWND() function.The message to send is _STM_SETIMAGE. We need to tell it what type of image is being used. We use either _IMAGE_BITMAP or _IMAGE_ICON. We also need to tell it the handle of the image. For a bitmap, we use the Liberty BASIC command LOADBMP, then get the handle with HBMP(). Using an icon is a bit more complicated. See the section later in this article for details.
API calls must be placed on one line. If the line continuation character is used, the API call can be written on mulitple lines, allowing each argument to be documented. To create shorter code, if you are comfortable with using API calls, write it like this:
Changing the Image
You can use the method outlined above to change the image on the statictext as many times as you'd like during a program's execution. It will always cause the statictext to be resized to match the size of the image.
StaticText with Bitmap
The following demo allows the user to select a bitmap file on disk. This demonstrates that the control is automatically sized to fit the image. In a program, you would hard-code a path to the bitmap on disk. If the bitmap is in the same folder as the program, no path information is needed.
Demo of BMP on Statictext
Here is the demo program to place a bitmap on a statictext control:StaticText with Icon
There is only one difference in the above technique when an icon is used instead of a bitmap. It is in the way the image is loaded. There is no native Liberty BASIC command to load an icon. To use the ExtractIconA API call, we need to know the instance handle of the window. We get this easily with GetWindowLongA.
The function also reqires the disk filename of the icon. If it is in the program's directory, there is no need for path information in the filename. The function returns the handle of the icon, which is then used to set the image for the statictext control.
When the program ends, we need to remove the icon from memory with a call to DestroyIcon.
It is also possible to load an icon using LoadImageA. It will allow you to load an icon from disk. The ExtractIconA function has more flexibility, since it allows you to extract the icon from a file on disk, from an executable, from a DLL, or from an icon library (*.icl). This allows you to place all of your icons in a single icon library file, or to extract an icon from the program executable to display on the window.
Demo of Icon on Statictext
The icon API functions in the demo have been wrapped in Liberty BASIC functions to make their use very easy. Here is the demo to place an icon on a statictext control:Images On Statictext | Why Use Statictext? | Stylebits | Setting the Image | Changing the Image | StaticText with Bitmap | Demo of BMP on Statictext | StaticText with Icon | Demo of Icon on Statictext