标准模块:Module1.bas Option ExplicitPublic Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long Public 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窗体模块:'Form1上添加一列表框控件List1,一命令按钮Command1 Option ExplicitPrivate Sub Command1_Click() Dim ofn As OPENFILENAME Dim rtn& ', S$, Str$, i%
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) 'For i = 1 To Len(Trim(ofn.lpstrFile)) ' S = Mid(Trim(ofn.lpstrFile), i, 1) ' If Len(S) <> 0 Then Str = Str & S 'Next 'Debug.Print Str 'List1.AddItem ofn.lpstrFile If rtn <> 0 Then List1.AddItem ofn.lpstrFile Else List1.AddItem "按了取消键!" End If End Sub
至于双击ListBox控件,打开文件显示内容的问题: 用Open (文件名) As ...#1 格式打开文件,然后在RichTextBox中显示内容.
Option ExplicitPublic Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Public 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窗体模块:'Form1上添加一列表框控件List1,一命令按钮Command1
Option ExplicitPrivate Sub Command1_Click()
Dim ofn As OPENFILENAME
Dim rtn& ', S$, Str$, i%
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)
'For i = 1 To Len(Trim(ofn.lpstrFile))
' S = Mid(Trim(ofn.lpstrFile), i, 1)
' If Len(S) <> 0 Then Str = Str & S
'Next
'Debug.Print Str
'List1.AddItem ofn.lpstrFile
If rtn <> 0 Then
List1.AddItem ofn.lpstrFile
Else
List1.AddItem "按了取消键!"
End If
End Sub
用Open (文件名) As ...#1 格式打开文件,然后在RichTextBox中显示内容.