Announcement

Collapse
No announcement yet.

How to center OpenFileDialog

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to center OpenFileDialog

    Hello fellow programmers,

    I've a program using an open file dialog (GetOpenFileName). By default it is positioned in the upper left corner of the screen (the program does not have a main window).
    Does anyone know how to center this dialog. My main problem is that I don't know how to 'catch' a handle for it.

    Thanks in advance

    ------------------
    mailto:[email protected][email protected]</A>
    www.basicguru.com/zijlema/

    Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
    http://zijlema.basicguru.eu
    *** Opinions expressed here are not necessarily untrue ***

  • #2
    Egbert, Semen probably knows some funky way of hooking it with a DLL, but you could also try FindWindow() if you know a part of the window title?
    Best of luck,
    Wayne


    ------------------
    -

    Comment


    • #3
      Egbert;

      Edwin posted the origional hook procedure about 18 months ago, Borje also posted a
      revised version of it recently, and added/fixed a few things. I have Edwin's
      origional hook code, but probably a quick search for Borje's version will
      yeild good/quick results to get you back on track. If you have any of Borje's
      kewl examples that use that dialog, then you have it already.

      Regards,
      Jules

      Comment


      • #4
        Not tried, but...

        Instead of writing hooks, etc, what if you created a transparent or invisible window to be the parent window of the GetOpenFilename dialog?

        You'd have handles and could MoveWindow, etc...

        MCM
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          Hi Egbert,

          I use this -

          Code:
          '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          function CentreHook ( byval hDlgChild as long, byval wMsg as long, _
                                byval wParam as long, byval lParam as long ) as long
          
            'Purpose : Callback function ( hook ) for common dialog, which we use to centre the dialog
            '
            'Remarks : Curiously, this procedure receives hDlgChild, the handle of 'a child dialog'
            '          of the File Open / Save dialog that we are interested in. By contrast,
            '          a Print hook function receives the handle of the common dialog itself. See MSDN,
            '          search on "Hook procedures for common dialog boxes"
            '
            local rDialog as RECT
            local mTop    as long
            local mLeft   as long
          
            'FWIW, this message is unique in that it is always called *after* the default handler...
            if wMsg = %WM_INITDIALOG then
          
              GetWindowRect GetParent(hDlgChild), rDialog
              GetCentreCoords mTop, mLeft, rDialog.nBottom - rDialog.nTop, rDialog.nRight - rDialog.nLeft
              SetWindowPos GetParent(hDlgChild), %NULL, mLeft, mTop, 0, 0, %SWP_NOSIZE OR %SWP_NOZORDER
              function = %TRUE                                            'message was handled
          
            else                                                          'pass message to default handler
              function = %FALSE
            end if
          
          end function
          '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          When preparing to invoke the Open File dialog, just add this to the OPENFILENAME structure -
          Code:
            uOFN.Flags    = uOFN.Flags or %OFN_ENABLEHOOK
            uOFN.lpfnHook = codeptr( CentreHook )
          Then, when you make the call, the dialog will be centered. You can see it working in Lynx .

          Regards,

          Paul


          ------------------
          http://www.zippety.net
          mailto[email protected][email protected]</A>
          Zippety Software, Home of the Lynx Project Explorer
          http://www.zippety.net
          My e-mail

          Comment


          • #6
            Thank you, guys!

            Borje's code looks very complicated to me.
            Therefore, I tried Michael's simple idea first. It works fine for me.
            Tonight I'll give Paul's code also a try. It looks lots simpler than Borje's, anyhow.


            ------------------
            mailto:[email protected][email protected]</A>
            www.basicguru.com/zijlema/

            Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
            http://zijlema.basicguru.eu
            *** Opinions expressed here are not necessarily untrue ***

            Comment


            • #7
              hooked procedure gives more power. allows one to do real "professional" stunts
              and make our pb applications stand out among all vb/delphi cra.. sorry, stuff.

              can be a bit more complicated yes, but not that much. don't know what code you saw,
              but just in case: http://www.powerbasic.com/support/pb...ad.php?t=23142


              ------------------

              Comment


              • #8
                egbert -
                there is a lot of ways.

                for example, cbt-hook (similar for msgbox)

                Code:
                   #compile exe
                   #dim all
                   #register none
                   #include "win32api.inc"
                   #include "comdlg32.inc"
                
                   global hhook as long
                
                   function sbproc(byval lmsg as long, byval wparam as long, _
                      byval lparam as long) as long
                      if lmsg = %hcbt_activate then
                         dim rc1 as rect, rc2 as rect
                         getwindowrect getdesktopwindow, rc1: getwindowrect wparam, rc2
                         setwindowpos wparam, %hwnd_topmost, (rc1.nleft + rc1.nright - rc2.nright + rc2.nleft) / 2 , _
                            (rc1.ntop + rc1.nbottom - rc2.nbottom + rc2.ntop) / 2, 0, 0, %swp_nosize or %swp_noactivate
                         unhookwindowshookex hhook
                      end if
                   end function
                
                   function pbmain
                      local ofn as openfilename, sztitlename as asciiz * %max_path, szfilter as asciiz * 100
                      
                      hhook = setwindowshookex(%wh_cbt, codeptr(sbproc), getmodulehandle(byval 0&), _
                               getcurrentthreadid)
                                 
                      ofn.lstructsize = sizeof(ofn)
                      szfilter = "all files (*.*)" + chr$(0) + "*.*" + chr$(0) + _
                        "text files (*.txt)" + chr$(0) + "*.txt" + chr$(0)
                      ofn.lpstrfilter = varptr(szfilter)
                      ofn.lpstrfiletitle = varptr(sztitlename)
                      ofn.nmaxfiletitle = sizeof(sztitlename)
                      ofn.flags = %ofn_hidereadonly or %ofn_createprompt or %ofn_explorer
                      
                      getopenfilename ofn
                      if sztitlename <> " then msgbox curdir$ + sztitlename      
                   
                   end function
                another ... using hookproc of ofn: http://www.powerbasic.com/support/pb...ad.php?t=22708




                ------------------
                e-mail: [email protected]

                Comment


                • #9
                  Oops...,

                  After all, Michael's idea is not a very good one. It looks (and is) simple indeed, but it only works if you know how to create a parent window that is about the size of the open file dialog. I mean in all circumstances, i.e.: on different unknown (i.e. your customer's) computers with different system metrics etcetera.
                  Why should both, the so called main dialog and the ofn-dialog, have approx. the same size? An open file dialog appears in the upper left corner of its parent window. So, if your invisible main window is significantly larger than its child, the ofn-dialog will appear somewhere in the left upper region of the screen.
                  And if the main window is too tiny, the opposite effect will be the case: the ofn-dialog will appear somewhere in the lower right part of the screen.

                  In the meantime, I 've seen Semen's addition to this discussion. I'll give it a try to-morrow.

                  Thanks for all your contributions

                  ------------------
                  mailto:[email protected][email protected]</A>
                  www.basicguru.com/zijlema/

                  Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
                  http://zijlema.basicguru.eu
                  *** Opinions expressed here are not necessarily untrue ***

                  Comment


                  • #10
                    Originally posted by Wayne Diamond:
                    Egbert, Semen probably knows some funky way of hooking it with a DLL, but you could also try FindWindow()
                    Originally posted by Semen Matusovski:
                    Egbert - There is a lot of ways.
                    For example, CBT-hook
                    Ok Semen, now you're starting to freak me out

                    ------------------
                    -

                    Comment


                    • #11
                      Well, I've definitely decided to use Semen's CBT-hook method. It's simple.
                      However, I don't exactly understand what happens 'under water'. The Microsoft
                      help file is 'as clear as mud', so maybe Semen is willing to give some additional info here?

                      Thanks very much for your various masterpieces, Semen!

                      ------------------
                      mailto:[email protected][email protected]</A>
                      www.basicguru.com/zijlema/

                      Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
                      http://zijlema.basicguru.eu
                      *** Opinions expressed here are not necessarily untrue ***

                      Comment

                      Working...
                      X