原创,发表于2000 年09 月06 日《中国电脑教育报》34 期25 版
许多软件在启动时都会有“日积月累”或“每日一帖”)之类的窗口打开,其中包含着这个软件的一些基本功能和技巧的解释,用户可以通过它尽快地熟悉和掌握这个软件。我们可以用VB 来实现类似金山词霸中“每日一帖”这个功能。
金山词霸的“每日一帖”条目存储在自己目录(默认为“c:programfileskingsoftxdict”)的tips.txt 文件中的,条目出现的顺序是随机的。金山词霸的初始化文件xdict.ini 存放在当前的windows 目录下,其中的Tips 节的Startup 变量记录着是否“启动时显示”信息。
首先用截图的方法取得金山词霸“每日一帖”的徽标存为xdict.bmp。
创建窗体一(Form1)为显示“每日一帖”界面,其中控件的设置如下表:
所有的字体为宋体,规则样式,五号字体
控件的布局如图一所示:
添加窗体二(Form2)作为主程序的界面,添加一个“帮助”菜单,加入一个菜单条“每日一帖”,如图二所示。
窗体二(Form2)和窗体一(Form1)的关系类
似“金山词霸”和其“每日一帖”的关系。若“每日一帖”中的“启动时显示”未选中,可从帮助菜单中启动之;若“每日一帖”中的“启动时显示”选中,则打开窗口二(Form2)后立即启动窗体一(Form1)。
添加模块一(Module1),设置整个程序由模块一(Module1)中的Sub Main()启动。
程序中有几个需要考虑的问题:
“每日一帖”需要始终在前;
可调用API 函数SetWindowPos 解决:
Declare Function SetWindowPos& Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
SetWindowPos Me.hwnd, -1, 0, 0, 0, 0, 3
包含“启动时显示”标志的xdixt.ini 在当前Windows 目录下,需要进行当
前 Windows 目录的判断;
可调用API 函数GetWindowsDirectory 解决:
Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Public Const MAX_PATH = 260
Sub FindWinDir()
Dim Buffer As String
Buffer = Space(MAX_PATH)
rtn = GetWindowsDirectory(Buffer, Len(Buffer))
WinPath = Left(Buffer, rtn) '
End Sub
在程序运行时需要读取和修改xdixt.ini 文件中Tips 节的Startup 变量的值(0 或1);
从初始化 INI 文件中取得某一键值
Declare Function GetPrivateProfileStringByKeyName& Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName$, ByVal lpszKey$,ByVal lpszDefault$, ByVal lpszReturnBuffer$, ByVal cchReturnBuffer&,ByVal lpszFile$)
Function GetPrivateStringValue(section$, Key$, File$) As String
Dim KeyValue$
Dim characters As Long
KeyValue$ = String$(128, 0)
characters = GetPrivateProfileStringByKeyName(section$, Key$, "",KeyValue$, 127, File$)
If characters > 1 Then
KeyValue$ = Left$(KeyValue$, characters)
End If
GetPrivateStringValue = KeyValue$
End Function
向初始化 INI 文件中写入某一键值
Declare Function WritePrivateProfileStringByKeyName& Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String,ByVal lpKeyName As String, ByVal lpString As String, ByVal lplFileName As String)
WritePrivateProfileStringByKeyName section$, Key$, KeyValue$, , File$
启动后程序将Tips.txt 文件中的条目随机取一条加以显示可以用 Line Input #1 来读取文件中的条目总数量。然后产生一个不大于条目总数的随机量,当读取时碰到的回车符号数量等于随机量时,显示当前条目。
'取得文本文件中条目的数量
Dim lineOfText$
Wrap$ = Chr(13) & Chr(10)
Open TipFileName For Input As #1
On Error GoTo Toobig:
Linenumber = 0
Do Until EOF(1)
Line Input #1, lineOfText$
TempText$ = TempText$ & lineOfText$ & Wrap$
Linenumber = Linenumber + 1
Loop
Close #1
'随机取一条目显示
Randomize
Displayline = Round(Linenumber * Rnd()) + 1
Call display
Toobig:
End Sub
'显示子程序,用于显某行的内容
Sub display()
Dim lineOfText$
Dim i As Integer
If Displayline = 0 Then Displayline = Linenumber
If Displayline = Linenumber + 1 Then Displayline = 1
Open TipFileName For Input As #1
On Error GoTo Toobig:
i = 1
Do Until EOF(1) 'then read lines from file
Line Input #1, lineOfText$
If i = Displayline Then Text1.Text = lineOfText$
i = i + 1
Loop
Toobig:
Close #1
End Sub
添加模块一(Module1),加入API 函数的声明,自定义函数和自程序。设置整个程序由模块一(Module1)中的Sub Main()启动。
整个程序用VB6.0 编写,在Win95/98/NT 上运行通过。
------------------------------------------------------------------------
附程序清单:
模块一(Module1)
Declare Function SetWindowPos& Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Declare Function GetPrivateProfileStringByKeyName& Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName$, ByVal lpszKey$, ByVal lpszDefault$, ByVal lpszReturnBuffer$, ByVal cchReturnBuffer&, ByVal lpszFile$)
Declare Function WritePrivateProfileStringByKeyName& Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lplFileName As String)
Public Const MAX_PATH = 260
Public WinPath As String
Public XdictIniFile As String
Public StartupTag As Boolean
'从INI 文件中取得某一变量的键值
Function GetPrivateStringValue(section$, Key$, File$) As String
Dim KeyValue$
Dim characters As Long
KeyValue$ = String$(128, 0)
characters = GetPrivateProfileStringByKeyName(section$, Key$, "", KeyValue$, 127, File$)
If characters > 1 Then
KeyValue$ = Left$(KeyValue$, characters)
End If
GetPrivateStringValue = KeyValue$
End Function
'判断Windows 的目录
Sub FindWinDir()
Dim Buffer As String
Dim rtn As String
Buffer = Space(MAX_PATH)
rtn = GetWindowsDirectory(Buffer, Len(Buffer))
WinPath = Left(Buffer, rtn) '
End Sub
Sub Main()
Form2.Show
Call FindWinDir
XdictIniFile$ = WinPath & "xdict.ini"
'判断是否“启动时显示”
StartupTag = GetPrivateStringValue("tips", "startup", XdictIniFile$)
If StartupTag = True Then Form1.Show
End Sub
窗体二(Form2)
Private Sub mnuHelpTips_Click()
Form1.Show
End Sub
窗体一(Form1)
Public Displayline As Integer
Public Linenumber As Integer
Public TipFileName As String
'显示子程序,用于显某行的内容
Sub display()
Dim lineOfText$
Dim i As Integer
If Displayline = 0 Then Displayline = Linenumber
If Displayline = Linenumber + 1 Then Displayline = 1
Open TipFileName For Input As #1
On Error GoTo Toobig:
i = 1
Do Until EOF(1) 'then read lines from file
Line Input #1, lineOfText$
If i = Displayline Then Text1.Text = lineOfText$
i = i + 1
Loop
Toobig:
Close #1
End Sub
Private Sub Check1_Click()
'根据选项修改Xdict.ini 文件中的[Tips]中Startup 的值
WritePrivateProfileStringByKeyName "Tips", "Startup", Check1.Value,XdictIniFile$
End Sub
Private Sub Command1_Click()
'向上翻一条目
Displayline = Displayline - 1
Call display
End Sub
Private Sub Command2_Click()
'向下翻一条目
Displayline = Displayline + 1
Call display
End Sub
Private Sub Command3_Click()
Unload Me
End Sub
Private Sub Form_Load()
'窗口初始化:大小,标题和始终在前
Me.Width = 5670
Me.Height = 3255
Me.BorderStyle = 1
Me.Caption = "词霸小技巧(演示)"
Move (Screen.Width - Width) 2, (Screen.Height - Height) 2
rtn = SetWindowPos(Me.hwnd, -1, 0, 0, 0, 0, 3)
'存放每日一帖条目的文本文件,默认为"c:program
fileskingsoftxdictips.txt"
TipFileName = "c:program fileskingsoftxdictips.txt"
'判断是否是“启动时显示”
StartupTag = GetPrivateStringValue("tips", "startup", XdictIniFile$)
If StartupTag = False Then Check1.Value = 0
If StartupTag = True Then Check1.Value = 1
Wrap$ = Chr(13) & Chr(10)
'取得文本文件中条目的数量
Dim lineOfText$
Open TipFileName For Input As #1
On Error GoTo Toobig:
Linenumber = 0
Do Until EOF(1)
Line Input #1, lineOfText$
TempText$ = TempText$ & lineOfText$ & Wrap$
Linenumber = Linenumber + 1
Loop
Close #1
'随机取一条目显示
Randomize
Displayline = Round(Linenumber * Rnd()) + 1
Call display
Toobig:
End Sub