non ActX com dlg GetOpenFilename

I want to use the common dialog box GetOpenFileName.  It will not be possible to distribute the ActiveX version, so I have to use the api calls (I want to do it this way, and I'm forced to.  COmpany rules: No ActiveX distributions AT ALL, etc).  

I have no problem calling up the dialog box, but it isn't centered.  How can I center it?  I tried writing a hook and using OFN_EABLEHOOK, but the best that happened is that I could change the height of the box, but not move it (this happend with the MoveWindow api call).

I have successfully centered the browse for folder dialog (shell32) via it's callback to start at a preselected path, but the GetOpenFilename Dialog box doesn't seem to work the same.

(I'm using VB 5)

Any help?
Who is Participating?

Improve company productivity with a Business Account.Sign Up

muffinthedogConnect With a Mentor Commented:
    I have old code that seems to do what you are asking... Don't really care to clean it all up but you can use the MoveWindow function inside of the hook procedure on the CDN_INITDONE message as follows... Think you will understand, if you need the code cleaned or need to center the dialog or such, let me know and I will add or delete accordingly... Old code used and have modified it to meet particular needs as they arise. Call like GetFile(me.hwnd) Search for MoveWindow and change the x, y, width and height parameters as needed.
    Just trying to help

Option Explicit

' OPENFILENAME constants
Private Const OFN_READONLY             As Long = &H1
Private Const OFN_OVERWRITEPROMPT      As Long = &H2
Private Const OFN_HIDEREADONLY         As Long = &H4
Private Const OFN_NOCHANGEDIR          As Long = &H8
Private Const OFN_SHOWHELP             As Long = &H10
Private Const OFN_ENABLEHOOK           As Long = &H20
Private Const OFN_ENABLETEMPLATE       As Long = &H40
Private Const OFN_NOVALIDATE           As Long = &H100
Private Const OFN_ALLOWMULTISELECT     As Long = &H200
Private Const OFN_EXTENSIONDIFFERENT   As Long = &H400
Private Const OFN_PATHMUSTEXIST        As Long = &H800
Private Const OFN_FILEMUSTEXIST        As Long = &H1000
Private Const OFN_CREATEPROMPT         As Long = &H2000
Private Const OFN_SHAREAWARE           As Long = &H4000
Private Const OFN_NOREADONLYRETURN     As Long = &H8000
Private Const OFN_NOTESTFILECREATE     As Long = &H10000
Private Const OFN_NONETWORKBUTTON      As Long = &H20000
Private Const OFN_NOLONGNAMES          As Long = &H40000
Private Const OFN_EXPLORER             As Long = &H80000
Private Const OFN_NODEREFERENCELINKS   As Long = &H100000
Private Const OFN_LONGNAMES            As Long = &H200000

'Dialog box ids
Private Const cmb2     As Long = &H471  'Drop-down combo box that displays the current drive or folder, and that allows the user to select a drive or folder to open
Private Const stc4     As Long = &H443  'Label for the cmb2 combo box
Private Const lst1     As Long = &H460  'List box that displays the contents of the current drive or folder
Private Const stc1     As Long = &H440  'Label for the lst1 list box
Private Const edt1     As Long = &H480  'Edit control that displays the name of the current file, or in which the user can type the name of the file to open
Private Const stc3     As Long = &H442  'Label for the edt1 edit control
Private Const cmb1     As Long = &H470  'Drop-down combo box that displays the list of file type filters
Private Const stc2     As Long = &H441  'Label for the cmb1 combo box
Private Const chx1     As Long = &H410  'The read-only check box
Private Const IDOK     As Long = &H1    'The OK command button (push button)
Private Const IDCANCEL As Long = &H2    'The Cancel command button (push button)
Private Const pshHelp As Long = &H40E   'The Help command button (push button)
'Message constants
Private Const WM_NOTIFY     As Long = &H4E
Private Const WM_GETDLGCODE As Long = &H87

' callback notification messages
Private Const CDN_FILEOK         As Long = -606
Private Const CDN_FOLDERCHANGE   As Long = -603
Private Const CDN_HELP           As Long = -605
Private Const CDN_INITDONE       As Long = -601
Private Const CDN_SELCHANGE      As Long = -602
Private Const CDN_SHAREVIOLATION As Long = -604
Private Const CDN_TYPECHANGE     As Long = -607

' callback broadcast messages
Private Const CDM_GETFILEPATH     As Long = &H465
Private Const CDM_GETFOLDERIDLIST As Long = &H467
Private Const CDM_GETFOLDERPATH   As Long = &H466
Private Const CDM_GETSPEC         As Long = &H464
Private Const CDM_HIDECONTROL     As Long = &H469
Private Const CDM_SETCONTROLTEXT  As Long = &H468
Private Const CDM_SETDEFEXT       As Long = &H46A

' constants for findfile
Private Const INVALID_HANDLE_VALUE     As Long = -1
Private Const MAX_PATH                 As Long = 260

    lStructSize As Long             'size of OPENFILENAME
    hWndOwner As Long               'owner window
    hInstance As Long               'dialog box template if OFN_ENABLETEMPLATEHANDLE flag
                                    'or module handle of pTemplateName if OFN_ENABLETEMPLATE flag
    lpstrFilter As String           'filter strings
    lpstrCustomFilter As String     'buffer for user filters
    nMaxCustFilter As Long          'size of lpstrCustomFilter
    nFilterIndex As Long            'index of filter or zero for custom filter
    lpstrFile As String             'initialization file and selection return
    nMaxFile As Long                'size of lpstrFile
    lpstrFileTitle As String        'filename and extension of selection
    nMaxFileTitle As Long           'size of lpstrFileTitle
    lpstrInitialDir As String       'initial directory
    lpstrTitle As String            'dialog title
    flags As Long                   'OFN_ALLOWMULTISELECT
    nFileOffset As Integer          '0 based offset of filename.ext in lpstrFile
    nFileExtension As Integer       '0 based offset of extension in lpstrFile
    lpstrDefExt As String           'default file name extension
    lCustData As Long               'data to send to the hook procedure
    lpfnHook As Long                'hook procedure
    lpTemplateName As String        'dialog template name if OFN_ENABLETEMPLATE
End Type

'OFNOTIFY header
    hwndFrom As Long
    idfrom As Long
    code As Long
End Type

Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (lpvDest As Any, ByVal lpvSource As Any, ByVal cbCopy As Long)
Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long

'GetProc(proc As Long) As Long
'VB hack to pass a function pointer to a variable
'   proc - address of function
'   proc
Function GetProc(proc As Long) As Long
    GetProc = proc
End Function

'GetFile(hWndParent as Long) As String
'Implements GetOpenFileName
'   hWndParent - handle of parent window
'   name of file selected in open common dialog
Function GetFile(hWndParent As Long) As String
    Dim ofn As OPENFILENAME         'structure for GetOpenFileName
    Dim sFile As String * 4096      'fully qualified path and filename of selection
    Dim sFileTitle As String * 256  'file name

    'initialize the OPENFILENAME structure
    ofn.hWndOwner = hWndParent
    ofn.lpstrFilter = "Kodak CD images (*.pcd)" & Chr(0) & "*.pcd" & Chr(0) & Chr(0)
    ofn.lpstrFile = sFile
    ofn.nMaxFile = 4096
    ofn.lpstrFileTitle = sFileTitle
    ofn.nMaxFileTitle = 256
    ofn.lpstrTitle = "Select Kodak CD file"
    ofn.lpstrDefExt = "txt"
    ofn.lCustData = &H0
    ofn.lpfnHook = GetProc(AddressOf OFNHookProcedure)
    ofn.lStructSize = Len(ofn)
    'do the file open dialog
    GetOpenFileName ofn
    'return the fully qualified path and file name of the selection
    GetFile = Left(ofn.lpstrFile, InStr(ofn.lpstrFile, Chr(0)) - 1)
End Function

'OFNHookProcedure(ByVal hdlg As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) as Long
'Callback function for GetOpenFileName
'   hdlg   - handle of the templated dialog
'   uMsg   - message
'   wParam - word parameter
'   lParam - long parameter
'   0 or message specific value if message handled within procedure
Function OFNHookProcedure(ByVal hdlg As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Dim hWndParent As Long          'handle of the open dialog
    Dim lResult As Long             'result of api call
    Dim sBuffer As String * 4096    'buffer for file selected (full path and file name)
    Dim sFile As String             'file name selected (full path and file name)
    Dim hdr As NMHDR                'OFNOTIFY header
    'get handle to the openfile dialog
    hWndParent = GetParent(hdlg)
    'switch on notify message
    If uMsg = WM_NOTIFY Then
        'get the message in hdr.code of OFNOTIFY structure pointed to by lParam
        CopyMemory hdr, lParam, Len(hdr)
        Select Case hdr.code
        'Case CDN_FILEOK         'user clicked OK
        'Case CDN_HELP          'help button not active
        Case CDN_INITDONE       'open dialog finished initialization not used
            lResult = MoveWindow(hWndParent, 100, 100, 100, 100, 1)
        Case CDN_FOLDERCHANGE   'user selected new folder
            'set selected file name to null and clear picture
            lResult = SendMessage(hWndParent, CDM_SETCONTROLTEXT, edt1, vbNullString)
            'ImageInput.pbFile.Picture = LoadPicture("")
        Case CDN_SELCHANGE      'user selected new file or folder from file list
            'get the file or folder selected
            lResult = SendMessage(hWndParent, CDM_GETFILEPATH, 4096, sBuffer)
            sFile = Left(sBuffer, InStr(sBuffer, Chr(0)) - 1)
            'If IsFile(sFile) Then
                'show picture here
                'ImageInput.pbFile.Picture = LoadPicture("C:\Program Files\DevStudio\VB\Projects\Image\interior.bmp")
               'set selected file name to null and clear picture
                'lResult = SendMessage(hWndParent, CDM_SETCONTROLTEXT, edt1, vbNullString)
                'ImageInput.pbFile.Picture = LoadPicture("")
            'End If
        'Case CDN_SHAREVIOLATION 'share violation on chosen file not used
        'Case CDN_TYPECHANGE     'user changed file type not used
        End Select
    End If
    OFNHookProcedure = 0
End Function

'IsFile(sFile As String) As Boolean
'Function returns boolean indicating if path passed is a file
'   sFile  - File system path
'   True  - File system path passed is a file
'   False - File system path passed is not a file

I have full code to do that. Give me your e-mail, I will send to you. In the mean time, I will try to extract all the needed code and post it here.
I have send to you the files.
obregoruAuthor Commented:
What I'm trying to do is to use comdlg32.dll and user32.dll *only*.  I got it to work if I don't use the new explorer look. Here is the code:


If I remove OFN_EXPLORER, I can move and resize it *BUT* it is the old Win 3.x GetOpenFilename shell (short filenames, etc.).

The minute I use OFN_EXPLORER, I can't move it.  (On a few occasions, the size did change, but there was no movement!!!)

Here are the values for the constants above:

lobal Const OFN_EXPLORER = &H80000                     ' new look commdlg
Global Const OFN_LONGNAMES = &H200000                    ' force long names for 3.x modules
Global Const OFN_ENABLEINCLUDENOTIFY = &H400000          ' send include message to callback
Global Const OFN_ENABLESIZING = &H800000

Any idea?
obregoruAuthor Commented:

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.