Thursday, June 30, 2016

GETCLIENTRECT - Gettting the dimensions of the inside of a window

Sometimes it comes in really handy to know exactly what the limits of the inside of a window are.  For example, if your Liberty BASIC program opens a window that is 500x400 pixels and you want to draw graphics that fit precisely or you are creating a game.. Think of Space Invaders where the aliens march back and forth on the screen!  You need to know the precise inner dimensions of the window so you need to know how thick the window frame is and also the title bar on top of the window.

The trouble is, these measurements are not going to be the same from one version of Windows to another, and they change if you modify your system font sizes.

So what to do?  Windows provides us a way to get the dimensions of the inside of the window by using the GETWINRECT function.

    WindowWidth = 500
    WindowHeight = 400
    open "get client rectangle" for window as #w

    'Get the window handle
    hndl = hwnd(#w)

    'Declare the struct which will be used to get the window client rectangle
    struct winRect, orgX as long, orgY as long, cornerX as long, cornerY as long

    'Make the GetClientRect call
    calldll #user32, "GetClientRect", hndl as ulong, winRect as struct, result as Boolean

    'Grab the width and height from the struct
    wide=winRect.cornerX.struct-winRect.orgX.struct
    high=winRect.cornerY.struct-winRect.orgY.struct

    notice "inside of window frame width = "; wide; " height = "; high