VB下怎样打开一个OpenDialog、SaveDialog之类的对话框?
对话框打开的文件怎样传递给调用者?
Dim strFileName As String
CommonDialog1.ShowOpen 打开
CommonDialog1.ShowSave 保存
strFileName = CommonDialog1.FileName
也可以用API函数实现:
GetOpenFileName打开OpenDialog
GetSaveFileName打开SaveDialog
说句废话:要先添加CommonDialog控件:)
同意lilaclone(~~阿九~~) 的
20分给定我。 CRTL+ T 然后 选择 Microsoft common dialog 6.0
然后画出来。 Dim strFileName As String
CommonDialog1.ShowOpen 打开
CommonDialog1.ShowSave 保存
strFileName = CommonDialog1.FileName
20分给我吧。 先打开电脑 然后 --》单击开始——》程序…………再打开
Microsoft Visual Basic 6.0 再打开你要编写的程序
CRTL+ T 然后 选择 Microsoft common dialog control 6.0
这个控件的路径是:c:\windows\system\comdlg32.ocx
双击 在左边工具栏 多出来的哪个图标
代码如下:
Dim strFileName As String
CommonDialog1.ShowOpen 打开
CommonDialog1.ShowSave 保存
strFileName = CommonDialog1.FileName
哈哈 够详细了吧!
用API函数的话,不用加载Microsoft common dialog control 6.0,但是要声明函数:Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) 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
Private Sub Command1_Click()
Dim OFName As OPENFILENAME
OFName.lStructSize = Len(OFName)
Set the parent window
OFName.hwndOwner = Me.hWnd
Set the applications instance
OFName.hInstance = App.hInstance
Select a filter
OFName.lpstrFilter = "Text Files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
create a buffer for the file
OFName.lpstrFile = Space$(254)
set the maximum length of a returned file
OFName.nMaxFile = 255
Create a buffer for the file title
OFName.lpstrFileTitle = Space$(254)
Set the maximum length of a returned file title
OFName.nMaxFileTitle = 255
Set the initial directory
OFName.lpstrInitialDir = "C:\"
Set the title
OFName.lpstrTitle = "Open File "
No flags
OFName.flags = 0
Show the Open File-dialog
If GetOpenFileName(OFName) Then
MsgBox "File to Open: " + Trim$(OFName.lpstrFile)
Else
MsgBox "Cancel was pressed"
End If
End Sub
Private Sub Command2_Click()
Dim OFName As OPENFILENAME
OFName.lStructSize = Len(OFName)
Set the parent window
OFName.hwndOwner = Me.hWnd
Set the applications instance
OFName.hInstance = App.hInstance
Select a filter
OFName.lpstrFilter = "Text Files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
create a buffer for the file
OFName.lpstrFile = Space$(254)
set the maximum length of a returned file
OFName.nMaxFile = 255
Create a buffer for the file title
OFName.lpstrFileTitle = Space$(254)
Set the maximum length of a returned file title
OFName.nMaxFileTitle = 255
Set the initial directory
OFName.lpstrInitialDir = "C:\"
Set the title
OFName.lpstrTitle = "Save File"
No flags
OFName.flags = 0
Show the Save File-dialog
If GetSaveFileName(OFName) Then
MsgBox "File to save: " + Trim$(OFName.lpstrFile)
Else
MsgBox "Cancel was pressed"
End If
End Sub