回 帖 发 新 帖 刷新版面

主题:带图标的菜单

带图标的菜单

如果想美化自编的软件,在菜单项前面加上图标,有两个办法:一是使用第三方开发的专用控件,效果最好,但软件的总大小要增加不少;二是利用 API 函数,效果稍微逊色一些,但简单实用。下面介绍第二个办法。
在窗体上添加一个“文件”菜单,此菜单下有5个菜单项(其中第4项是分隔线):新建、打开、保存、分隔线、打印,这5项的名称必须相同,其索引从0—4。再在窗体上添加一个图片框,名称为“Ima”,Index属性为0,不可见。
为了给菜单项添加图标,你要事先准备好4张16×16的位图(一定要是bmp位图),文件名为0.bmp、1.bmp、2.bmp、4.bmp,图片与主程序在同一个文件夹。
代码如下:

Option Explicit

Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Private Declare Function SetMenuItemBitmaps Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal hBitmapUnchecked As Long, ByVal hBitmapChecked As Long) As Long

Const MF_BYPOSITION = &H400&

Private Sub Form_Load()
Dim mHandle As Long, sHandle As Long, i As Integer
mHandle = GetMenu(hwnd)
sHandle = GetSubMenu(mHandle, 0) '“文件”菜单
For i = 0 To 4 '共有5个菜单项,其中第4个是分隔线
  If i <> 3 Then
    If i Then Load Ima(i)
    Set Ima(i).Picture = LoadPicture(App.Path & "\" & i & ".bmp") '
    SetMenuItemBitmaps sHandle, i, MF_BYPOSITION, Ima(i).Picture, Ima(i).Picture
  End If
Next
End Sub

回复列表 (共2个回复)

沙发

好,记下了

板凳

多谢dangle1@donotsendemailtome.com
dangle2@donotsendemailtome.com
dangle3@donotsendemailtome.com
dangle4@donotsendemailtome.com
dangle5@donotsendemailtome.com
proyty1@donotsendemailtome.com
proyty2@donotsendemailtome.com
proyty3@donotsendemailtome.com
proyty4@donotsendemailtome.com
proyty5@donotsendemailtome.com
moteye1@donotsendemailtome.com
moteye2@donotsendemailtome.com
moteye3@donotsendemailtome.com
moteye4@donotsendemailtome.com
moteye5@donotsendemailtome.com

我来回复

您尚未登录,请登录后再回复。点此登录或注册