To show the save file common dialog use the following code:
Option Explicit
Private Declare Function GetSaveFileNameA Lib "comdlg32.dll" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
'Purpose     :  Shows the save file common dialog
'Inputs      :  sTitle                  The title of the dialog
'               sFilter                 The filter list in the format:
'                                       Filter Caption & chr$(0) & Filter String & chr$(0)
'               sDefaultDir             The default path
'Outputs     :  Returns the specified file name and path
'Author      :  Andrew Baker
'Date        :  17/11/2000 11:22
'Notes       :  Adapted from code found on http://www.allapi.net
'Revisions   :
'Assumptions :
Function ShowSave(Optional sTitle = "Save File", Optional sFilter As String, Optional sDefaultDir As String) As String
Const clBufferLen As Long = 255
Dim OFName As OPENFILENAME, sBuffer As String * clBufferLen
On Error GoTo ExitFunction
OFName.lStructSize = Len(OFName)
OFName.hwndOwner = GetActiveWindow  'or Me.hwnd in VB
OFName.hInstance = 0                'or App.hInstance in VB
If Len(sFilter) Then
OFName.lpstrFilter = sFilter
Else
OFName.lpstrFilter = "Text Files (*.txt)" & Chr$(0) & "*.txt" & Chr$(0) & "All Files (*.*)" & Chr$(0) & "*.*" & Chr$(0)
End If
OFName.lpstrFile = sBuffer
OFName.nMaxFile = clBufferLen       'Set max number of characters
OFName.lpstrFileTitle = sBuffer
OFName.nMaxFileTitle = clBufferLen  'Set max number of characters
'Set the initial directory
If Len(Dir$(sDefaultDir)) Then
OFName.lpstrInitialDir = sDefaultDir
Else
OFName.lpstrInitialDir = CurDir$
End If
OFName.lpstrTitle = sTitle
OFName.flags = 0
'Show dialog
If GetSaveFileNameA(OFName) Then
ShowSave = Left$(OFName.lpstrFile, InStr(1, OFName.lpstrFile, Chr(0)) - 1)
Else
ShowSave = ""
End If
ExitFunction:
On Error Goto 0
End FunctionTo show the browse for folder common dialog use the following routine:
Private Type BrowseInfo
hWndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As String
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Private Declare Function GetActiveWindow Lib "user32" () As Long
'Purpose     :  Allows the user to select a folder
'Inputs      :  sCaption              The caption text on the dialog
'Outputs     :  Returns the select path
'Author      :  Andrew Baker
'Date        :  12/08/2000 13:06
'Notes       :  See http://www.mvps.org/vbnet/code/callback/browsecallback.htm
'               for code showing how to specify a initial path
'Revisions   :
Function BrowseForFolder(Optional sCaption As String = "Select a folder") As String
Const BIF_RETURNONLYFSDIRS = 1
Const MAX_PATH = 260
Dim lPos As Integer, lpIDList As Long, lResult As Long
Dim sPath As String, tBrowse As BrowseInfo
With tBrowse
'Set the owner window
.hWndOwner = GetActiveWindow        'Me.hWnd in VB
.lpszTitle = sCaption
.ulFlags = BIF_RETURNONLYFSDIRS     'Return only if the user selected a directory
End With
'Show the dialog
lpIDList = SHBrowseForFolder(tBrowse)
If lpIDList Then
sPath = String$(MAX_PATH, 0)
'Get the path from the IDList
SHGetPathFromIDList lpIDList, sPath
CoTaskMemFree lpIDList
lPos = InStr(sPath, vbNullChar)
If lPos Then
BrowseForFolder = Left$(sPath, lPos - 1)
If Right$(BrowseForFolder, 1) <> "\" Then
BrowseForFolder = BrowseForFolder & "\"
End If
End If
End If
End Function

