一些菜单函数和位图函数,你需要将这些函数的声明包含在你的应用程序的标准模
块中,具体的内容请参见样例程序。步骤如下:
使用函数GetSubMenu来提取子菜单项的句柄,并通过使用函数CreateCompatibleDC
来创建一个兼容的设备环境描述表;
在一个循环过程中通过使用CreateCompatibleBitmap函数,SelectObject函数以及
BitBlt函数来分别将针对各个菜单项所载入的位图选入到兼容设备环境中;
通过ModifyMenu函数绘制真正的位图菜单选项;
使用DeleteDC函数来释放设备环境,以便其他的程序可以使用它们。
----提取位图可以有多种方法,在本样例程序中在窗体上设置了四个图形框控件,
使用它们载入4个预设的图标来作为菜单选项位图的源文件,当然你也可以使用其
他的方法,例如使用LoadPicture函数来从磁盘装载位图。
样例程序
在Visual Basic中开始一个新的工程,采用缺省的方法建立Form1。
创建一个新的模块,采用缺省的方法建立Module1.Bas。
将如下的声明语句和常量添加到Module1.Bas模块中:
Option Explicit
Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long,
ByVal nPos As Long) As Long
Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long,
ByVal nPos As Long) As Long
Declare Function ModifyMenu Lib "user32" Alias "ModifyMenuA"
(ByVal hMenu As Long,ByVal nPosition As Long, ByVal wFlags As Long,
ByVal wIDNewItem As Long, ByVal lpString As Any) As Long
Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As
Long
Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function CreateCompatibleBitmap Lib "gdi32"
(ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long,
ByVal hObject As Long) As Long
Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As
Long,
ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal
hSrcDC
As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As
Long
Public Const SRCCOPY = &HCC0020
Public Const MF_BYPOSITION = &H400&
Public Const MF_BITMAP = &H4&
注意上面的声明语句需要书写在一行内。
在Form1上添加4个图形框控件,将它们的Name属性设置为Picture1,将它们的
Index属性依次设置为0,1,2,3,将它们的AutoRedrew属性设置为True,将它们
的AutoResize属性设置为Ture,以及将它们的Visable属性设置为False。
将上面的4个图形框控件的Picture属性依次设置为Face1.ico,Face2.ico,Face3.
ico,Face4.ico。
在Form1上添加第一个菜单项,将它的标题设置为“[&F]文件”,名称设置为
mnuFile。在其下添加一个子菜单项,将它的标题设置为“[&E]退出”,名称设置
为mnuExit。
在Form1上添加第二个菜单项,将它的标题设置为“[&A]脸谱”,名称设置为
mnuFace。在其下添加4个子菜单项,分别将改4个子菜单项的名称设置为“[N]正常”
,“[&S]微笑”,“ [&L]大笑”,以及“[&O]悲伤”。将它们的名称设置为“
mnuFaceSel”,并相应将这4个子菜单项的索引设置为0,1,2,3。
将如下的代码添加到Form1的Form_Load事件中:
Private Sub Form_Load()
Dim nLoopCtr As Integer
Dim lResult As Long
Dim hTempDC As Long
Dim nWidth As Integer
Dim nHeight As Integer
Dim lTempID As Long
Dim hMenuID As Long
Dim lItemCount As Long
Dim hBitmap As Long
nWidth = Picture1(nLoopCtr).Width \ Screen.TwipsPerPixelX
nHeight = Picture1(nLoopCtr).Height \ Screen.TwipsPerPixelY
hMenuID = GetSubMenu(GetMenu((Me.hwnd)), 1)
hTempDC = CreateCompatibleDC(Picture1(nLoopCtr).hdc)
For nLoopCtr = 0 To 3
hBitmap = CreateCompatibleBitmap(Picture1(nLoopCtr).hdc, nWidth,
nHeight)
lTempID = SelectObject(hTempDC, hBitmap)
lResult = BitBlt(hTempDC, 0, 0, nWidth, nHeight, (Picture1(
nLoopCtr).hdc), 0, 0, SRCCOPY)
lTempID = SelectObject(hTempDC, lTempID)
nuFaceSel(nLoopCtr).Caption = ""
lResult = ModifyMenu(hMenuID, nLoopCtr, MF_BYPOSITION Or
MF_BITMAP,
GetMenuItemID(hMenuID, nLoopCtr), hBitmap)
Next nLoopCtr
lResult = DeleteDC(hTempDC)
End Sub
将如下的代码添加到“退出”子菜单的单击事件中:
Private Sub mnuExit_Click(Index As Integer)
Select Case Index
Case 0
Unload Me
End Select
End Sub
运行该样例程序,单击“脸谱”菜单,则会看到由4个脸谱图标所形成的位图子菜
单项,单击“文件”\“退出”菜单可退出应用程序。
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/