新建,打开,保存,另存为,退出,退出时保存
程序代码:
Option Explicit
Dim 是否被修改 As Boolean '是否被修改
Dim Filename As String '路径 + 文件名
Dim filename2 As String '仅文件名
Private Sub Form_Load()
Dim j As String
j = Command '命令行参数
If j <> "" Then '如果参数不为空
If Dir(j) <> "" Then '如果文件存在
Call 打开文件(j) '打开文件,否则什么都不做
End If
End If
End Sub
Private Sub Form_Resize()
On Error Resume Next
Text1.Move 120, 120, Me.ScaleWidth - 240, Me.ScaleHeight - 240
End Sub
Private Sub Form_Unload(Cancel As Integer)
'退出
Dim i As Long
Dim j As String
If 是否被修改 Then '被修改
i = MsgBox("文件被修改,是否保存?", vbYesNoCancel)
Select Case i
Case vbNo '否
'什么也不做
Case vbCancel '取消
Cancel = 1
Case vbYes '要保存
If Filename <> "" Then
Call 保存文件
Else
CommonDialog1.Filename = "*.txt"
CommonDialog1.Filter = "文本文档(*.txt)|*.txt|所有文件|*.*"
CommonDialog1.FilterIndex = 1
CommonDialog1.ShowSave
If CommonDialog1.Filename = "*.txt" Then '按下的取消
Cancel = 1
Else
If Not 保存文件(CommonDialog1.Filename) Then '保存文件时被取消
Cancel = 1
End If
End If
End If
End Select
End If
End Sub
Private Sub MenuFileExit_Click()
Unload Me
End Sub
Private Sub MenuFileNew_Click()
'新建
Dim i As Long
Dim j As String
If 是否被修改 Then '被修改
i = MsgBox("文件被修改,是否保存?", vbYesNoCancel)
Select Case i
Case vbNo '否
Call 新建
Case vbCancel '取消
Exit Sub
Case vbYes '要保存
If Filename <> "" Then
Call 保存文件
Else
CommonDialog1.Filename = "*.txt"
CommonDialog1.Filter = "文本文档(*.txt)|*.txt|所有文件|*.*"
CommonDialog1.FilterIndex = 1
CommonDialog1.ShowSave
If CommonDialog1.Filename = "*.txt" Then '按下的取消
Exit Sub
Else
If 保存文件(CommonDialog1.Filename) Then
Call 新建
End If
End If
End If
End Select
Else
Call 新建
End If
End Sub
Private Sub MenuFileOpen_Click()
call MenuFileNew_Click '先调用新建
'打开文件
CommonDialog1.Filename = "*.txt"
CommonDialog1.Filter = "文本文档(*.txt)|*.txt|所有文件|*.*"
CommonDialog1.FilterIndex = 1
CommonDialog1.ShowOpen
If CommonDialog1.Filename <> "*.txt" Then '按下的取消
Call 打开文件(CommonDialog1.Filename)
End If
End Sub
Private Sub MenuFileSave_Click()
'保存
If Filename = "" Then '如果无文件名
Call MenuFileSaveA_Click '等同于另存为
Else
Call 保存文件("", True) '否则直接保存
End If
End Sub
Private Sub MenuFileSaveA_Click()
'另存为
CommonDialog1.Filename = "*.txt"
CommonDialog1.Filter = "文本文档(*.txt)|*.txt|所有文件|*.*"
CommonDialog1.FilterIndex = 1
CommonDialog1.ShowSave
If CommonDialog1.Filename = "*.txt" Then '按下的取消
Exit Sub
Else
Call 保存文件(CommonDialog1.Filename)
End If
End Sub
Private Sub Text1_Change()
Me.Caption = filename2 & " *"
是否被修改 = True
End Sub
Private Function 保存文件(Optional cs As String, Optional cs2 As Boolean = False) As Boolean '保存文件,参数为文件名,参数2表示是否直接覆盖
'返回是否保存,真表示保存了,假表示重名,用户取消了
Dim fr As Long
Dim i As Long, j As String
fr = FreeFile
If cs = "" And Filename = "" Then '如果没有文件名
保存文件 = False '保存失败
Exit Function '退出函数
End If
If cs <> "" Then
j = Filename
Filename = cs
End If
If Dir(Filename) <> "" And Not cs2 Then
i = MsgBox(Filename & "文件存在,是否覆盖!", vbYesNo)
If i = vbNo Then
保存文件 = False
Filename = j '还原文件名
Exit Function
End If
End If
Open Filename For Output Access Write As fr
Print #fr, Text1.Text
Close fr
是否被修改 = False
保存文件 = True
filename2 = Mid(Filename, InStrRev(Filename, "\") + 1)
Me.Caption = filename2
End Function
Private Sub 打开文件(Optional cs As String) '打开文件,参数为文件名
Dim fr As Long
fr = FreeFile
If cs = "" And Filename = "" Then '如果没有文件名
Call 新建 '新建了事
Else '否则打开文件
If cs <> "" Then
Filename = cs
End If
If Dir(Filename) = "" Then
MsgBox Filename & " 文件不存在!"
Exit Sub
End If
Text1.Text = ""
Dim j As String
Open Filename For Input Access Read As fr
Do While Not EOF(fr)
Line Input #fr, j
Text1.Text = Text1.Text & vbCrLf & j
Loop
Close #fr
是否被修改 = False
filename2 = Mid(Filename, InStrRev(Filename, "\") + 1)
Me.Caption = filename2
End If
End Sub
Private Sub 新建()
Text1.Text = ""
Filename = ""
filename2 = ""
是否被修改 = False
Me.Caption = ""
End Sub
你慢慢看吧.
工程很简单,我就不上传了.
[
本帖最后由 风吹过b 于 2010-4-10 22:27 编辑 ]