(一)编程原理;
由于windows自身并未提供这项接口函数,因此我们必须从分析菜单的实质入手,我认为任何菜单实质上是一个没有标题栏的窗体,菜单项目是某些控件(如标签控件),通过监测鼠标是否移动到控件上而相应的改变控件的背景色和填充色,从而达到相应的目的,当然另外一项关键是如何制造出那一个倒立着的写着“windows98”字样的标题,这需要我们调用复杂的系统函数来实现。
(二)编程实践;
(1)运行
vb6,建立一个标准exe工程,添加命名为form1的窗体,放上一个command控件“command1”,caption=“开始”,调整到适当的位置,双击窗体,写入以下代码:
Private Sub Command1_Click()
frmTest.Show ‘当开始按钮被点击时激活超级菜单
End Sub
Private Sub Form_Load()
Me.left = (Screen.Width - Me.Width) / 2
Me.tOp = (Screen.Height - Me.Height) / 2 ‘窗体位置居中
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If frmTest.Visible = True Then
Unload frmTest
End If ‘当鼠标离开菜单时卸载菜单
End Sub
Private Sub Form_Unload(Cancel As Integer)
End ‘结束
程序 End Sub
(2) 添加命名为frmtest的窗体,添加一个picturebox控件,命名为piclogo,采用默认值就行了,添加控件数组label1(1--6)(读者可以根据自己的需要添加),caption=“菜单项目”,添加一个image控件,将它的图片设计为自己喜欢的图片,移动窗体和
图片到适当位置,双击窗体,写入以下代码:
Option Explicit
Dim cL As New cLogo ‘引用类模块
Private Sub Form_Load()
Me.left = Form1.left
Me.tOp = Form1.tOp - Form1.Height ‘指定窗体位置
Me.Caption = App.Title ‘窗体标题
cL.DrawingObject = picLogo ‘指定piclogo为载体
cL.Caption = ″ 欢迎使用国产软件! --zouhero 2000 ″‘文本
cL.StartColor = vbBlue ‘前段颜色-蓝色
cL.EndColor = vbRed ‘后段颜色-红色
End Sub
Private Sub Form_Resize()
On Error Resume Next
picLogo.Height = Me.ScaleHeight
cL.Draw
End Sub
Private Sub Label1_Click(Index As Integer)
MsgBox ″你选择了菜单″ & Index, vbExclamation
’在这里添加你的相应代码
End Sub
Private Sub Label1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim i As Integer ‘当鼠标移动标签控件时,前景色变成白色,背景色变成蓝色
Label1(Index).BackColor = vbBlue
Label1(Index).ForeColor = &HFFFFFF
For i = 0 To Label1.Count - 1 ‘其他标签颜色恢复原状
If i = Index Then GoTo aa
Label1(i).BackColor = vbButtonFace
Label1(i).ForeColor = &H0
aa:
Next ‘恢复除选定标签外的所有标签的前景色和背景色
End Sub ‘代码结束
(3)选择“工程”菜单-“添加类模块”,命名为clogo,写入以下代码:
Option Explicit ’以下是令人眼花缭乱的win api引用
Private Type RECT
left As Long
tOp As Long
Right As Long
Bottom As Long
End Type
Private Declare Function FillRect Lib ″user32″ (ByVal hDC As Long, lpRect As RECT, ByVal hBrush As Long) As Long
Private Declare Function CreateSolidBrush Lib ″gdi32″ (ByVal crColor As Long) As Long
Private Declare F