ICON: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "{{DISPLAYTITLE:_ICON}} The _ICON statement uses an image handle from _LOADIMAGE for the program header and icon image in the OS. {{PageSyntax}} : _ICON [{{Parameter|mainImageHandle&}}[, {{Parameter|smallImageHandle&}}]] {{Parameters}} * {{Parameter|mainImageHandle&}} is the LONG handle value of the OS icon and title bar image pre-loaded with _LOADIMAGE when used alone. * {{Parameter|smallImageHandle&}} is the LONG handle value of a different...")
 
No edit summary
Line 15: Line 15:
{{PageDescription}}
{{PageDescription}}
* If no image handle is passed, the default QB64 icon will be used (all versions). If the [[$EXEICON]] metacommand is used, [[_ICON]] without an image handle uses the embedded icon from the binary (Windows only).
* If no image handle is passed, the default QB64 icon will be used (all versions). If the [[$EXEICON]] metacommand is used, [[_ICON]] without an image handle uses the embedded icon from the binary (Windows only).
* Beginning with '''version 1.000''', the following is considered:  
* Beginning with '''version 1.000''', the following is considered:
:::{{Parameter|mainImageHandle&}} creates the image as the icon in the OS and the image in the program header (title bar).
:::{{Parameter|mainImageHandle&}} creates the image as the icon in the OS and the image in the program header (title bar).
:::{{Parameter|smallImageHandle&}} can be used for a different image in the program header bar.
:::{{Parameter|smallImageHandle&}} can be used for a different image in the program header bar.
Line 23: Line 23:


{{PageErrors}}
{{PageErrors}}
* '''NOTE: Icon files are not supported with [[_LOADIMAGE]] and an error will occur. See Example 2.'''  
* '''NOTE: Icon files are not supported with [[_LOADIMAGE]] and an error will occur. See Example 2.'''
* Images used can be smaller or larger than 32 X 32 pixels, but image resolution may be affected.
* Images used can be smaller or larger than 32 X 32 pixels, but image resolution may be affected.
* It is important to free unused or uneeded images with [[_FREEIMAGE]] to prevent memory overflow errors.
* It is important to free unused or uneeded images with [[_FREEIMAGE]] to prevent memory overflow errors.
Line 31: Line 31:
{{PageExamples}}
{{PageExamples}}
''Example 1:'' Loading an image to a 32 bit palette in SCREEN 0 (the default screen mode).
''Example 1:'' Loading an image to a 32 bit palette in SCREEN 0 (the default screen mode).
{{CodeStart}} '' ''
{{CodeStart}}
i& ={{Cl|_LOADIMAGE}}("RDSWU16.BMP", 32) '<<<<<<< use your image file name here
i& ={{Cl|_LOADIMAGE}}("RDSWU16.BMP", 32) '<<<<<<< use your image file name here


{{Cl|IF}} i& < -1 THEN  
{{Cl|IF}} i& < -1 THEN
   {{Cl|_ICON}} i&
   {{Cl|_ICON}} i&
   {{Cl|_FREEIMAGE}} i& ' release image handle after setting icon
   {{Cl|_FREEIMAGE}} i& ' release image handle after setting icon
{{Cl|END IF}}
{{Cl|END IF}}
{{CodeEnd}}
{{CodeEnd}}
:''Note:'' _ICON images can be freed if the [[SCREEN]] mode stays the same. Freed image handles can on longer be referenced.  
:''Note:'' _ICON images can be freed if the [[SCREEN]] mode stays the same. Freed image handles can on longer be referenced.




