DIALOG type windows are positioned wrong

From Liberty BASIC Family
Jump to navigation Jump to search

Description

UpperLeftX and UpperLeftY for dialog type windows, except dialog_modal, are in screen pixels instead of dialog base units, which results in a wrong position, if a different screen DPI setting is used than 96DPI.

Example code to demonstrate the bug.


    nomainwin

    UpperLeftX = int((DisplayWidth-WindowWidth)/2)
    UpperLeftY = int((DisplayHeight-WindowHeight)/2)

    open "I am centered" for dialog as #m
    #m "trapclose [quit]"
    wait

[quit]
    close #m
    end

Example code to work around the bug.

Using a style bit to center the dialog


    nomainwin

    stylebits #m, _DS_CENTER, 0, 0, 0
    open "I am centered" for dialog as #m
    #m "trapclose [quit]"
    wait

[quit]
    close #m
    end

Applying the Screen Scale (usable for any position)


    nomainwin

    UpperLeftX = int((DisplayWidth-WindowWidth)/2)/GetScreenScale()
    UpperLeftY = int((DisplayHeight-WindowHeight)/2)/GetScreenScale()

    open "I am centered" for dialog as #m
    #m "trapclose [quit]"
    wait

[quit]
    close #m
    end

function GetScreenScale()
    calldll #user32, "GetDC",_
        0   as ulong,_   ' entire screen
        hDC as ulong

    nIndex = _LOGPIXELSX

    calldll #gdi32, "GetDeviceCaps",_
        hDC    as ulong,_
        nIndex as ulong,_
        dpi    as ulong

    calldll #user32, "ReleaseDC",_
        0      as ulong,_   ' entire screen
        hDC    as ulong,_
        result as ulong     ' 1 = success

    GetScreenScale = dpi / 96
end function