getByte( ) example
The GetClientRect( ) API function returns the coordinates of a window’s client rectangle (the area inside the window borders) in a structure made up of four long integers: left, top, right, and bottom. A long integer is 32 bits, or 4 bytes. Therefore the rectangle structure is 16 bytes long. In a dBL string, each character is two bytes, so a string must be 8 characters long to store the rectangle structure.
The client rectangle coordinates are relative to the window’s client area, so the left and top are always zero; the right and bottom coordinates contain the width and height of the client area. The following example displays the width of the client area of a default form in pixels:
if type("GetClientRect") # "FP"
extern CLOGICAL GetClientRect(CHANDLE, CPTR) USER32
endif
c = space(8) // 16-byte structure
f = new Form()
f.open() // Form must be open to have valid hWnd
if GetClientRect(f.hWnd, c)
n = c.getByte(8) + ;
bitlshift(c.getByte(9), 8) + ;
bitlshift(c.getByte(10), 16) + ;
bitlshift(c.getByte(11), 24)
if bitset(n, 31)
n := - bitnot(n)
endif
? n
endif
f.close()
The SPACE( ) function is used to create a string of the desired length. The entire structure will be overwritten by the function, so it doesn’t matter that the string initially contains spaces.
After the function call, getByte( ) is used to extract the coordinate, byte-by-byte. The right coordinate starts at offset 8 (the left starts at offset 0, the top at offset 4, and the bottom at offset 12) with the low byte first. The second byte is shifted 8 bits to the left and added to the first byte. Only the first two bytes are used, because the width will always be well under 64 K pixels, the maximum number that can be represented by two bytes (16 bits).