如何用自己做的程序直接打开文本文件???

来源:百度知道 编辑:UC知道 时间:2024/06/21 21:47:38
最近用VB做了一个类似记事本的程序,却发现一个问题?把程序生成为exe文件后,直接在文本文件上按右键选择打开方式,当选择用记事本或者写字板打开时,都可以正常打开(文本文件内的文字都能出来),如果选择用刚生成的程序打开时,只看见程序被打开,TextBox里面却什么也看不到,是不是程序中少了一些Form_Load时的脚本呢?希望大家帮帮忙让程序打开时也能看见文本文件内的文字,谢谢了!

用Command.用你做的程序打开文件时,你的程序里的Command就是那个文件的地址:

Private Sub Form_Load()
If Command <> "" Then
Text1 = Command
Text1 = Replace(Text1, """", "")'Command得到的地址是带双引号的,要把双引号去掉
Open Text1 For Input As #1
Text1 = StrConv(InputB$(LOF(1), 1), vbUnicode)
Close #1
End If
End Sub

用api接受windows消息及时文本。

Function FileText(ByVal Filename As String) As String
On Error Resume Next
Dim Handle As Integer
' 判断文件存在性
If Len(Dir$(Filename)) = 0 Then
Err.Raise 53 '文件没有找到
End If
' 以binary模式打开文件
Handle = FreeFile
Open Filename$ For Binary As #Handle
' 读取内容,关闭文件
FileText = Space$(LOF(Handle))
Get #Handle, , FileText
Close #Handle
End Function

然后,在form_load中添加:

filetext(command)