''Example 2:'' Function that converts an icon into a temporary bitmap for use in QB64. Function returns the available image count.
''Example 2:'' Function that converts an icon into a temporary bitmap for use in QB64. Function returns the available image count.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|SCREEN}} {{Cl|_NEWIMAGE}}(640, 480, 256)
{{Cl|SCREEN}} {{Cl|_NEWIMAGE}}(640, 480, 256)
{{Cl|_TITLE}} "Icon Converter"
{{Cl|_TITLE}} "Icon Converter"
Line 56: Line 56:
     {{Cl|_PUTIMAGE}} (300, 250), img& 'place image on screen
     {{Cl|_PUTIMAGE}} (300, 250), img& 'place image on screen
     {{Cl|_FREEIMAGE}} img& '          always free unused handles to save memory
     {{Cl|_FREEIMAGE}} img& '          always free unused handles to save memory
     {{Cl|KILL}} bitmap$ '              comment out and/or rename to save the bitmaps  
     {{Cl|KILL}} bitmap$ '              comment out and/or rename to save the bitmaps
   {{Cl|END IF}}
   {{Cl|END IF}}
{{Cl|END IF}}
{{Cl|END IF}}
Line 69: Line 69:
rf = {{Cl|FREEFILE}}
rf = {{Cl|FREEFILE}}
{{Cl|IF...THEN|IF}} {{Cl|LCASE$}}({{Cl|RIGHT$}}(filein, 4)) = ".ico" {{Cl|THEN}} 'check file extension is ICO only
{{Cl|IF...THEN|IF}} {{Cl|LCASE$}}({{Cl|RIGHT$}}(filein, 4)) = ".ico" {{Cl|THEN}} 'check file extension is ICO only
   {{Cl|OPEN}} filein {{Cl|OPEN|FOR}} {{Cl|BINARY}} {{Cl|ACCESS}} {{Cl|ACCESS|READ}} {{Cl|AS}} rf  
   {{Cl|OPEN}} filein {{Cl|OPEN|FOR}} {{Cl|BINARY}} {{Cl|ACCESS}} {{Cl|ACCESS|READ}} {{Cl|AS}} rf
{{Cl|ELSE}} {{Cl|EXIT FUNCTION}}
{{Cl|ELSE}} {{Cl|EXIT FUNCTION}}
{{Cl|END IF}}
{{Cl|END IF}}
Line 88: Line 88:


{{Cl|SEEK}} rf, 1 + offset + 14 'only read the BPP in BMP header
{{Cl|SEEK}} rf, 1 + offset + 14 'only read the BPP in BMP header
{{Cl|GET}} rf, , word: bpp = word  
{{Cl|GET}} rf, , word: bpp = word
{{Cl|IF...THEN|IF}} bpp = 0 {{Cl|THEN}} {{Cl|CLOSE}} rf: {{Cl|EXIT FUNCTION}}
{{Cl|IF...THEN|IF}} bpp = 0 {{Cl|THEN}} {{Cl|CLOSE}} rf: {{Cl|EXIT FUNCTION}}
{{Cl|IF...THEN|IF}} bpp <= 24 {{Cl|THEN}} pixelbytes = bpp / 8 {{Cl|ELSE}} pixelbytes = 3
{{Cl|IF...THEN|IF}} bpp <= 24 {{Cl|THEN}} pixelbytes = bpp / 8 {{Cl|ELSE}} pixelbytes = 3
Line 116: Line 116:
{{Cl|CLOSE}} rf, wf
{{Cl|CLOSE}} rf, wf
Icon2BMP = count '            return the number of icons available in the icon file
Icon2BMP = count '            return the number of icons available in the icon file
{{Cl|END FUNCTION}} '' ''
{{Cl|END FUNCTION}}
{{CodeEnd}}{{small|Code by Ted Weissgerber}}
{{CodeEnd}}{{small|Code by Ted Weissgerber}}
: ''Note:'' Once the file has been loaded into memory, the image handle can still be used even after the file has been deleted.
: ''Note:'' Once the file has been loaded into memory, the image handle can still be used even after the file has been deleted.

Revision as of 01:47, 23 January 2023

The _ICON statement uses an image handle from _LOADIMAGE for the program header and icon image in the OS.


Syntax

_ICON [mainImageHandle&[, smallImageHandle&]]


Template:Parameters

  • mainImageHandle& is the LONG handle value of the OS icon and title bar image pre-loaded with _LOADIMAGE when used alone.
  • smallImageHandle& is the LONG handle value of a different title bar image pre-loaded with _LOADIMAGE when used.
  • No image handle designates use of the default QB64 icon or the embedded icon set by $EXEICON.


Description

  • If no image handle is passed, the default QB64 icon will be used (all versions). If the $EXEICON metacommand is used, _ICON without an image handle uses the embedded icon from the binary (Windows only).
  • Beginning with version 1.000, the following is considered:
mainImageHandle& creates the image as the icon in the OS and the image in the program header (title bar).
smallImageHandle& can be used for a different image in the program header bar.
  • The header image will automatically be resized to fit the icon size of 16 X 16 if smaller or larger.
  • Once the program's icon is set, the image handle can be discarded with _FREEIMAGE.


Template:PageErrors

  • NOTE: Icon files are not supported with _LOADIMAGE and an error will occur. See Example 2.
  • Images used can be smaller or larger than 32 X 32 pixels, but image resolution may be affected.
  • It is important to free unused or uneeded images with _FREEIMAGE to prevent memory overflow errors.
  • In SCREEN 0 (default text mode) you need to specify 32-bit mode in _LOADIMAGE to load images.


Examples

Example 1: Loading an image to a 32 bit palette in SCREEN 0 (the default screen mode).