解决方案 »

  1.   

    SHGetPathFromIDList
    Converts an item identifier list to a file system path. BOOL SHGetPathFromIDList(
        LPCITEMIDLIST pidl,
        LPSTR pszPath
    );Parameters
    pidl 
    Address of an item identifier list that specifies a file or directory location relative to the root of the namespace (the desktop). 
    pszPath 
    Address of a buffer to receive the file system path. This buffer must be at least MAX_PATH characters in size. 
    Return Values
    Returns TRUE if successful, or FALSE otherwise. ResIf the location specified by the pidl parameter is not part of the file system, this function will fail. Requirements 
      Version 4.00 and later of Shell32.dll  Windows NT/2000: Requires Windows NT 4.0 or later. 
      Windows 95/98: Requires Windows 95 or later. 
      Header: Declared in shlobj.h. 
      Import Library: shell32.lib.
    BROWSEINFO
    Contains parameters for the SHBrowseForFolder function and receives information about the folder selected by the user. typedef struct _browseinfo { 
        HWND hwndOwner; 
        LPCITEMIDLIST pidlRoot; 
        LPTSTR pszDisplayName; 
        LPCTSTR lpszTitle; 
        UINT ulFlags; 
        BFFCALLBACK lpfn; 
        LPARAM lParam; 
        int iImage; 
    } BROWSEINFO, *PBROWSEINFO, *LPBROWSEINFO; Members
    hwndOwner 
    Handle to the owner window for the dialog box. 
    pidlRoot 
    Pointer to an ITEMIDLIST structure (PIDL) specifying the location of the root folder from which to start browsing. Only the specified folder and any subfolders that are beneath it in the namespace hierarchy will appear in the dialog box. This member can be NULL; in that case, the namespace root (the desktop folder) is used. 
    pszDisplayName 
    Address of a buffer to receive the display name of the folder selected by the user. The size of this buffer is assumed to be MAX_PATH bytes. 
    lpszTitle 
    Address of a null-terminated string that is displayed above the tree view control in the dialog box. This string can be used to specify instructions to the user. 
    ulFlags 
    Flags specifying the options for the dialog box. This member can include zero or a combination of the following values: BIF_BROWSEFORCOMPUTER  Only return computers. If the user selects anything other than a computer, the OK button is grayed.  
    BIF_BROWSEFORPRINTER  Only return printers. If the user selects anything other than a printer, the OK button is grayed.  
    BIF_BROWSEINCLUDEFILES  Version 4.71. The browse dialog will display files as well as folders.  
    BIF_BROWSEINCLUDEURLS  Version 5.0. The browse dialog box can display URLs. The BIF_USENEWUI and BIF_BROWSEINCLUDEFILES flags must also be set. If these three flags are not set, the browser dialog box will reject URLs. Even when these flags are set, the browse dialog box will only display URLs if the folder that contains the selected item supports them. When the folder's IShellFolder::GetAttributesOf method is called to request the selected item's attributes, the folder must set the SFGAO_FOLDER attribute flag. Otherwise, the browse dialog box will not display the URL.  
    BIF_DONTGOBELOWDOMAIN  Do not include network folders below the domain level in the dialog box's tree view control.  
    BIF_EDITBOX  Version 4.71. Include an edit control in the browse dialog box that allows the user to type the name of an item.  
    BIF_NEWDIALOGSTYLE Version 5.0. Use the new user interface. Setting this flag provides the user with a larger dialog box that can be resized. The dialog box has several new capabilities including: drag and drop capability within the dialog box, reordering, context menus, new folders, delete, and other context menu commands. To use this flag, you must call OleInitialize or CoInitialize before calling SHBrowseForFolder. 
    BIF_RETURNFSANCESTORS  Only return file system ancestors. An ancestor is a subfolder that is beneath the root folder in the namespace hierarchy. If the user selects an ancestor of the root folder that is not part of the file system, the OK button is grayed. 
    BIF_RETURNONLYFSDIRS  Only return file system directories. If the user selects folders that are not part of the file system, the OK button is grayed.  
    BIF_SHAREABLE  Version 5.0. The browse dialog box can display shareable resources on remote systems. It is intended for applications that want to expose remote shares on a local system. The BIF_USENEWUI flag must also be set.  
    BIF_STATUSTEXT  Include a status area in the dialog box. The callback function can set the status text by sending messages to the dialog box.  
    BIF_USENEWUI Version 5.0. Use the new user interface, including an edit box. This flag is equivalent to BIF_EDITBOX | BIF_NEWDIALOGSTYLE. To use BIF_USENEWUI, you must call OleInitialize or CoInitialize before calling SHBrowseForFolder. 
    BIF_VALIDATE  Version 4.71. If the user types an invalid name into the edit box, the browse dialog will call the application's BrowseCallbackProc with the BFFM_VALIDATEFAILED message. This flag is ignored if BIF_EDITBOX is not specified. lpfn 
    Address of an application-defined function that the dialog box calls when an event occurs. For more information, see the BrowseCallbackProc function. This member can be NULL. 
    lParam 
    Application-defined value that the dialog box passes to the callback function, if one is specified. 
    iImage 
    Variable to receive the image associated with the selected folder. The image is specified as an index to the system image list. 
    Requirements 
      Version 4.00 and later of Shell32.dll  Windows NT/2000: Requires Windows NT 4.0 or later. 
      Windows 95/98: Requires Windows 95 or later. 
      Header: Declared in shlobj.h.
      

  2.   

    pidlRoot—从何根路径开始展开文件夹,缺省情况下从“桌面”开始展开。
      

  3.   

    肯定可以定位的
    微软的outlook express 的附件保存的目录就是上次打开过的目录
    而且我感觉好像很多都是这样的
      

  4.   

    设置回调函数
    接收到BFFM_INITIALIZED时发送BFFM_SETSELECTION设置路径
      

  5.   

    http://www.csdn.net/expert/topic/269/269966.xml?temp=.6397516回复人: playyuer(小干部) (  ) 信誉:100  2001-9-4 13:01:54  得分:50  
     
     
      'Objects: Form1、Command1、Module1
    'Form1:
    Option Explicit
    Private Const BIF_RETURNONLYFSDIRS = 1
    Private Const BIF_DONTGOBELOWDOMAIN = 2
    Private Const MAX_PATH = 260
    Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
    Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
    Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
    Private Declare Function LocalAlloc Lib "kernel32" (ByVal uFlags As Long, ByVal uBytes As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)
    Private Const LPTR = (&H0 Or &H40)
    Private Type BrowseInfo
            hWndOwner      As Long
            pIDLRoot      As Long
            pszDisplayName As Long
            lpszTitle      As Long
            ulFlags        As Long
            lpfnCallback  As Long
            lParam        As Long
            iImage        As Long
    End Type
    Private Function MyAddressOf(AddressOfX As Long) As Long
    MyAddressOf = AddressOfX
    End FunctionPrivate Sub Command1_Click()
    Dim lpIDList As Long
    Dim sBuffer As String
    Dim szTitle As String
    Dim tBrowseInfo As BrowseInfo
    Dim Ret As Long
    szTitle = "This is the title"
    Dim sPath As String
    sPath = VBA.InputBox("初始路径:", , "C:\program files")
    With tBrowseInfo
        .hWndOwner = Me.hWnd
        .lpszTitle = lstrcat(szTitle, "")
        .ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN
        .lpfnCallback = MyAddressOf(AddressOf BrowseForFolders_CallbackProc)
        Ret = LocalAlloc(LPTR, VBA.Len(sPath) + 1)
        CopyMemory ByVal Ret, ByVal sPath, VBA.Len(sPath) + 1
        .lParam = Ret
    End With
    lpIDList = SHBrowseForFolder(tBrowseInfo)
    If (lpIDList) Then
      sBuffer = VBA.Space(MAX_PATH)
      SHGetPathFromIDList lpIDList, sBuffer
      sBuffer = VBA.Left(sBuffer, VBA.InStr(sBuffer, vbNullChar) - 1)
      MsgBox sBuffer
      End If
    End Sub'Module1:
    Option Explicit
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Const WM_USER = &H400
    Private Const BFFM_SETSELECTIONA As Long = (WM_USER + 102)
    Private Const BFFM_SETSELECTIONW As Long = (WM_USER + 103)
    Private Const BFFM_INITIALIZED As Long = 1
    Public Function BrowseForFolders_CallbackProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal lParam As Long, ByVal lpData As Long) As Long
    If uMsg = BFFM_INITIALIZED Then
      SendMessage hWnd, BFFM_SETSELECTIONA, True, ByVal lpData
    End If
    End Function
      

  6.   

    主要是设置两个参数
        .lpfn = AddressOf CallbackProcStr ' Dialog callback function that preselectes the folder specified
        selectedPathPointer = LocalAlloc(LPTR, Len(selectedPath) + 1)   '分配一个字符串
        CopyMemory ByVal selectedPathPointer, ByVal selectedPath, Len(selectedPath) + 1  'Copy the path to the string
        .lParam = selectedPathPointer   'selectedPath 就是要选定的路径Public Function CallbackProcStr(ByVal hWnd As Long, ByVal uMsg As Long, ByVal lParam As Long, ByVal lpData As Long) As Long
    If uMsg = 1 Then
        Call SendMessage(hWnd, BFFM_SETSELECTIONA, True, ByVal lpData)
    End If
    End Function