WIDTH (function): Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
Tag: Manual revert
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 11: Line 11:
* To get the width of the current program [[SCREEN|screen]] window use zero for the handle value or nothing: {{Parameter|columns&}} = [[_WIDTH (function)|_WIDTH]](0) ''or'' {{Parameter|columns&}} = [[_WIDTH (function)|_WIDTH]]
* To get the width of the current program [[SCREEN|screen]] window use zero for the handle value or nothing: {{Parameter|columns&}} = [[_WIDTH (function)|_WIDTH]](0) ''or'' {{Parameter|columns&}} = [[_WIDTH (function)|_WIDTH]]
* If the image specified by {{Parameter|imageHandle&}} is in text only([[SCREEN]] 0) mode, the number of characters per row is returned.
* If the image specified by {{Parameter|imageHandle&}} is in text only([[SCREEN]] 0) mode, the number of characters per row is returned.
* If the image specified by {{Parameter|imageHandle&}} is in graphics mode, the number of pixels per row is returned.  
* If the image specified by {{Parameter|imageHandle&}} is in graphics mode, the number of pixels per row is returned.
* If {{Parameter|imageHandle&}} is an invalid handle, then an [[ERROR Codes|invalid handle error]] is returned.
* If {{Parameter|imageHandle&}} is an invalid handle, then an [[ERROR Codes|invalid handle error]] is returned.
* The last visible pixel coordinate of a program [[SCREEN|screen]] is '''[[_WIDTH (function)|_WIDTH]] - 1'''.
* The last visible pixel coordinate of a program [[SCREEN|screen]] is '''[[_WIDTH (function)|_WIDTH]] - 1'''.
Line 17: Line 17:


{{PageExamples}}
{{PageExamples}}
''Example:'' A SUB program that centers text in any graphic screen mode except text mode [[SCREEN (statement)|SCREEN]] 0.
''Example:'' A SUB program that centers text in any graphic screen mode except text mode [[SCREEN]] 0.


{{CodeStart}}
{{CodeStart}}
Line 25: Line 25:
  Align 15, 5, s&, "This text is centered on the screen!"
  Align 15, 5, s&, "This text is centered on the screen!"


  SUB Align (Tcolor, Trow, mode&, txt$)  
  SUB Align (Tcolor, Trow, mode&, txt$)
   center& = {{Cl|_WIDTH (function)|_WIDTH}} (mode&) \ 2    'returns pixels in graphic modes  
   center& = {{Cl|_WIDTH (function)|_WIDTH}} (mode&) \ 2    'returns pixels in graphic modes
   MaxCol = (center& \ 8) + 1              'screen text width = 8 pixels
   MaxCol = (center& \ 8) + 1              'screen text width = 8 pixels
   Tcol = MaxCol - ({{Cl|LEN}}(txt$) \ 2)
   Tcol = MaxCol - ({{Cl|LEN}}(txt$) \ 2)
   {{Cl|COLOR}} Tcolor: {{Cl|LOCATE}} Trow, Tcol: {{Cl|PRINT}} txt$;
   {{Cl|COLOR}} Tcolor: {{Cl|LOCATE}} Trow, Tcol: {{Cl|PRINT}} txt$;
  END SUB
  END SUB


Line 38: Line 38:


: ''Note:'' The screen handle parameter is required because using no handle could assume other page handles created by functions like [[_NEWIMAGE]] or [[_PUTIMAGE]]. Use the correct handle in the SUB call! When using SCREEN 0, the MaxCol variable is not needed because _WIDTH returns the number of text columns, not pixels. Use the center value and add 1. '''Tcol = (center& + 1) - LEN(txt$) \ 2'''
: ''Note:'' The screen handle parameter is required because using no handle could assume other page handles created by functions like [[_NEWIMAGE]] or [[_PUTIMAGE]]. Use the correct handle in the SUB call! When using SCREEN 0, the MaxCol variable is not needed because _WIDTH returns the number of text columns, not pixels. Use the center value and add 1. '''Tcol = (center& + 1) - LEN(txt$) \ 2'''





Latest revision as of 01:21, 29 January 2023

The _WIDTH function returns the width of an image handle or of the current write page.


Syntax

columns& = _WIDTH[(imageHandle&)]


Description

  • If imageHandle& is omitted, it's assumed to be the handle of the current SCREEN or write page.
  • To get the width of the current program screen window use zero for the handle value or nothing: columns& = _WIDTH(0) or columns& = _WIDTH
  • If the image specified by imageHandle& is in text only(SCREEN 0) mode, the number of characters per row is returned.
  • If the image specified by imageHandle& is in graphics mode, the number of pixels per row is returned.
  • If imageHandle& is an invalid handle, then an invalid handle error is returned.
  • The last visible pixel coordinate of a program screen is _WIDTH - 1.


Examples

Example: A SUB program that centers text in any graphic screen mode except text mode SCREEN 0.


 s& = _NEWIMAGE(800, 600, 256)
 SCREEN s&
 Align 15, 5, s&, "This text is centered on the screen!"

 SUB Align (Tcolor, Trow, mode&, txt$)
   center& = _WIDTH (mode&) \ 2     'returns pixels in graphic modes
   MaxCol = (center& \ 8) + 1              'screen text width = 8 pixels
   Tcol = MaxCol - (LEN(txt$) \ 2)
   COLOR Tcolor: LOCATE Trow, Tcol: PRINT txt$;
 END SUB

Explanation: _NEWIMAGE enlarges a screen to 800 pixels wide which is what _WIDTH function will return. The center is 800 \ 2 or 400. Since the text width is 8 pixels, that is divided by 8 to get 50 as the center text column. Then half of the text length is subtracted to find the starting text print LOCATE column.


Note: The screen handle parameter is required because using no handle could assume other page handles created by functions like _NEWIMAGE or _PUTIMAGE. Use the correct handle in the SUB call! When using SCREEN 0, the MaxCol variable is not needed because _WIDTH returns the number of text columns, not pixels. Use the center value and add 1. Tcol = (center& + 1) - LEN(txt$) \ 2


See also



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