i& =_LOADIMAGE("RDSWU16.BMP", 32) '<<<<<<< use your image file name here

IF i& < -1 THEN
  _ICON i&
  _FREEIMAGE i& ' release image handle after setting icon
END IF
Note: _ICON images can be freed if the SCREEN mode stays the same. Freed image handles can on longer be referenced.


Example 2: Function that converts an icon into a temporary bitmap for use in QB64. Function returns the available image count.

SCREEN _NEWIMAGE(640, 480, 256)
_TITLE "Icon Converter"
icon$ = "daphne.ico"     '<<<<<<<<< change icon file name
bitmap$ = "tempfile.bmp"
indx% = 6  '1 minimum <<<<<<< higher values than count get highest entry image in icon file

IF Icon2BMP(icon$, bitmap$, indx%) THEN
  img& = _LOADIMAGE(bitmap$) '  use 32 as color mode in SCREEN 0
  IF img& < -1 THEN '           check that handle value is good before loading
    _ICON img& '                place image in header
    _PUTIMAGE (300, 250), img& 'place image on screen
    _FREEIMAGE img& '           always free unused handles to save memory
    KILL bitmap$ '              comment out and/or rename to save the bitmaps
  END IF
END IF
END
'                ----------------------------------------------------

FUNCTION Icon2BMP% (filein AS STRING, fileout AS STRING, index AS INTEGER)
'function creates a bitmap of the icon and returns the icon count
DIM byte AS _UNSIGNED _BYTE, word AS INTEGER, dword AS LONG
DIM wide AS LONG, high AS LONG, BM AS INTEGER, bpp AS INTEGER

rf = FREEFILE
IF LCASE$(RIGHT$(filein, 4)) = ".ico" THEN 'check file extension is ICO only
  OPEN filein FOR BINARY ACCESS READ AS rf
ELSE EXIT FUNCTION
END IF
GET rf, , word
GET rf, , word: icon = word
GET rf, , word: count = word
IF icon <> 1 OR count = 0 THEN CLOSE rf: EXIT FUNCTION
'PRINT icon, count
IF index > 0 AND index <= count THEN entry = 16 * (index - 1) ELSE entry = 16 * (count - 1)
SEEK rf, 1 + 6 + entry 'start of indexed Entry header
GET rf, , byte: wide = byte ' use this unsigned for images over 127
GET rf, , byte: high = byte ' use this unsigned because it isn't doubled
GET rf, , word 'number of 4 BPP colors(256 & 32 = 0) & reserved bytes
GET rf, , dword '2 hot spots both normally 0 in icons, used for cursors
GET rf, , dword: size = dword 'this could be used, doesn't seem to matter
GET rf, , dword: offset = dword 'find where the specific index BMP header is
'PRINT wide; "X"; high, size, offset

SEEK rf, 1 + offset + 14 'only read the BPP in BMP header
GET rf, , word: bpp = word
IF bpp = 0 THEN CLOSE rf: EXIT FUNCTION
IF bpp <= 24 THEN pixelbytes = bpp / 8 ELSE pixelbytes = 3
IF bpp > 1 AND bpp <= 8 THEN palettebytes = 4 * (2 ^ bpp) ELSE palettebytes = 0
datasize& = (wide * high * pixelbytes) + palettebytes 'no padder should be necessary
filesize& = datasize& + 14 + 40 '                      data and palette + header
bmpoffset& = palettebytes + 54 '                       data offset from start of bitmap
readbytes& = datasize& + 28 ' (40 - 12) bytes left to read in BMP header and XOR mask only
'PRINT bpp, bmpoffset&, filesize&

BM = CVI("BM") 'this will create "BM" in file like MKI$ would
wf = FREEFILE
OPEN fileout FOR BINARY AS wf
PUT wf, , BM
PUT wf, , filesize&
dword = 0
PUT wf, , dword
PUT wf, , bmpoffset& 'byte location of end of palette or BMP header
dword = 40
PUT wf, , dword '              start of 40 byte BMP header
PUT wf, , wide
PUT wf, , high
SEEK rf, 1 + offset + 12 '     after 12 bytes start copy of BMP header starting at planes
dat$ = STRING$(readbytes&, 0) 'create string to hold remaining bytes needed w/o AND mask data
GET rf, , dat$ '               copy lower header, palette(if used) and XOR mask
PUT wf, , dat$ '               put all of the string data in the bitmap all at once
CLOSE rf, wf
Icon2BMP = count '             return the number of icons available in the icon file
END FUNCTION
Code by Ted Weissgerber
Note: Once the file has been loaded into memory, the image handle can still be used even after the file has been deleted.


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link