最近用过的文件会自动出现在文档菜单中,只要用很少的代码,在你的程序中也可实现这样的功能:
声明:
Public Const SHARD_PATH = &H2&
Public Declare Function SHAddToRecentDocs Lib "shell32.dll" (ByVal dwFlags As Long, ByVal dwData As String) As Long
函数:
Public Sub AddRecent(strFile As String)
Dim lRetVal As Long
If strFile = "" Then
lRetVal = SHAddToRecentDocs(SHARD_PATH, vbNullString)
Else
lRetVal = SHAddToRecentDocs(SHARD_PATH, strFile)
End If
End Sub
例子:
AddRecent "C:myfile.txt"
AddRecent "" '清除文档菜单
'读写INI的API函數
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any,
ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
'自定义写入INI函數
Public Function WriteIni(ByVal section As String, ByVal key As String, ByVal value As String) As Boolean
Dim x As Long, Buff As String * 128, I As Integer
Buff = value + Chr(0)
x = WritePrivateProfileString(section, key, Buff, App.Path + "\MenuSetting.ini")
WriteIni = x
End Function
'自定义读取INI函數
Public Function ReadIni(ByVal section As String, ByVal key As String) As String
Dim x As Long, Buff As String * 128, I As Integer
x = GetPrivateProfileString(section, key, "", Buff, 128, App.Path + "\MenuSetting.ini")
I = InStr(Buff, Chr(0))
ReadIni = Trim(Left(Buff, I - 1))
End Function
Option Explicit
'声明用于判断写入INI中的FileName(n)中的n变量
Dim I As String '为了能添在FileName串的后面,声明为String
Private Sub Form_Load()
I = 0 '初值
Text1.Left = 0
Text1.Top = 0
Text1 = ""
Text1.FontSize = 12
Me.Caption = "txtEditor"
Me.Width = 8000
Me.Height = 6000
AddMenu '添加动态菜单
End Sub
Private Sub Form_Resize() '这个没什么可说,为了使例程完整而已
Text1.Width = Me.ScaleWidth
Text1.Height = Me.ScaleHeight
End Sub
Private Sub mnuExit_Click()
End '退出
End Sub
'打开文件
Private Sub mnuOpen_Click()
Dim sF As String
CommonDialog1.Filter = "文档文件(*.txt)|*.txt|所有文件(*.*)|*.*"
CommonDialog1.ShowOpen
Open CommonDialog1.FileName For Input As #1
Text1.Text = StrConv(InputB$(LOF(1), 1), vbUnicode)
Close #1
If I > = 3 Then I = 0 '如大于等于3则返回原值
I = I + 1
sF = "FileName" + I
'打开后写进INI文件
CommonDialog1.FileName = WriteIni("Open", sF, CommonDialog1.FileName)
AddMenu '立即添加使动态菜单生效
End Sub
'添加菜单
Private Sub AddMenu()
Dim fN1 As String, fN2 As String, fN3 As String
'從INI文件中读取数据
fN1 = ReadIni("Open", "FileName1")
fN2 = ReadIni("Open", "FilEName2")
fN3 = ReadIni("Open", "FileName3")
'如数据存在则令动态菜单可见并给其Caption属性赋值
If fN3 < > "" Then mnuSep02.Visible = True: mnuAdd(1).Visible = True: mnuAdd(1).Caption = fN1
If fN2 < > "" Then mnuSep02.Visible = True: mnuAdd(2).Visible = True: mnuAdd(2).Caption = fN2
If fN1 < > "" Then mnuSep02.Visible = True: mnuAdd(3).Visible = True: mnuAdd(3).Caption = fN3
End Sub
Const MF_CHECKED = &H8&
Const MF_APPEND = &H100&
Const TPM_LEFTALIGN = &H0&
Const MF_DISABLED = &H2&
Const MF_GRAYED = &H1&
Const MF_SEPARATOR = &H800&
Const MF_STRING = &H0&
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function CreatePopupMenu Lib "user32" () As Long
Private Declare Function TrackPopupMenu Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal x As Long, ByVal y As Long, ByVal nReserved As Long, ByVal hwnd As Long, ByVal lprc As Any) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Dim hMenu As Long
Private Sub Form_Load()
'Create an empty popupmenu
hMenu = CreatePopupMenu()
'Append a few menu items
AppendMenu hMenu, MF_STRING, ByVal 0&, "Hello !"
AppendMenu hMenu, MF_GRAYED Or MF_DISABLED, ByVal 0&, "Testing ..."
AppendMenu hMenu, MF_SEPARATOR, ByVal 0&, ByVal 0&
AppendMenu hMenu, MF_CHECKED, ByVal 0&, "TrackPopupMenu"
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim Pt As POINTAPI
'Get the position of the mouse cursor
GetCursorPos Pt
If Button = 1 Then
'Show our popupmenu
TrackPopupMenu hMenu, TPM_LEFTALIGN, Pt.x, Pt.y, 0, Me.hwnd, ByVal 0&
Else
'Show our form's default popup menu
TrackPopupMenu GetSystemMenu(Me.hwnd, False), TPM_LEFTALIGN, Pt.x, Pt.y, 0, Me.hwnd, ByVal 0&
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Destroy our menu
DestroyMenu hMenu
End Sub