' save dialog
'   创建并显示会保存文件通用对话框，允许用户选择的文件名和目录来保存该文件。
'用 AfxSaveFileDialog 替代这个
'FF_SaveFileDialog( HWND_FORM5, "保存的文件", b, "", "所有的文件 (*.*)|*.*", "",  %OFN_HIDEREADONLY Or %OFN_OVERWRITEPROMPT Or %OFN_EXPLORER, %TRUE )
'默认文件夹最后不能带 \ 字符
'   hWnd:         （如果有） 的父窗口的句柄
'   Caption:      标题将显示为对话框的标题。
'   FileSpec:     要显示的默认文件名。此参数还返回的用于保存的文件名。
'   InitialDir:   对话框中将显示初始目录。如果此值为 null，则使用当前目录。
'   Filter:       要在对话框中显示的文件的筛选器： 说明。这是一个分隔的字符串，会产生"文件类型"组合框中的可用选项。
'                 示例： "Source files (*.bas, *.inc)|*.bas;*.inc|所有的文件 (*.*)|*.*"
'   DefExtension: 默认文件扩展名适用于文件名，如果用户不提供一个。
'   Flags:        Win32 帮助下"OpenFileName"文件中所述的一个或多个值的组合。
'                 E.g.: %OFN_HIDEREADONLY Or %OFN_OVERWRITEPROMPT or %OFN_EXPLORER
'   CenterFlag:   %TRUE  到中心对话框上的桌面屏幕。     
'返回： 如果用户指定的文件名，然后单击确定按钮然后返回值为非零。如果取消对话框中为零。该参数包含将返回该文件被保存为文件的名称。
'   Flags:        A combination of one or more values as described in the
'                 Win32 Help file under "OpenFileName".
'                 E.g.: OFN_HIDEREADONLY Or OFN_OVERWRITEPROMPT or OFN_EXPLORER
'                 Here are the flags:
'                 OFN_ALLOWMULTISELECT	
'                 OFN_CREATEPROMPT	
'                 OFN_ENABLEHOOK	
'                 OFN_ENABLETEMPLATE	
'                 OFN_ENABLETEMPLATEHANDLE	
'                 OFN_EXPLORER	
'                 OFN_EXTENSIONDIFFERENT	
'                 OFN_FILEMUSTEXIST	
'                 OFN_HIDEREADONLY	
'                 OFN_LONGNAMES	
'                 OFN_NOCHANGEDIR	
'                 OFN_NODEREFERENCELINKS	
'                 OFN_NOLONGNAMES	
'                 OFN_NONETWORKBUTTON	
'                 OFN_NOREADONLYRETURN	
'                 OFN_NOTESTFILECREATE	
'                 OFN_NOVALIDATE	
'                 OFN_OVERWRITEPROMPT	
'                 OFN_PATHMUSTEXIST	
'                 OFN_READONLY	
'                 OFN_SHAREAWARE	
'                 OFN_SHOWHELP	
'   CenterFlag:   TRUE to center dialog on the Desktop screen.       
'   Returns:      If the user specifies a filename and clicks the OK button
'                 then the return value is non-zero. Zero if the dialog is
'                 canceled. The parameter FileSpec will return the name
'                 of the file that the file was saved as.
' Help function for FF_SaveFileDialog that will center the dialog on the screen.

Function FF_SaveFileDialog(ByVal HWnd as HWnd, ByRef sCaption as String, ByRef FileSpec As String, ByRef InitialDir As String, ByRef Filter As String, ByRef DefExtension As String, ByVal Flags As UInteger, ByVal CenterFlag As Integer) As Long
   
   Dim sFile As String
   
   Dim Ofn As OPENFILENAMEA
   Dim zFileTitle As ZString * MAX_PATH
   Dim zInitialDir As ZString * MAX_PATH
   Dim zTitle As ZString * MAX_PATH
   Dim zDefExt As ZString * 10
   Dim hHook As Long
   
   Filter = FF_Replace(Filter, "|", Chr(0))
   
   If Len(InitialDir) = 0 Then InitialDir = Curdir
   
   Filter = Filter & Chr(0)
   zInitialDir = InitialDir & Chr(0)
   
   sFile = String(16383, 0)  '<- PLENTY OF ROOM FOR FILE NAME
   Mid(sFile, 1) = FileSpec
   
   zDefExt = DefExtension & Chr(0)
   zTitle = sCaption & Chr(0)
   
   ofn.lStructSize = SizeOf(OPENFILENAME)
   ofn.hWndOwner = HWnd
   ofn.lpstrFilter = StrPtr(Filter)
   ofn.nFilterIndex = 1
   ofn.lpstrFile = StrPtr(sFile)
   ofn.nMaxFile = Len(sFile)
   ofn.lpstrFileTitle = VarPtr(zFileTitle)
   ofn.nMaxFileTitle = SizeOf(zFileTitle)
   ofn.lpstrInitialDir = VarPtr(zInitialDir)
   'ofn.FlagsEx           = 0
   If Len(zTitle) Then
      ofn.lpstrTitle = Varptr(zTitle)
   End If
   
   'If needed, hook the Common dialog so it centers on the screen
   If CenterFlag Then
      If Flags = 0 Then
         Flags = OFN_EXPLORER Or OFN_ENABLESIZING
      End If
      Flags = Flags Or OFN_ENABLEHOOK
      ofn.lpfnHook = Cast(LPOFNHOOKPROC, @FF_CenterSaveFileDialog)
   End If
   
   ofn.Flags = Flags
   ofn.lpstrDefExt = Varptr(zDefExt)
   
   Function = GetSaveFileNameA(ByVal Varptr(ofn))
   
   Filespec = RTrim(sFile, Chr(0))  ' return the filename used to save the file.
   
   
End Function
Function FF_CenterSaveFileDialog(ByVal HWnd as HWnd, ByVal wMsg as Integer, ByVal wParam as WPARAM, ByVal lParam as LPARAM) as Long
   
   Dim pNMHDR as NMHDR Ptr
   Dim rc1 as Rect
   Dim rc2 as Rect
   
   If wMsg = WM_NOTIFY Then
      pNMHDR = Cast(Any Ptr, lParam)
      
      If pNMHDR->Code = CDN_INITDONE Then
         GetWindowRect GetDesktopWindow, VarPtr(rc1)
         GetWindowRect GetParent(HWnd), VarPtr(rc2)
         SetWindowPos GetParent(HWnd), HWND_TOPMOST, (rc1.Left + rc1.Right - rc2.Right + rc2.Left) / 2, _
            (rc1.Top + rc1.Bottom - rc2.Bottom + rc2.Top) / 2, 0, 0, SWP_NOSIZE Or SWP_NOACTIVATE
         
         Function = True : Exit Function
         
      End If
      
   End If
End Function
