主题:[讨论]关于用API打开“另存为”对话框的问题
Option Explicit
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Const OFN_OVERWRITEPROMPT = &H2 '当覆盖时提示
Private Const OFN_PATHMUSTEXIST = &H800 '路径必须存在
'* VB自带的API浏览器里的声明
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
'* FoxAPI Ver1.5里的声明,出错
'Private Type OPENFILENAME
' lStructSize As Long
' lpstrDefExt As String
' lCustData As Long
' lpfnHook As Long
' lpTemplateName 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
' lpstrCustomFilter As String
' nMaxCustFilter As Long
' nFilterIndex As Long
' lpstrFile As String
' hInstance As Long
' lpstrFilter As String
' hwndOwner As Long
'End Type
Private Function ShowSave() As String
Dim ofname As OPENFILENAME
ofname.lStructSize = Len(ofname)
ofname.hwndOwner = Form1.hWnd
ofname.lpstrFilter = "JPG Files (*.jpg)" + Chr$(0) + "*.jpg" + Chr$(0) + "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
ofname.lpstrFile = Space$(254)
ofname.nMaxFile = 255
ofname.lpstrFileTitle = Space$(254)
ofname.nMaxFileTitle = 255
ofname.lpstrInitialDir = "C:\"
ofname.lpstrTitle = "另存为"
ofname.flags = OFN_OVERWRITEPROMPT Or OFN_PATHMUSTEXIST
ofname.lpstrDefExt = ".jpg"
Debug.Print ofname.nFileExtension
If GetSaveFileName(ofname) Then
ShowSave = Trim$(ofname.lpstrFile)
Else
ShowSave = ""
End If
End Function
Private Sub Command1_Click()
Debug.Print ShowSave
End Sub
当声明OPENFILENAME结构时使用vb自带的api浏览器里的格式时,是正常的。
但使用FoxAPI v1.5里的声明时,vb就崩溃了~~。
两种声明只是结构里的元素的顺序不一样而已,真是其名其妙。
难道是由于所谓的“内存对齐”的原因??
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Const OFN_OVERWRITEPROMPT = &H2 '当覆盖时提示
Private Const OFN_PATHMUSTEXIST = &H800 '路径必须存在
'* VB自带的API浏览器里的声明
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
'* FoxAPI Ver1.5里的声明,出错
'Private Type OPENFILENAME
' lStructSize As Long
' lpstrDefExt As String
' lCustData As Long
' lpfnHook As Long
' lpTemplateName 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
' lpstrCustomFilter As String
' nMaxCustFilter As Long
' nFilterIndex As Long
' lpstrFile As String
' hInstance As Long
' lpstrFilter As String
' hwndOwner As Long
'End Type
Private Function ShowSave() As String
Dim ofname As OPENFILENAME
ofname.lStructSize = Len(ofname)
ofname.hwndOwner = Form1.hWnd
ofname.lpstrFilter = "JPG Files (*.jpg)" + Chr$(0) + "*.jpg" + Chr$(0) + "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
ofname.lpstrFile = Space$(254)
ofname.nMaxFile = 255
ofname.lpstrFileTitle = Space$(254)
ofname.nMaxFileTitle = 255
ofname.lpstrInitialDir = "C:\"
ofname.lpstrTitle = "另存为"
ofname.flags = OFN_OVERWRITEPROMPT Or OFN_PATHMUSTEXIST
ofname.lpstrDefExt = ".jpg"
Debug.Print ofname.nFileExtension
If GetSaveFileName(ofname) Then
ShowSave = Trim$(ofname.lpstrFile)
Else
ShowSave = ""
End If
End Function
Private Sub Command1_Click()
Debug.Print ShowSave
End Sub
当声明OPENFILENAME结构时使用vb自带的api浏览器里的格式时,是正常的。
但使用FoxAPI v1.5里的声明时,vb就崩溃了~~。
两种声明只是结构里的元素的顺序不一样而已,真是其名其妙。
难道是由于所谓的“内存对齐”的原因??