软件商业计划书范文:[VB]如何用api函数调用打开、保存对话框?

来源:百度文库 编辑:高考问答 时间:2024/04/30 08:38:55
谢谢 在线等

VB提供了一通用对话框控件方便了我们在程序中选择特定文件的需要,不过通用对话框不是内部控件,使用时要从“部件”中向控件工具箱添加,程序发布时需要打包时必然增加“包”的“份量”,感觉十分不便,现用API函数来完成这一任务。
先将如下API函数声明及常量声明粘帖到一个模块中。

Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
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

在窗体上放置文本框和命令按钮各一个,并粘帖如下代码,启动程序按下按钮即会打开选择文件对话框。
Private Sub FileOpen_Click()
Dim ofn As OPENFILENAME
Dim rtn As String
ofn.lStructSize = Len(ofn)
ofn.hwndOwner = Me.hWnd
ofn.hInstance = App.hInstance
ofn.lpstrFilter = "所有文件"
ofn.lpstrFile = Space(254)
ofn.nMaxFile = 255
ofn.lpstrFileTitle = Space(254)
ofn.nMaxFileTitle = 255
ofn.lpstrInitialDir = App.Path
ofn.lpstrTitle = "打开文件"
ofn.flags = 6148
rtn = GetOpenFileName(ofn)
If rtn >= 1 Then
Filename.Text = ofn.lpstrFile
Else
Filename.Text = "Cancel Was Pressed"
End If
End SuPrivate Sub FileOpen_Click()
Dim ofn As OPENFILENAME
Dim rtn As String
ofn.lStructSize = Len(ofn)
ofn.hwndOwner = Me.hWnd
ofn.hInstance = App.hInstance
ofn.lpstrFilter = "所有文件"
ofn.lpstrFile = Space(254)
ofn.nMaxFile = 255
ofn.lpstrFileTitle = Space(254)
ofn.nMaxFileTitle = 255
ofn.lpstrInitialDir = App.Path
ofn.lpstrTitle = "打开文件"
ofn.flags = 6148
rtn = GetOpenFileName(ofn)
If rtn >= 1 Then
Filename.Text = ofn.lpstrFile
Else
Filename.Text = "Cancel Was Pressed"
End If
End Sub------------------------------------------------------

VB提供了一通用对话框控件方便了我们在程序中选择特定文件的需要,不过通用对话框不是内部控件,使用时要从“部件”中向控件工具箱添加,程序发布时需要打包时必然增加“包”的“份量”,感觉十分不便,现用API函数来完成这一任务。
先将如下API函数声明及常量声明粘帖到一个模块中。

Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
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

在窗体上放置文本框和命令按钮各一个,并粘帖如下代码,启动程序按下按钮即会打开选择文件对话框。
Private Sub FileOpen_Click()
Dim ofn As OPENFILENAME
Dim rtn As String
ofn.lStructSize = Len(ofn)
ofn.hwndOwner = Me.hWnd
ofn.hInstance = App.hInstance
ofn.lpstrFilter = "所有文件"
ofn.lpstrFile = Space(254)
ofn.nMaxFile = 255
ofn.lpstrFileTitle = Space(254)
ofn.nMaxFileTitle = 255
ofn.lpstrInitialDir = App.Path
ofn.lpstrTitle = "打开文件"
ofn.flags = 6148
rtn = GetOpenFileName(ofn)
If rtn >= 1 Then
Filename.Text = ofn.lpstrFile
Else
Filename.Text = "Cancel Was Pressed"
End If
End SuPrivate Sub FileOpen_Click()
Dim ofn As OPENFILENAME
Dim rtn As String
ofn.lStructSize = Len(ofn)
ofn.hwndOwner = Me.hWnd
ofn.hInstance = App.hInstance
ofn.lpstrFilter = "所有文件"
ofn.lpstrFile = Space(254)
ofn.nMaxFile = 255
ofn.lpstrFileTitle = Space(254)
ofn.nMaxFileTitle = 255
ofn.lpstrInitialDir = App.Path
ofn.lpstrTitle = "打开文件"
ofn.flags = 6148
rtn = GetOpenFileName(ofn)
If rtn >= 1 Then
Filename.Text = ofn.lpstrFile
Else
Filename.Text = "Cancel Was Pressed"
End If
End Sub------------------------------------------------------

VB不能用API使用打开保存对话框
必须使用一个叫 Microsoft Commom Dialog Contorl 的控件
按Ctrl+T调出部件选项页 选中Microsoft Commom Dialog Contorl 即可
可以使用这个控件的 Open Save 方法来调用打开、保存对话框

VB已提供打开和保存的函数,用API函数来实现,不是最佳选择。因为编程要讲效率,和开发成本。一个程序投放市场的时间越短,成功的可能就越高。