Setting the position of the File Open dialogue

by Richard Russell, February 2016

The File Open (and File Save) dialogues open, by default, at a position on the screen determined by Windows. Whilst this will almost always be satisfactory, there may very occasionally be a requirement to position the dialogue box at specific coordinates. This may be achieved using the code below:

        INSTALL @lib$+"CALLBACK" : REM Must be version 2.0 or later
 
        SWP_NOSIZE = &1
        SWP_NOZORDER = &4
        WM_INITDIALOG = &110
        OFN_ENABLEHOOK = &20
        OFN_EXPLORER = &80000
 
        xOFN% = 400
        yOFN% = 100
 
        DIM fs{lStructSize%, hwndOwner%, hInstance%, lpstrFilter%, \
        \      lpstrCustomFilter%, nMaxCustFilter%, nFilterIndex%, \
        \      lpstrFile%, nMaxFile%, lpstrFileTitle%, \
        \      nMaxFileTitle%, lpstrInitialDir%, lpstrTitle%, \
        \      flags%, nFileOffset{l&,h&}, nFileExtension{l&,h&}, \
        \      lpstrDefExt%, lCustData%, lpfnHook%, lpTemplateName%, \
        \      pvReserved%, dwReserved%, FlagsEx%}
        DIM fp{t&(260)}
 
        ff$ = "All files"+CHR$0+"*.*"+CHR$0+CHR$0
        fs.lStructSize% = DIM(fs{})
        fs.hwndOwner% = @hwnd%
        fs.lpstrFilter% = !^ff$
        fs.lpstrFile% = fp{}
        fs.nMaxFile% = 260
        fs.flags% = OFN_EXPLORER OR OFN_ENABLEHOOK
        fs.lpfnHook% = FN_callback(FN_OFNHookProc(), 4)
 
        SYS FN_syscalls("GetOpenFileName"), fs{} TO !FN_systo(result%)
        IF result% THEN
          filename$ = $$fp{}
          PRINT filename$
        ENDIF
        END
 
        DEF FN_OFNHookProc(hdlg%, uiMsg%, wParam%, lParam%)
        IF uiMsg% <> WM_INITDIALOG THEN = 0
        SYS "GetParent", hdlg% TO hdlg%
        SYS "SetWindowPos",hdlg%,0,xOFN%,yOFN%,0,0,SWP_NOSIZE OR SWP_NOZORDER
        = 0

The xOFN% and yOFN% global variables determine the position at which the dialogue box opens.

Unfortunately a side-effect is that the appearance of the dialogue will change from the 'modern' style to the 'classic' style, because Windows assumes that the HookProc is possibly being used to customise the layout.