Silverfrost Logo About Us | Contact Us
 

Using Windows API structures from Fortran

The difficulty of accessing Windows API structures from Fortran can be alleviated by the use of Fortran statement functions and Fortran EQUIVALENCE statements.

For example, suppose the text metrics for the system font are required in order to determine the size of a main window before it is created. This requires finding the height of a single line of text. The Windows API function GetTextMetrics returns the metric properties of the system font in a structure, LPTEXTMETRICS. The specific fields of interest are tmHeight and tmExternalLeading.

Examining the windows.h file, it will be seen that the structure length is 29 bytes. In order to represent this in Fortran, any array may be used, provided its length is at least 29 bytes. In the interests of efficiency it is a good idea to keep 4-byte alignment, so an array 32 bytes long might be used to hold the structure. After the call to GetTextMetrics, the relevant field values may be extracted by use of statement functions, using the address of the structure as the argument. For example:

INTEGER*1 TEXTMETRICS(32)
INTEGER*2 line_hght,tmHeight,tmExternalLeading
INTEGER*4 atm
  . . .
tmHeight(i)=CORE2(i+0)
tmExternalLeading(i)=CORE2(i+8)
. . .
got_tm=GetTextMetrics(hwnd,TEXTMETRICS)
atm=LOC(TEXTMETRICS)
line_hght=tmHeight(atm)+tmExternalLeading(atm)

CORE2 and LOC are Silverfrost Fortran intrinsic functions.

The use of EQUIVALENCE statements is illustrated in Edit box (%eb).

 

 

Copyright © 1999-2017 Silverfrost Limited